ServletRequest To Get Request Data

ServletRequest instance is used to retrieve request information send from client user. We commonly use HttpServletRequest object in java servlet code. You can use it to collect content length, content type, parameter name value pairs, http header etc. You can click here to see detail introduction for it’s method. We will give you 5 java code examples in this article to show you how to use it to get client data.

Example 1: Get request parameter value in url.

Suppose the url is http://localhost:8080/Dev2qaWebAppExample/GetRequestParameter?userName=Jerry. There is a request parameter “userName” in the url. You can use below java code in doGet(HttpServletRequest req, HttpServletResponse resp) method to retrieve the parameter’s value. Because the http request method for this kind of url is get. So servlet doGet() method will be invoked.

 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 /* Use HttpServletRequest's getParameter method to get the parameter value in request url.*/
 String userName = req.getParameter("userName");
 if(userName!=null && !"".equals(userName))
 /* If the parameter's value is not null and empty then print below to the return page.*/
 resp.getWriter().println("Hello " + userName + " , you are welcome to servlet world.");

Example 2: Get request parameter value in html form.

Add following html code in index.jsp which is the web application’s welcome file.

<form action="/Dev2qaWebAppExample/GetRequestParameter" method="post">
<input type="text" id="userName" name="userName"/>
<input type="submit" value="Submit"/>

Please note the input text box must has a name attribute. And it’s value should be userName. If the input text box do not has name attribute, then you can not get the submitted text box’s value by HttpServletRequest object’s getParameter() method in servlet doGet() method.

The java code to get request parameter in Example 2 is same as Example 1.

READ :   Servlet Overview

Example 3: List all the request headers name and value. 

HttpServletRequest’s getHeaderNames() method will return all http header name in the request. And getHeader(String headerName) method will return the header value for it.

 String action = req.getParameter("action");
 if(action!=null && !"".equals(action))
 PrintWriter pw = resp.getWriter();
 pw.println("Request header list.");
 Enumeration paramEnu = req.getHeaderNames();
 Object nameObj = paramEnu.nextElement();
 String name = (String)nameObj;
 String value = req.getHeader(name);
 pw.println(name + " = " + value);

We use action request parameter to switch request actions. If it’s value is “showHeaders” then the java code will list all the http request header name and value pair. You should use below url to invoke above java code. http://localhost:8080/Dev2qaWebAppExample/GetRequestParameter?action=showHeaders. Below picture show the headers for http get request.
http header name and values for get request

You can also invoke above java code by form submit. Add below html code in index.jsp

<form action="/Dev2qaWebAppExample/GetRequestParameter" method="post">
<input type="hidden" name="action" value="showHeaders" />
<input type="text" id="userName" name="userName"/>
<input type="submit" value="Submit"/>

Please notice the name attribute of hidden input form field. Name attribute must be specified otherwise you can not get it’s value use HttpServletRequest getParameter() method. When you click Submit button, you will see below web page.
http header name and values for post request
You can notice that there has more request header in http post request method.

Example http request header explaination. 

host: Is the requested web server’s ip and port.

content-length: The submit data’s length.

user-agent: The browser type and version information.

accept: Specify the type of content data that client can accept.

referer: The previous page url that lead to current page.

accept-language: Specify the languages that client can accept.

Click here to learn detail explanation for http headers.

Example 4: Get client ip and port that send the request. 

getRemoteAddr() : Get the client IP address or the IP address of last proxy that request to the server.

READ :   Servlet RequestDispatcher And SendRedirect

getRemoteHost() : Get the client hostname or the hostname of last proxy that request to the server.

getRemotePort() : Get the client port or the port of last proxy that request to the server.

If method getRemoteAdrr() return a null or empty string. This means the client request to you through a proxy server. Then you need to use req.getHeader(“X-Forwarded-For”) to get the ip address that the last proxy server added in the http header.

else if("getClientInfo".equalsIgnoreCase(action))
 String remoteAddr = req.getRemoteAddr();
 /* This means the client request to you through a proxy server. */
 if(remoteAddr==null || "".equals(remoteAddr))
 remoteAddr = req.getHeader("X-Forwarded-For");
 /* This means you just access the web application in localhost. */
 /* Get localhost inetAddress object */
 InetAddress inetAddressObj = InetAddress.getLocalHost();
 /* Get localhost real ip*/
 remoteAddr = inetAddressObj.getHostAddress();
 /* Get request client port. */
 int remotePort = req.getRemotePort();
 pw.println("Remote Addr = " + remoteAddr);
 pw.println("Remote Port = " + remotePort);

Example 5 : List all cookie values.

Cookie is a small piece of data that stored in web browser. The cookie data is send from web server. Click here to learn more about http cookie.

else if("listCookieValues".equalsIgnoreCase(action))
 Cookie cookieArr[] = req.getCookies();
 StringBuffer cookieBuf = new StringBuffer();
 int len = cookieArr.length;
 for(int i=0;i<len;i++)
 Cookie cookie = cookieArr[i];
 cookieBuf.append("cookie name = " + cookie.getName() + " , ");
 cookieBuf.append("value = " + cookie.getValue() + " , ");
 cookieBuf.append("domain = " + cookie.getDomain() + " , ");
 cookieBuf.append("comment = " + cookie.getComment() + " , ");
 cookieBuf.append("maxage = " + cookie.getMaxAge() + " , ");
 cookieBuf.append("path = " + cookie.getPath() + " , ");
 cookieBuf.append("version = " + cookie.getVersion() + " , ");

 cookieBuf.delete(0, cookieBuf.length());

You can get above code example in in the download file.

Download “” – Downloaded 35 times – 25 KB

(Visited 68 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *