Home Tutorial Java Jsp JSP implicit object "request"

 
 

Share on Google+Share on Google+
JSP implicit object "request"
Posted on: June 22, 2010 at 12:00 AM
Advertisement
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 :

 

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

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. 

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:

requestobject.html

<html>

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

<body>

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

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

<tr>

<td>User Name: </td>

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

</tr>

<tr>

<td>Password: </td>

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

</tr>

<tr>

<td>&nbsp;</td>

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

</tr>

</table>

</form>

</body>

</html>

requestobject.jsp

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

<%

String username, password;

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

username = "";

else

username = request.getParameter("txtUserName");

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

password = "";

else

password = request.getParameter("txtPassword");

%>

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

<tr>

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

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

</tr>

<tr>

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

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

</tr>

</table>

 

OUTPUT :

After pressing "Submit" button :

Download source code

Advertisement

Related Tags for JSP implicit object "request":


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.

Posted on: June 22, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com