11 - JSP Request

11.1 JSP Request Overview

Web application is based on client and server interactions and both client and server has to send information to each other. When browser (client) sends the request to the server, it sends a lot of information and data to server.

Information sent are of two types –

a) Information about client (browser )like type of browser , encoding ,locale etc are sent as a part of header

b) Data which user has input in form or as query parameters.

Specification provides us a API in HttpServletRequest which allows us to get both type of information.

The request object is an instance of a javax.servlet.http.HttpServletRequest. Each time a client requests a page the JSP engine creates a new instance of request object

11.2 HTTP Request Headers

Browser sends a lot of information about client in the form of headers. Request API provides a method to -

· Get a complete list of headers sent by a client

· get a specific header

Following is the list of most commonly used Request Headers

· Accept-Encoding-Browser sends information about what all encoding the client browser can handle. gzip is one of the encoding which is used to compress the data. Server can check the value of “Accept-Encoding” header and if browser supports gzip encoding , then can send compressed data.

· Accept- This header specifies what all MIME types browser can handle.

· Connection-This header indicates whether the client can handle persistent HTTP connections. Persistent connections permit the client or other browser to retrieve multiple files with a single request. A value of Keep-Alive means that persistent connections should be used

· Content-Length- Server can get the length of data in bytes sent in POST request by this header.

· Cookie – This header is used to retrieve the cookie sent by server.

· Referer- This header specifies the source Url which means a destination page can get the source url from this header.

· User-Agent- This header informs about the client browser . As there are several browser available like Firefox, Internet Explorer, Google Chrome so browser which is being used to initiate the request can be identified.

11.3 HttpServletRequest

This is the Http protocol based request and an instance of javax,servlet.http.HttpServletRequest and extends ServletRequest Interface

Each time a client requests a page the JSP engine creates a new instance of request object

As HttpServletRequest extends Servlet Request all of its methods will be available. Below are the commonly used methods -

· Cookies getCookies()- this methods returns an array containing all of the Cookie objects the client sent with this request.

· String getQueryString() this methods returns the query string that is contained in the request URL.

· String getMethod()- this methods returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.

· HttpSession getSession() this methods returns the current HttpSession associated with this request.

· HttpSession getSession(boolean create)- if value of that argument is passed as true then it will return a current session and if there is no current session returns a new session. In case value is being passed as false then it returns null if session does not exist.

· String getHeader(String headerName) – this method is used to get the any of header value based on given header name.

· Enumeration getHeaderNames()- Returns an enumeration of all the header names the corresponding request.

· String getParameter(String parameterName)- this method is used to get the value of request parameter by name. Request parameters are the parameters sent by the user either as a query parameter or in the html form. Remember its return type is String.

Example if the parameters are sent as query param like http://localhost:8080/jsp-tutorial/index.jsp?param=hello then we can get the parameter in the servlet like request.getParameter(“param”) and this call will return “hello”

· String[] getParameterValues(String parameterName)- this method is similar to getParameter() with the difference is that it returns an array of String containing all of the values the given request parameter.

You might be wondering how we can have a multiple parameters with same name- so think of the scenarios where the check box has been used (user can select multiple options) OR a multi select dropdown.

· Object getAttribute(String attributeName) – this method is used to get the attribute stored in a request scope by attribute name. Remember the return type is Object.

· void setAttribute(String attributeName, Object value)- this method is used to store an attribute in request scope. This method takes two arguments- one is attribute name and another is value.

As getAttribute() returns Object which means setAttribute() takes Object as well.

· ServletContext getServletContext() – this method can be used to get the servlet context. The ServletContext contains information about the web application and is available in all servlets.

Note: Do not get confused between parameter and attribute.

o Parameters are the ones submitted by user in the form of query param or form elements where as attributes are the one which can be stored in request to take it later.

o Parameters cannot be set programmatically where as we can set the attributes.

o Parameters are of type String where as attributes are Objects.

11.4 JSP Request Examples

Question - How to get the query parameter “param” in servlet?

String value = request.getParameter(“param”);

Question -How to store a attribute in request?


Question - How to get the stored request variable in example #b ?

Object value =request.getAttribute(“attributeName”);

String attribValue=value.toString();

Question- Write a JSP that will display all the header information sent by a client?

Create displayHTTPHeaders.jsp in WebContent directory with below content.

   <title>Displaying HTTP Headers</title>
   <%@ page import="java.util.Enumeration"%>
   <h3> HTTP Headers corresponding to this Request </h3>
    Enumeration<String> headerEnumeration= request.getHeaderNames();
    StringBuffer buffer = new StringBuffer()
     String headerName= headerEnumeration.nextElement();
     String headerValue =request.getHeader(headerName);
     buffer.append("<strong>" +headerName + "<strong>:" + headerValue)

Access the JSP using http://localhost:8080/jsp-tutorial/displayHTTPHeaders.jsp

Question : Write an example to show getParameterValues() API ?

Create checkbox.jsp which will display a group of check box and will submit the user selection to displayResults.jsp


    <H1>Select the fruits you like ? </H1> 
    <form name="MutlipleValues" action="displayResults.jsp" method="POST">
      Orange <input type ="checkbox" name="CheckBoxGrp" value="Orange"/>
      Apple <input type ="checkbox" name="CheckBoxGrp" value="Apple"/> 
      Banana<input type ="checkbox" name="CheckBoxGrp" value="Banana"/> 
       <input type="submit" value="Submit"/> 


    <H1>You have selected   </H1> 
    <% String message=""; 
       String values[] = request.getParameterValues("CheckBoxGrp"); 
          for(int i=0;i<values.length;i++)  
             message= message+ "<BR/>" + values[i];  
     <%= message %> 

Access the JSP using http://localhost:8080/jsp-tutorial/checkbox.jsp . Select the fruits and click submit


Like us on Facebook