JSP implicit object "request"


 

JSP implicit object "request"

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

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)

getCookies():

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.* " %>

<html>

<head>

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

</head>

<body>

<%

Enumeration enumeration = request.getHeaderNames();

while (enumeration.hasMoreElements()) {

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

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

}

%>

</body>

</html>

OUTPUT :

 

0

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();

getMethod():

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"))
{
.........
.......
}

1

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.

getParameterNames():

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. 

2

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.

3 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:

4

requestobject.html

<html>

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

5

<body>

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

6

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

<tr>

<td>User Name: </td>

7

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

</tr>

<tr>

8

<td>Password: </td>

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

</tr>

9

<tr>

<td>&nbsp;</td>

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

0

</tr>

</table>

</form>

1

</body>

</html>

requestobject.jsp

2

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

<%

3

String username, password;

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

username = "";

4

else

username = request.getParameter("txtUserName");

5

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

password = "";

else

6

password = request.getParameter("txtPassword");

%>

7

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

<tr>

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

8

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

</tr>

<tr>

9

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

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

</tr>

0

</table>

 

OUTPUT :

1

After pressing "Submit" button :

2

Download source code

Ads