Back to: Java Servlets Tutorials
ServletRequest Interface in Java
In this article, I am going to discuss the ServletRequest Interface in Java Application. Please read our previous article where we discussed Servlet Communication. At the end of this article, you will understand the following pointers.
- ServletRequest Interface
- Methods of ServletRequest Interface
- HttpServletRequest Interface
- Methods of HttpServletRequest Interface
- Example to understand ServletRequest Interface
- Getting Parameter Values: getParameter() method
- Getting parameter names and values: using getParameterNames() and getParameter() methods
ServletRequest Interface
When a client sends a request to the webserver, the servlet container creates ServletRequest and ServletResponse objects and passes them as an argument to the servlet’s service() method. The request object provides the access to the request information such as header and body information of request data.
public interface ServletRequest
This interface is used for getting data from the client to the servlet for a service request. Defines an object to provide client request information to a servlet. The servlet container creates a ServletRequest object and passes it as an argument to the servlet’s service method. A ServletRequest object provides data including parameter name and values, attributes, and an input stream. Interfaces that extend ServletRequest can provide additional protocol-specific data.
Methods of ServletRequest Interface
- getAttribute(String): It returns the value of the named attribute of the request, or null if the attribute does not exist.
- getAttributeNames(): It returns an enumeration of attribute names contained in this request.
- getCharacterEncoding(): It returns the character set encoding for the input of this request.
- getContentLength(): It returns the size of the request entity data, or -1 if not known.
- getContentType(): It returns the Internet Media Type of the request entity data, or null if not known.
- getInputstream(): It returns an input stream for reading binary data in the request body.
- getParameter(String): It returns a string containing the lone value of the specified parameter, or null if the parameter does not exist.
- getParameterNames(): It returns the parameter names for this request as an enumeration of strings, or an empty enumeration if there are no parameters or the input stream is empty.
- getParameterValues(String): It returns the values of the specified parameter for the request as an array of strings, or null if the named parameter does not exist.
- getProtocol(): It returns the protocol and version of the request as a string of the form <protocol>/ <major version>.<minor version>.
- getReader(): It returns a buffered reader for reading text in the request body.
- getRealPath(String): It applies alias rules to the specified virtual path and returns the corresponding real path, or null if the translation cannot be performed for any reason.
- getRemoteAddr(): It returns the IP address of the agent that sent the request.
- getRemoteHost(): It returns the fully qualified hostname of the agent that sent the request.
- getScheme(): It returns the scheme of the URL used in this request.
- getServerName(): It returns the hostname of the server that received the request.
- getServerPort(): It returns the port number on which this request was received.
- setAttribute(String, Object): It stores an attribute in the request context; these attributes will be reset between requests.
HttpServletRequest Interface
public interface HttpServletRequest extends ServletRequest
It extends the ServletRequest interface to provide request information for HTTP servlets. The servlet container creates an HttpServletRequest object and passes it as an argument to the servlet’s service methods (doGet, doPost, etc).
Methods of HttpServletRequest Interface
- getContextPath(): It returns the portion of the request URI that indicates the context of the request.
- getCookies(): It returns an array containing all of the Cookie objects the client sent with this request.
- getQuesryString(): It helps returns the query string that is contained in the request URL after the path.
- getSession(): It returns the current HttpSession associated with this request.
- getMethod(): It returns the name of the HTTP method with which this request was made.
- getPart(String name): It helps gets the Part with the given name.
- getPathInfo(): It returns any extra path information associated with the URL.
- getServletPath(): It helps returns the part of the request’s URL that calls the servlet.
Example to understand ServletRequest Interface
Getting Parameter Values: getParameter() method
In this example, I am using the getParemeter() method that returns the value of the given parameter. In the “index.html” form, we are taking the user input (name) and storing them in the parameters “user”. In the “MyServlet.java” class, we are getting the value of the parameters (user) by using the getParameter() method.
In this example we have HttpServletRequest as a parameter of doGet() method, HttpServletRequest extends ServletRequest interface so the getParameter() method is available to the “request” object. The “web.xml” file maps the servlet to the URL. Since our form has a details page as action, we mapped the servlet class to the details page.
index.html
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <form method="post" action="check"> Name <input type="text" name="user"> <input type="submit" value="submit"> </form> </body> </html>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>Servlet</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <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> </web-app>
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
Getting parameter names and values: using getParameterNames() and getParameter() methods
In this example, we are using the getParameterNames() and getParameter() methods to get parameter names and values respectively. The getParameterNames() method is used to return an Enumeration of String objects containing the names of the parameters contained in this request. getParameter() method is used to return the value of the given Parameter.
In the “index.html” form, we are taking the user input (name) and storing them in the parameters “user”. In the “MyServlet.java” class, we are getting the value of the parameters (user) by using the getParameter() method and the name of the parameters using the getParameterNames() method. The “web.xml” file maps the servlet to the URL. Since our form has a details page as action, we mapped the servlet class to the details page.
index.html
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <form method="post" action="check"> Name <input type="text" name="user" > <input type="submit" value="submit"> </form> </body> </html>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>Servlet</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <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> </web-app>
MyServlet.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.Enumeration; 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(); Enumeration en=request.getParameterNames(); while(en.hasMoreElements()) { Object obj = en.nextElement(); String param=(String)obj; String user=request.getParameter(param); out.print("Parameter Name: "+param+" Parameter Value : "+user); } out.close(); } }
In the next article, I am going to discuss RequestDispatcher in Servlet. Here, in this article, I try to explain ServletRequest Interface in Java. I hope you enjoy this ServletRequest Interface in Java article.