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.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 :
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 :
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.
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.
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.
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> </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 :