implicit object request JSP Example

1. What is implicit object request JSP?

request is one of the 9 implicit objects, JSP supports. request is an object of javax.servlet.http.HttpServletRequest interface. Being implicit, it comes into existence (or created) automatically and the Programmer can use the object in his code without the necessity of creation.

2. With implicit object request JSP, what Programmer can do?

Programmer can use request object to read HTML parameters sent by the client, to get cookies stored on client-side, header information and client system information etc. Programmer can set attributes with request object as key/value pairs.

3. What are the important methods of implicit object request JSP?

The methods of request object are nothing but the methods of HttpServletRequest interface. Important are getParameter(String), getParameterNames(), getCookies(), getRemoteAddr(), getQueryString() etc.

Following example uses a few methods of implicit object request JSP.

Client HTML Program, File Name: Request.html

Using JSP implict object request

Enter User Name
Enter Password

Server-side JSP Program, File Name: UsingRequest.jsp

  out.println("Using getParameter(String) method
"); String str1 = request.getParameter("t1"); String str2 = request.getParameter("t2"); out.println("User Name: " + str1 + " and Password: " + str2); out.println("

Using getParameterNames()method"); java.util.Enumeration e = request.getParameterNames(); while(e.hasMoreElements()) { String fieldName = (String) e.nextElement(); String fieldValue = request.getParameter(fieldName); out.println("
HTML Field Name: " + fieldName + " Value entered: " + fieldValue); } %>

Using other methods:
User-Agent: <%=request.getHeader("User-Agent") %>
Query String: <%=request.getQueryString() %>
Client address: <%=request.getRemoteAddr() %>
URI: <%=request.getRequestURI() %>
Protocol used <%=request.getProtocol() %>

HTML screen when some values are entered.

implicit object request JSP

Output screenshot of JSP file.

implicit object request JSP

String str1 = request.getParameter("t1");

getParameter("t1") returns the value entered by the user in HTML text box t1.

java.util.Enumeration e = request.getParameterNames();

The getParameterNames() is used to retrieve all the form fields at a time. The Enumeration object e contains all the form field names like t1 and t2.

String fieldName = (String) e.nextElement();
String fieldValue = request.getParameter(fieldName);

The nextElement() method of Enumeration returns the HTML field name as an object of Object class. This object is casted to String and assigned to fieldName. The getParameter(fieldName) returns the value or data entered by the user in string fieldName. The data is printed using out implicit object.

Other methods used are:

User-Agent: <%=request.getHeader("User-Agent") %> 
Query String: <%=request.getQueryString() %>
Client address: <%=request.getRemoteAddr() %>
URI: <%=request.getRequestURI() %>
Protocol used: <%=request.getProtocol() %>

User-Agent returns the browser used by the client.
getQueryString() returns the name of the HTML field and the value entered by the user.
getRemoteAddr() returns the client’s IP address.
getRequestURI() returns the name of JSP file.
getProtocol() returns the protocol used by client to send the request.

Leave a Comment

Your email address will not be published.