JSP Request Implicit Object

Request Implicit Object:

In JSP, request is an implicit object of type HttpServletRequest. It is created for each jsp request by the web container. It can be used to get request information such as parameter, header information etc.

This can be used to get the data on a JSP which has been entered by a user already or on a previous page.

The following methods can be used for Request Implicit Object:

  1. getParameter(String name)
  2. getParameterNames()
  3. getParameterValues(String name)
  4. getAttribute(String name)
  5. getAttributeNames()
  6. setAttribute(String,Object)
  7. removeAttribute(String)
  8. getCookies()
  9. getHeader(String name)
  10. getHeaderNames()

 

getParameter(String name):

This method is used to get the value of the request. Say for example if a user logs in to a login page of a website with his credentials and we can get the value of the userid and password used at the login page by means of request.getParameter method.

StringUserid= request.getParameter("userid");
StringPassword= request.getParameter("password");

 

(Enumeration) getParameterNames():

This method returns an enumeration of all string objects containing the names of the parameters contained in this request.

Enumeration ab = request.getParameterNames();

 

String getParameter(String name)

This method returns the value of a request parameter as a string, or null if the parameter does not exist. This can be used with array too like returning the values in an array.

String[] schools = request.getParameterValues("school");

getAttribute(String name):

This is used to the value for the attribute.

 

getAttributeNames():

It is generally used to get the attribute names associated to the current session. It returns the enumeration of attribute names present in session.

Enumerator ab = request.getAttributeNames();

 

setAttribute(String,Object):

This method assigns a value to the attribute. Say for example we have an attribute named as “userid” and a String named “name” having a value “Bob”. If we can use request.setAttribute(“userid”, name), which means we have assigned the value “Bb” to the attribute userid.

removeAttribute(String) :

This method is just the opposite of setAttribute wherein we can remove the value for an attribute. If we use, request.removeAttribute(“userid”)  then we remove the value of userid and if we put request.getAttribute(“userid”) this will return a default value of NULL.

getCookies() :

This method returns an array of cookie objects got from the client. This is mainly used in handling of cookies which we will see later in the JSP sessions.

getHeader(String name): 

This method is used to get the header information of the request.

 

A sample program using Request Implicit object:

We make use of two JPS’s a main.jsp and index.jsp which is used to display the userid and password used for logging in the website by using the request.getParameter() method.

main.jsp:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="index.jsp">
UserName: <input type="text" name="username" /><br><br>
Password: <input type="text" name="password" /><br><br>
<input type="submit" value="Go"/>
</form>
</body>
</html>

index.jsp:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 
<%
String username=request.getParameter("username");
String password=request.getParameter("password");
out.print("Username and Password used to login: " + username + " and " + password);
%>
 
</body>
</html>

When we run the output of the main.jsp we have the below:

RequestImplicit

If we give any details like the above then we will get the below data when we hit the “Go” button.
2RequestImplicit

Posted on July 24, 2014 in Java Server Pages

Share the Story

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top