Pr.Pg Next Pg

HttpServletRequest Object in jsp tutorials

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

  • The request object provides methods to get HTTP header information including form data, cookies, HTTP methods etc.

  • All the things like headers, cookies or parameters are sent from the client browser to the server during an HTTP request object which most common use of the request object is to obtain parameter or query string values.


Methods to read HTTP Header

  • Below is the list of methods which can be used to read HTTP header in your JSP program. These method are available with HttpServletRequest object.






Cookie[] getCookies()


The getCookies method returns the contents of the Cookie
header, parsed and stored in an array of Cookie objects.


Enumeration getAttributeNames()


Returns an Enumeration containing the names of the attributes available to this request.


Enumeration getHeaderNames()


Rather than looking up one particular header, you can use the getHeaderNames method to get an Enumeration of all header names received on this particular request.


Enumerationget ParameterNames()


Returns an Enumeration of String objects containing the names of the parameters contained in this request.


HttpSession getSession()


Returns the current session associated with this request, or if the request does not have a session, creates one.


HttpSession getSession(boolean create)


Returns the current HttpSession associated with this request or, if if there is no current session and create is true, returns a new session.


Locale getLocale()


Returns the preferred Locale that the client will accept content in, based on the Accept-Language header


Object getAttribute(String name)


Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.


ServletInputStream getInputStream()


Retrieves the body of the request as binary data using a ServletInputStream.


String getAuthType()

The getAuthType methods break the Authorization header into its component pieces.


String getCharacterEncoding()

Returns the name of the character encoding used in the body of this request.


String getContentType()

Returns the MIME type of the body of the request, or null if the type is not known.


String getContextPath()

Returns the portion of the request URI that indicates the context of the request.


String getHeader(String name)

Returns the value of the specified request header as a String.


String getMethod()


Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.


String getParameter(String name)


Returns the value of a request parameter as a String, or null if the parameter does not exist.


String getPathInfo()

Returns any extra path information associated with the URL the client sent when it made this request.


String getProtocol()

Returns the name and version of the protocol the request.


String getQueryString()


Returns the query string that is contained in the request URL after the path.


String getRemoteAddr()


Returns the Internet Protocol (IP) address of the client that sent the request.


String getRemoteHost()


Returns the fully qualified name of the client that sent the request.


String getRemoteUser()


Returns the login of the user making this request, if the user has been authenticated, or null if the user has not been authenticated.


String getRequestURI()


Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.


String getRequestedSessionId()

Returns the session ID specified by the client.


String getServletPath()

Returns the part of this request's URL that calls the JSP.


String[] getParameterValues(String



Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.


boolean isSecure()


Returns a boolean indicating whether this request was made using a secure channel, such as HTTPS.


int getContentLength()


The getContentLength method returns the value of the
Content-Length header (as an int).


int getIntHeader(String name)

Returns the value of the specified request header as an int.


int getServerPort()

Returns the port number on which this request was received.



JSP program to show the HTTP Header of a Client

Sam Sir

<%-- Http Request Header.jsp --%>

<%-- JSP program to show the HTTP Header of a Client --%>


<%@page import="java.util.*;" contentType="text/html" pageEncoding="UTF-8"%>



<title>Http Request Headers Example</title>



<h2>HTTP Request Headers Received</h2>

<table border="1">

<tr><th>Header Name</th>

<th>Header value</th> </tr>


Enumeration enumeration = request.getHeaderNames();

while (enumeration.hasMoreElements()) {

String name = (String) enumeration.nextElement();

String value = request.getHeader(name);



<tr><td><%= name %></td>

<td><%= value %></td></tr>








HTTP request Header

Pr.Pg border                                              Next Pg