In this tutorial, you will learn request implicit object in JSP. It is 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 associated to the URL after question mark sign (?).
Request Implicit Object Example
In the following 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>
Output:
Upon running the above JSP page, it shows you the following screen with two text fields of uname
and pass
. User enters the name in name in uname
field and password in the pass
field. Once the details are entered, user clicks the Submit Details button, which redirects the user to userinfo.jsp
page.
This is the output of userinfo.jsp page. See the code of “userinfo.jsp” page above. We have used getParameter() method of request implicit object to fetch the values of uname
and pass
field. These values are displayed on the “userinfo.jsp” page as shown below.
karan sisodiya says
i want to insert time from html into database using jsp