JSP: Implicit Objects

We know that every JSP page turned into an equivalent Servlet. Every JSP equivalent Servlet must be extended from HttpJspBase class. HttpJspBase class is an abstract class with all implicit objects are defined. Those implicit objects are called as JSP implicit objects. Because of objects’ names are already defined, we have to use them as it is. In JSP there are nine implicit objects are available for every JSP page.

In this article I am going to discuss each individual object. The implicit objects are:

  • request
  • response
  • config
  • application
  • out
  • page
  • exception
  • session
  • pageContext

request Implicit Object

When the user requests for a JSP page, web-browser or client will send an HTTP Request to the server.

JSP’s request object enables to access the HTTP Request within the JSP page to get the parameters information, client information etc.,. request object is an instance of a class that implements the interfaces HttpServletRequest and ServletRequest. JSP Engine will pass this request object to the life-cycle method _jspService

_jspService(HttpServletRequest request, HttpServletResponse response)

to provide the requested service to the user or client. By using request object we can call all HttpServletRequest methods and we can also place additional attributes in this object.

Lets take a simple example: When the user requests for a JSP page with additional parameters like this “http://localhost:8080/sample.jsp?key=xs23eS” ; the web-server receives this request and attempts to process the “sample.jsp” page. Within the JSP page we can write the below statement to access the parameter “key” value.

<%=request.getParameter("key")%>

Above line returns the value of the given parameter “key” as “xs23eS” which we passed through the URL.

By using request object we can get the RequestDispatcher object which is most widely used in servlets. Below code will get this object from request object:

<%
   RequestDispatcher rd = request.getRequestDispatcher("sample.jsp");
%>

RequestDispatcher object is used to send the request to another servlet or JSP.

We will discuss more about this in our upcoming articles.

response Implicit Object

For each request comes from the web-browser or client, JSP Engine (actually Servlet Engine provides a response object) generates a response object. With this response object we can provide the processed information back to the requester (web-browser or client).

response object is an instance of a class that implements the HttpServletResponse and ServletResponse interfaces. By using this response object we can call all HttpServletResponse methods.

For example: Below statement will set the content type of the response. Based on the response’s content type the web-browser or client interpret the response comes from the server.

<% response.setContentType("text/html") %>

config Implicit Object

config object is an instance of a class that implements ServletConfig interface. Generally we use config object to get initialization parameters configured in “web.xml” file (In Tomcat web-server). We can get these parameters by calling getInitParameter() or getInitParameterNames() methods through config object.

To get the name of the servlet, we can use the below statement:

<%=config.getServletName()%>

Above statement returns the name of the servlet.

We can also get ServletContext object by using this config object. ServeltContext object contains the information about the environment in which the servlet is running.

application Implicit Object

application object is used to access application level resources shared within the web application. application object is an instance of type class that implements ServletContext interface. application object is used to get the context parameters and context attributes. For example: below statement

<%=application.getServerInfo()%>

returns the name and version of the servlet container in which the servlet is running.

Through application object also we can get RequestDispatcher object.

<%
RequestDispatcher rd =
application.getRequestDispatcher("sample.jsp");
%>

See in request object section to know more about RequestDispacther object.

out Implicit Object

There must be a simple way to send back the information to the web-browser or client. Tomcat provides out implicit object for this purpose. out object is an instance of the class JspWriter.

For example: By using out object we can write our response on to the web-browser by calling below method:

<%out.println("hello");%>

Above code simply prints “hello” message in web-browser.

page Implicit Object

page object is of type java.lang.Object. page object is nothing but “this” in JSP equivalent servlet. By using page object we can call any methods of the servlet. But before calling any method we need to type-cast it to Servlet because page object is of type Object.

By using page object we can call any methods of Servlet and HttpServlet because the the JSP equalent servlet implements HttpServlet. Below example:

<%=((HttpServlet)page).getServletName()%>

will return the name of JSP equalent servlet. Remember that servlet name and servlet class name are different.

exception Implicit Object

exception object is of type java.lang.Throwable class. By using exception object we can know stack trace or root cause or other details of raised exception. We can’t use this object in all JSP pages.The JSP pages which are for error pages (isErrorPage attribute of page directive set to true.) are only available to use this exception object.

For example: To get the error message we can use below statement:

<%=exception.getMessage()%>

Above code returns the error message string.

session Implicit Object

session object is an instance of a class that implements an interface HttpSession. By default every JSP page can use session object; which means session is automatically enabled. We can disable the session by using session attributes of the page directive.

<%=session.isNew()%>

Above code returns “true” if session is newly created otherwise returns “false”.

pageContext Implicit Object

pageContext object is of type PageContext class. All implicit objects are created from pageContext object. So, we can say that pageContext object maintains all references of implicit objects. Whatever we can do with all implicit objects the same things we can do with pageContext object. By using this object we can set or remove attributes in different scopes.

<%pageContext.getAttribute("name",PageContext.SESSION_SCOPE);%>

Above code searches for the given attribute in session scope; if it finds, it will return the attribute, otherwise returns null.

Lets discuss more JSP concepts in our upcoming articles.

Leave a Reply