This section illustrates more about the JSP implicit object called Request object. 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:
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.
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.
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.
This is the method of the request object used for getting all the cookies existed in the HTTP request object.
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.
This is the method of the request object used for getting the URI of the current page.
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.
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.
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.
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.
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.
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.
This section provides two file for the best explanation about the request object in JSP. These files are as follows:
Above mentioned html file is used for the constructing a form with a file username and another field is the password. When you submit the form you will redirect to the above mentioned JSP file that will show the entered username and the password in a table.
Here is the code the html file:
<html> <head><title>Request Object In JSP.</title></head>
<body> <form action="RequestObjectInJSP.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> </td> <td><input type="submit" value="Submit" name="B1" /></td> </tr> </table> </form> </body> </html>
Output for the html file:
Here is the code of the JSP file:
<%@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 align><b>Your User Name: </b></td> <td><%=username %><br/></td> </tr> <tr> <td><b>Your Password: </b></td> <td><%=password %></td> </tr> </table>
Output for the above JSP code:
Posted on: March 13, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles