Request Implicit Object in JSP with examples:
Here we will discuss request implicit object in JSP. It is mainly used to get
the data on a JSP page which has been entered by user on the previous JSP page.
Methods of request Implicit Object:
getParameter(String name) - This method is used to get the value of a
request’s parameter. For example at login page user enters user-id and password
and once the credentials are verified the login page gets redirected to user
information page, then using request.getParameter we can get the value of
user-id and password which user has input at the login page.
String Uid= request.getParameter("user-id");
String Pass= request.getParameter("password");
getParameterNames() - It returns enumeration of all the parameter names
associated to the request.
Enumeration e= request.getParameterNames();
getParameterValues(String name) – It returns the array of parameter
values.
String[] allpasswords = request.getParameterValues("password");
getAttribute(String name) – Used to get the attribute value. request.getAttribute(“adminâ€) would give you the value of attribute admin.
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 e = request.getAttributeNames();
setAttribute(String,Object) – It assigns an object’s value to the attribute. For
example I have an attribute password and a String object str which has a value
“admin†then calling request.setAttribute(“passwordâ€, str) would assign a value
admin to the attribute password.
removeAttribute(String) – By using this method a attribute can be removed and
cannot be used further. For e.g. If you have a statement request.removeAttribute(“useridâ€) on a JSP page then the userid attribute would
be completely removed and request.getAttribute(“useridâ€) would return NULL if
used after the removeAttribute method.
getCookies() – It returns an array of cookie objects received from the client.
This method is mainly used when dealing with cookies in JSP.
getHeader(String name) – This method is used to get the header information of
the request.
getHeaderNames() – Returns enumerator of all header names. Below code snippet
would display all the header names associated with the request.
Enumeration e = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
String str = (String)e.nextElement();
out.println(str);
}
getRequestURI() – This method (request.getRequestURI()) returns the URL of
current JSP page.
getMethod() - It returns HTTP request method. request.getMethod(). For example
it will return GET for a Get request and POST for a Post Request.
getQueryString() – Used for getting the query string associated to the JSP page
URL. It is the string associted to the URL after question mark sign (?).
Request Implicit Object Example:
In the below example we are receiving the input from user in index.html page and
displaying the same information in userinfo.jsp page using request implicit
object.
index.html
<html>
<head>
<title>Enter UserName and Password</title>
</head>
<body>
<form action="userinfo.jsp">
Enter User Name: <input type="text" name="uname" /> <br><br>
Enter Password: <input type="text" name="pass" /> <br><br>
<input type="submit" value="Submit Details"/>
</form>
</body>
</html>
userinfo.jsp:
<%@ page import = " java.util.* " %>
<html>
<body>
<%
String username=request.getParameter("uname");
String password=request.getParameter("pass");
out.print("Name: "+username+" Password: "+password);
%>
</body>
</html>