JSP Request Object
In this JSP tutorial, you will learn about JSP request object, JSP request object methods, getParameter(String name), getParameterNames(), getParameterValues(String name), getQueryString(), getRequestURI(), getServletPath(), setAttribute(String,Object), removeAttribute(String)
The request object in JSP is used to get the values that the client passes to the web server during an HTTP request. The request object is used to take the value from the client’s web browser and pass it to the server. This is performed using an HTTP request such as: headers, cookies or arguments. The class or the interface name of the object request is http.httpservletrequest. The object request is written: Javax.servlet.http.httpservletrequest.
Methods of request Object:
There are numerous methods available for request Object. Some of them are:
- getCookies()
- getHeader(String name)
- getHeaderNames()
- getAttribute(String name)
- getAttributeNames()
- getMethod()
- getParameter(String name)
- getParameterNames()
- getParameterValues(String name)
- getQueryString()
- getRequestURI()
- getServletPath()
- setAttribute(String,Object)
- removeAttribute(String)
Below is a list of the usage with syntax and examples with explanation of each.
getCookies():
The getCookies() method of request object returns all cookies sent with the request information by the client. The cookies are returned as an array of Cookie Objects. We will see in detail about JSP cookies in the coming sections. General syntax of getHeader() of request object is request.getHeader("String").
getHeader()request object returned value is a string.
For instance String exfosys = request.getHeader("exforsys")
The above would retrieve the value of the HTTP header whose name is exforsys in JSP.
getHeader(String name):
The method getHeader(String name) of request object is used to return the value of the requested header. The returned value of header is a string. General syntax of getHeader() of request object is request.getHeader("String")
In the above the returned value is a String.
For instance: String exfosys = request.getHeader("exforsys")
The above retrieves the value of the HTTP header named exforsys in JSP.
getHeaderNames():
The method getHeaderNames() of request object returns all the header names in the request. This method is used to find available headers. The value returned is an enumerator of all header names. General syntax of getHeaderNames() of request object is as follows: request.getHeaderNames();
In the above the returned value is an enumerator.
For example: Enumeration exforsys = request.getHeaderNames();
The above returns all header names under the enumerator exforsys.
getAttribute(String name):
The method getAttribute() of request object is used to return the value of the attribute. The getAttribute() method returns the objects associated with 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. General syntax of getAttribute() of request object is request.getAttribute()
In the above the returned value is an object.
For example: Object exforsys = request.getAttribute("test");
The above retrieves the object stored in the request test and returns the object in exforsys.
getAttributeNames():
The method getAttribute() of request object is used to return the object associated with the particular given attribute. If the user wants to get names of all the attributes associated with the current session, then the request object method getAttributeNames() can be used. The returned value is an enumerator of all attribute names. General syntax of getAttributeNames() of request object is request.getAttributeNames()
For example: Enumeration exforsys = request.getAttributeNames();
The above returns all attribute names of the current session under the enumerator: exforsys.
getMethod():
The getMethod() of request object is used to return the methods GET, POST, or PUT corresponding to the requested HTTP method used. General syntax of getMethod() of request object is request.getMethod()
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):
getParameter() method of request object is used to return the value of a requested parameter. The returned value of a parameter is a string. If the requested parameter does not exist, then a null value is returned. If the requested parameter exists, then the value of the requested parameter is returned as a string. General syntax of getParameter() of request object is request.getParameter(String name)
The returned value by the above statement is a string.
For example: String exforsys = request.getParameter("test");
The above example returns the value of the parameter test passed to the getParameter() method of the request object in the string exforsys. If the given parameter test does not exist then a null value is assigned to the string exforsys.
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. General syntax of getParameterNames() of request object is request.getParameterNames()
Value returned from the above statement getParameterNames() method is enumeration of string objects.
Enumeration exforsys = request.getParameterNames();
The above statement returns the names of the parameters in the current request as an enumeration of string object.
getParameterValues(String name):
The getParameter(String name) method of request object was used to return the value of a requested given parameter. The returned value of the parameter is a string. 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. General syntax of getParameterValues of request object is request.getParameterValues(String name)
The returned value from the above method getParameterValues() is array of string objects.
For example: String[] vegetables = request.getParameterValues("vegetable");
The above example returns a value of parameter vegetable passed to the method getParameterValues() of request object and the returned values are array of string of vegetables.
getQueryString():
The getQueryString() method of request object is used to return the query string from the request. From this method, the returned value is a string. General syntax of getQueryString() of request object is request.getQueryString()
Value returned from the above method is a string.
For example: String exforsys=request.getQueryString();
out.println("Result is"+exforsys);
The above example returns a string exforsys from the method getQueryString() of request object. The value is returned and the string is printed in second statement using out.println statement.
getRequestURI():
The getRequestURI() method of request object is used for returning the URL of the current JSP page. Value returned is a URL denoting path from the protocol name up to query string. General syntax of getRequestURI() of request object is request.getRequestURI()
The above method returns a URL.
For example: out.println("URI Requested is " + request.getRequestURI());
Output of the above statement would be: URI Requested is /Jsp/test.jsp
getServletPath():
The getServletPath() method of request object is used to return the part of request URL that calls the servlet. General syntax of getServletPath() of request object is request.getServletPath()
The above method returns a URL that calls the servlet.
For example: out.println("Path of Servlet is " + request.getServletPath());
The output of the above statement would be: Path of Servlet is/test.jsp
setAttribute(String,Object):
The setAttribute method of request object is used to set object to the named attribute. If the attribute does not exist, then it is created and assigned to the object. General syntax of setAttribute of request object is request.setAttribute(String, object)
In the above statement the object is assigned with named string given in parameter.
For example: request.setAttribute("username", "exforsys");
The above example assigns the value exforsys to username.
removeAttribute(String):
The removeAttribute method of request object is used to remove the object bound with specified name from the corresponding session. If there is no object bound with specified name then the method simply remains and performs no function. General syntax of removeAttribute of request object is request.removeAttribute(String);