Share on Google+Share on Google+

JSP implicit object "request"

In this section, we will discuss about the implicit object "request" and its uses with an example.

JSP implicit object "request"

In this section, we will discuss about the implicit object "request" and its uses with an example. The 'request' object takes the value from the web browser and pass it to the server. This is performed using an HTTP request such as: headers, cookies or arguments.

Methods of the "request" object :

  • getCookies()
  • getHeader(String name)
  • getHeaderNames()
  • getAttribute(String name)
  • getAttributeNames()
  • getMethod()
  • getParameter(String name)
  • getParameterNames()
  • getParameterValues(String name)
  • getQueryString()
  • getRequestURI()
  • setAttribute(String,Object)
  • removeAttribute(String)


This method of 'request ' object retrieves all cookies sent with the request information by the client. The cookies are returned as an array of Cookie Objects.

getHeader(String name):

Whenever an http client sends a request, It also sends the headers with it.All the headers are optional except Content-length, which is required only for POST requests. Here is the list of most commonly used headers are:

1) Accept : The MIME types the browser prefers.

2) Accept- Encoding: The types of data encodings the browser knows how to decode.

3) Accept- Charset: The character set the browser expects.

4) Content-Length: It is mostly used for POST messages, it tells how much data is attached.

5) Cookie: It is one of the most frequently used headers, it returns the cookies.

6) Accept- Language: The language the browser is expecting.

There are many more request headers available. These headers you can see in the output of the program.

getHeaderNames() :  This method retrieves all the header names in the request. The value returned is an enumerator of all header names.

Small Example using "getHeaderNames()"&"getHeader()" : 

<%@ page import = " java.util.* " %>



<title>Getting a header in Jsp</title>




Enumeration enumeration = request.getHeaderNames();

while (enumeration.hasMoreElements()) {

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

out.println("<font size = 3>" +string +": " + request.getHeader(string)+ "</font><br>");







Download source code

getAttribute(String name) :  This method is used to return the value of the attribute. When the attribute is not present, then a null value is returned. If the attribute is present then the return value is the object associated with the attribute.
Example:    Object roseindia = request.getAttribute("test");

getAttributeNames() : 
Using this method ,you can get the name of all the attributes of the current session. The returned value is an enumerator of all attribute names.
Example :
Enumeration exforsys = request.getAttributeNames();


The "getMethod()" of request object is used to return the methods GET, POST, or PUT corresponding to the requested HTTP method used.

For example:

if (request.getMethod().equals("POST"))

In the above example, the method returned by the 'request.getMethod' is compared with POST Method and if the returned method from 'request.getMethod()' equals POST then the statement in "if" block executes.

getParameter(String name):

The "getParameter()" method of request object is used to return the value of a requested parameter as string.


The "getParameterNames()" method of request object is used to return the names of the parameters given in the current request. The names of parameters returned are enumeration of string objects. 

Example:  Enumeration roseindia = request.getParameterNames();

getParameterValues(String name):

 If there are a number of values of parameter to be returned, then the method "getParameterValues(String name)" of request object can be used by the programmer. The "getParameterValues(String name)" method of request object is used to return all the values of a given parameter?s request. The returned values of parameter is a array of string objects. If the requested parameter is found, then the values associated with it are returned as array of string object. If the requested given parameter is not found, then null value is returned by the method.

Example : String[] fruits = request . getParameterValues("fruit") ; 

getQueryString():  This is the method of the request object used for getting the query string which is the values with the attribute of the html from where the jsp page is referenced.

Example:  String roseindia=request.getQueryString();
                  out.println("Result is"+roseindia);

getRequestURI() : The "getRequestURI()" method of request object is used for returning the URL of the current JSP page.

Example :       out.println("URI Requested is " + request.getRequestURI());

Output of the above statement would be: URI Requested is  /Jsp/test.jsp.

setAttribute(String,Object): This method assigns the 'value of object' as the String's value. If the attribute does not exist, then it is created and assigned to the object.

removeAttribute(String):   This method removes the attribute. This method takes a string parameter i.e. the attribute name that has to be removed. After applying this method you can't access the attribute. If you specify the method for getting the value of the attribute which has removed, you will get the null value.

EXAMPLE using "request" object:



<head><title>Request Object In JSP.</title></head>


<form action="requestobject.jsp" method="post">

<table border="0" cellspacing="0" cellpadding="0">


<td>User Name: </td>

<td><input type="text" size="20" name="txtUserName" />



<td>Password: </td>

<td><input type="password" size="20" name="txtPassword" />




<td><input type="submit" value="Submit" name="B1" /></td>







<%@page import="java.util.*" %>


String username, password;

if(request.getParameter("txtUserName") == null)

username = "";


username = request.getParameter("txtUserName");

if(request.getParameter("txtPassword") == null)

password = "";


password = request.getParameter("txtPassword");


<table align="center" bgcolor="ffff00" border="1" cellspacing="0" cellpadding="0">


<td><b>Your User Name: </b></td>

<td><%=username %><br/></td>



<td><b>Your Password: </b></td>

<td><%=password %></td>





After pressing "Submit" button :

Download source code


Posted on: June 22, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null