Introduction to ServletRequest class
True job of a Servlet is to handle client request. Servlet API provides two important interfaces javax.servlet.ServletRequest and javax.servlet.http.HttpServletRequest to encapsulate client request. Implementation of these interfaces provide important information about client request to a servlet.
Some Important Methods of ServletRequest
Methods | Description |
Object getAttribute(String name) | return attribute set on request object by name |
Enumeration getAttributeName() | return an Enumeration containing the names of the attributes available inthis request |
int getContentLength() | return size of request body |
int getContentType() | return media type of request content |
ServletInputStream getInputStream() | returns a input stream for reading binary data |
String getParameter(String name) | returns value of parameter by name |
String getLocalAddr() | returns the Internet Protocol(IP) address of the interface on which the request was received |
Enumeration getParameterNames() | returns an enumeration of all parameter names |
String[] getParameterValues(String name) | returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist |
ServletContext getServletContext() | return the servlet context of current request. |
String getServerName() | returns the host name of the server to which the request was sent |
int getServerPort() | returns the port number to which the request was sent |
boolean isSecure() | returns a boolean indicating whether this request was made using a secure channel, such as HTTPS. |
void removeAttribute(String name) | removes an attribute from this request |
void setAttribute(String name, Object o) | stores an attribute in this request. |
HttpServletRequest interface
HttpServletRequest interface adds the methods that relates to the HTTP protocol.
Some important methods of HttpServletRequest
Methods | Description |
String getContextPath() | returns the portion of the request URI that indicates the context of the request |
Cookies getCookies() | returns an array containing all of the Cookie objects the client sent with this request |
String getQueryString() | returns the query string that is contained in the request URL after the path |
HttpSession getSession() | returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session |
String getMethod() | Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT. |
Part getPart(String name) | gets the Part with the given name |
String getPathInfo() | returns any extra path information associated with the URL the client sent when it made this request. |
String getServletPath() | returns the part of this request's URL that calls the servlet |
Example demonstrating Servlet Request
In this example, we will show how a parameter is passed to a Servlet in a request object from HTML page.
index.html
<form method="post" action="check">
Name <input type="text" name="user" >
<input type="submit" value="submit">
</form>
web.xml
<servlet>
<servlet-name>check</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>check</servlet-name>
<url-pattern>/check</url-pattern>
</servlet-mapping>
MyServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String user = request.getParameter("user");
out.println("<h2> Welcome "+user+"</h2>");
} finally {
out.close();
}
}
}
Output :