JSP implicit objects REQUEST with example - Java @ Desk

Sunday, December 22, 2013

JSP implicit objects REQUEST with example

JSP implicit objects REQUEST with example
In all there are 9 implicit JSP objects.

In our last posts, we have learned
JSP implicit objects OUT with example,
JSP Implicit Object CONFIG with example ,
JSP Implicit Object APPLICATION with example ,
JSP Implicit object session with example ,
JSP implicit object PageContext with example,
JSP implicit object RESPONSE with example,

The request object is an instance of class implementing an javax.servlet.http.HttpServletRequest interface. This object holds the client request that is being sent either through Post or Get request.

Request object is used to fetch the following information:
1) Request Parameters - Returns the value of a request parameter as a String, or null if the parameter does not exist. It uses the getParameter() method to access the request parameter.
2) Header Information - Returns the value of an HTTP header. The method getHeaderNames() can be used to determine what headers are available.
3) Cookies - Get the array of cookies from the request using getCookies() method.
4) Query String - Uses the getQueryString() method to get the query string, if any, passed in the request. Returns null if no query string is passed. Query String is passed after the URL by appending a '?' mark. Multiple query strings are seperated using the '&' symbol.
For ex : Consider a URL : http://localhost:8080/StockQuote/jsp/ImplicitObjectRequest.jsp?firstName=Kumar&secondName=Bhatia
In this, request.getQueryString() will give "firstName=Kumar&secondName=Bhatia"
5) Request URI - Method getRequestURI() gives the "/StockQuote/jsp/ImplicitObjectRequest.jsp" from above URL. It helps in getting which page is getting called. Gets the URI to the current JSP page.
etc.

Below is the sample example usage of few methods from the request object:
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
   pageEncoding="ISO-8859-1"%>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title>Implicit Object 'request' Example</title>  
 </head>  
 <body>  
 <%-- request object example --%>  
 <strong>Request User-Agent</strong>: <%=request.getHeader("User-Agent") %><br><br>  
 <strong>Request Parameter Names</strong>: <%=request.getParameterNames() %><br><br>  
 <strong>Request Cookies</strong>: <%=request.getCookies() %><br><br>  
 <strong>Request Query String</strong>: <%=request.getQueryString() %><br><br>  
 <strong>Request URI</strong>: <%=request.getRequestURI() %><br><br>  
 </body>  
 </html>  
 

Output is :

Request User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)

Request Parameter Names: org.apache.tomcat.util.http.Parameters$NamesEnumeration@ac86bb

Request Cookies: [Ljavax.servlet.http.Cookie;@c16890

Request Query String: firstName=Kumar&secondName=Bhatia

Request URI: /StockQuote/jsp/ImplicitObjectRequest.jsp







No comments:

Post a Comment