JSP Client Request

We have already learnt the JSP processing architecture. In this tutorial we will be learning more details about request made by client.

Browser sends lot of information along with the request. Information sent by browser is stored in the request header of the HTTP request made by browser or any client side program.

Header information sent by Browser :

Header Description Example
Accept Header specifies the MIME types that the browser or other clients can handle. image/png or image/jpeg
Accept-Charset Header specifies the character set used by the browser to display the information. ISO-8859-1.
Accept-Encoding Header specifies the type of encodings handled by browser. gzip or compress
Accept-Language Header specifies the client’s preferred languages if servlet produces result in more than one language. en, en-us
Authorization In case of Password Protected web-pages, this header is useful for clients to identify themselves
Connection This header indicates whether the client can handle persistent HTTP connections. Using persistent connections browser can retrieve multiple files with a single request. Keep-Alive
Content-Length This header is applicable to POST requests. It gives the size of the POST data in bytes.
Cookie Header returns cookies to servers.
Host Header specifies the host and port given in the original URL.
If-Modified-Since Header indicates that the client need the page only if it has been changed or modified after the specified date.
If-Unmodified-Since This header is the reverse of If-Modified-Since
Referer Header indicates the URL of the referring Web page.
User-Agent Header identifies the browser or other client making the request

The HttpServletRequest Object:

  1. The request object is an instance of a javax.servlet.http.HttpServletRequest object.
  2. Whenever client makes a requests to the JSP page, JSP engine creates a new object to represent the request.
  3. The request object have methods by which we can get HTTP header information.
  4. HTTP header information includes form data, cookies, HTTP methods etc.

HTTP Header Request Example:

<%@ page import="java.io.*,java.util.*" %>
<title>HTTP Header Request Example</title>
<h2>HTTP Header Request Example</h2>

<table border="1">
<th>Header Name</th><th>Header Value(s)</th>
   Enumeration headerNames = request.getHeaderNames();
   while(headerNames.hasMoreElements()) {
       String paramName = (String)headerNames.nextElement();
       out.print("<tr><td>" + paramName + "</td>\n");
       String paramValue = request.getHeader(paramName);
       out.println("<td> " + paramValue + "</td></tr>\n");


Output :

Header Name Header Value(s)
host localhost:8080
connection keep-alive
accept text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
user-agent Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
accept-encoding gzip,deflate,sdch
accept-language en-US,en;q=0.8,hi;q=0.6,mr;q=0.4

Explanation :

  1. In the above example, getHeaderNames() method of HttpServletRequest is used to read the HTTP header information.
  2. This method returns an Enumeration that contains the header information associated with the current HTTP request.
  3. We need to iterate through the loop using hasMoreElements() method.
  4. To determine when to stop iterating Enumeration nextElement() method is used