JSP Request Object

JSP Request Object is implicitly available to the developer on Java Server Pages. This object implements javax.servlet.ServletRequest interface. It is useful object in getting request parameters. This object has a scope for that specific request.

This object retrieves values whatever client passes to the server by an HTTP request. Let if you submit the html form with some data to be send to the server, is received by the HTTP request object of the JSP. To access the HTML form data we use the request object and it's several methods like getParameter().

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. you can illustrates more about the request object for parsing form data by using the following program when you will direct copy and paste this in your JSP application.

Methods of the request object is explained one by one as follows:
request.getParameter(String name):

This is the method, used for getting the value of the HTML form attribute. This method returns the string type value i.e. the value of the specified field of an HTML form. This method takes a string type parameter which is the name of the attribute of the html which value has to be retrieved through the request object.
request.getParameterNames():

This is the method of the request object used for getting the enumeration of the attributes of the html form. These values are later retrieved through the java programming language by enumerating the retrieved enumerated data by the method of the request object.
request.getParameterValues(String name):

This is the method of the request object used for getting the string array containing all of the values which are contained by the request parameter. This method takes a String type parameter which is the name of the field of the html form. This method is used where the array of controls of the HTML lies. All the control of the HTML form contains same name and then makes a control array.
request.getCookies():

This is the method of the request object used for getting all the cookies existed in the HTTP request object.
request.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.
request.getRequestURI():

This is the method of the request object used for getting the URI of the current page.
request.getHeaderNames():

This method returns the enumerator of all HTTP header names means this method retrieves name of all the headers in enumeration form that is enumerated and get all the name one by one by using the Enumeration.nextElement() up to the last element of the enumeration.
request.getHeader(String hdr):

This is the method return the value of the HTTP header like query string or the URL. This method takes a string parameter which is the header name retrieved by the method getHeaderNames() of the request object that gives all the HTTP header names in the enumeration form which has to be converted into string form later for getting the value of the value of the HTTP header from if one by one.
request.getAttribute(String):

This method is used for getting the value of the attribute which is set through the setAttribute(String attributeName) method of the request object. This method takes a string type parameter written in double quotes ("") i.e. the attribute name that is specified in the page where the value of the attribute is set with the attribute name is to be retrieved either in the current page or any other page by passing the value the attribute name through the request object by using dispatcher method.
request.getAttributeNames():

Above method is used for retrieving all the attributes name in the current session of the page. This method returns the enumerated data which is to be retrieved later by enumerating.
request.setAttribute(String, object):

Above method sets the value of the attribute for the request which is retrieved later either in the current JSP page or the another JSP page by passing the request object through the dispatcher method. The set value of the attribute is retrieved by the getAttribute(String) method of the request object.
request.removeAttribute(String):

This method removes the attribute. This method takes a string type 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 what has removed, you will get the null value.

JSP Request Object Example:

First JSP (index.jsp)
<html>
<head><title>Implicit Request Object In JSP.</title></head>
  <body>
   <form action="getRequestParam.jsp" method="post">
 <table border="1">
   <tr>
  <td>User Name: </td>
  <td><input type="text" size="20" name="userName" />
   </tr>
   <tr>
  <td>Password: </td>
  <td><input type="password" size="20" name="password" />
   </tr>
   <tr>
         <td>&nbsp;</td>
  <td><input type="submit" value="Submit"/></td>
   </tr>
 </table>
   </form>
  </body>
</html>

Second JSP (getRequestParam.jsp):
<%@page import="java.util.*" %>
<%
 String username;
 String password;
 if(request.getParameter("userName") == null)
  username = "";
 else
  username = request.getParameter("userName");
 
 if(request.getParameter("password") == null)
  password = "";
 else
  password = request.getParameter("password");
%>
<table align="center" bgcolor="pink" border="1">
 <tr>
  <td align><b>Your User Name: </b></td>
  <td><%=username %><br/></td>
 </tr>
 <tr>
  <td><b>Your Password: </b></td>
  <td><%=password %></td>
 </tr>
</table>

Copy implicitobjects folder on webapp directory of tomcat at
C:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0\webapps\

and restart server and hit the following URL
http://localhost:8080/implicitobjects/index.jsp


As you can see above, First JSP has two fields "userName" and "password" have one submit button that points to another JSP getRequestParam.jsp. With that button, parameter "userName" and "password" are sent second jsp.

This parameter is read on Second JSP with the help of getParameter() method of HttpServletRequest object "request". With the help of scriptlet, request object printed the value of parameter "userName" and "password" on browser.

As you have noticed here, request object is implicit and there is no need to define it on JSP. You can call these objects directly with the name of implicit object.

Fill user name and password and submit then forward to second jsp as following url.
http://localhost:8080/implicitobjects/getRequestParam.jsp


Download Example.


<<Previous <<   || Index ||   >>Next >>






No comments:

Post a Comment