10 - JSP Implicit Objects

10.1 Overview of JSP implicit objects

JSP implicit objects are the objects created by the container and are directly available for use. These implicit objects are available in service method which means we can use these objects scriptlets and in expressions tag only because code written inside scriplets goes in service method and we cannot use these variable inside any JSP Declaration tags because code written in declaration tag is class level.

10.2 JSP Implicit Variables

There are total of 9 implicit variables provided by container. Do you remember _jspService() method signature? It takes two arguments request and response (refer below )

public void _jspService(HttpServletRequest request, HttpServletResponse response)
{
  // Custom code
}

Out of the 9 variables two are request and response (available through arguments ) and rest 7 are generated inside _jspService() means local variable to _jspService() method

Since these variable are readily available ,we need not to even import anything to use these variables.

Below are the 9 implicit objects

· request

· response

· session

· out

· application

· config

· page

· pageContext

· exception

Note : You have to use these variable with same name and in a same case as mentioned above.

10.2.1 JSP request

JSP request implicit object is an instance of javax.servlet.http.HttpServletRequest implementation. This object is available as an argument of _jspService() method.

In scriplets we can directly use request variable similarly to service() or doGet()/ doPost() methods in servlet to get the request headers , request parameters , attributes ,content types etc.

For example

<body>
  <%
    String param = request.getParameter("param");
    request.setAttribute("attribute","value");
  %>
  <%= request.getContentType() %>
</body>

10.2.2 JSP response

JSP response implicit object is an instance of javax.servlet.http.HttpServletResponse implementation. This object is also available as an argument of _jspService() method.

All the methods available in HttpServletResponse API like set the response header, redirecting request, adding cookies, encoding URL etc can be used.

For example

<body>
  <%
   response.encodeURL("http://www/google.com");
   response.addCookie(new Cookie("key","val"));
  %>
</body>

10.2.3 JSP session

JSP session implicit object is instance of javax.servlet.http.HttpSession implementation. We can get a session object from request object as well but container provides us this variable directly.

All session’s API like storing, removing, retrieving of attributes, invalidating session etc can be used .

For example

<body>
 <%
  session.setAttribute("attribute","value");
  session.getAttribute("attribute");
  session.removeAttribute("attribute");
 %>
</body>

10.2.4 JSP out

JSP out implicit object is instance of javax.servlet.jsp.JspWriter implementation and it is used to output the content in client response. In servlet we can get the PrintWriter Object using response object like response.getWriter() but in JSP , out variable is available directly for use.

For Example

<body>
 <%
  out.println("Hello World");
 %>
 <%=
  request.getAttribute("attribute")
 %>
</body>

All the content of expressions goes as an argument to out.println().

Above highlighted statement will translated as out.println(“request.getAttribute(“attribute”)”);

10.2.5 JSP application

JSP application implicit object is instance of javax.servlet.ServletContext implementation and it’s used to get the context information and attributes in JSP.

Remember ServletContext is applicable for entire web application. In servlets , ServletContext object can be grabbed by getServletContext() method but in jsp it is available as application object.

All API of ServletContext like to get RequestDispatcher object (to forward the request to another resource or to include the response) , get context param etc are available

For Example

<body>
 <%
  application.getInitParameter("init-param");
  application.getRequestDispatcher("/path");
 %>
</body>

10.2.6 JSP config

JSP config implicit object is instance of javax.servlet.ServletConfig implementation.

ServletConfig is available per servlet or jsp . In servlets , ServletConfig object can be grabbed by getServletConfig() method but in jsp it is available as config object.

All API of ServletConfig like to get initial parameters are available

For Example

<body>
 <%
  config.getInitParameter("init-param");
  config.getServletName();
 %>
</body>

10.2.7 JSP page

JSP page implicit object is instance of java.lang.Object class and represents the current JSP page. This is equivalent to “this” in java which means current instance.

10.2.8 JSP pageContext

JSP pageContext implicit object is instance of javax.servlet.jsp.PageContext implementation and it holds the reference of other implicit objects.

Along with several methods , pageContext defines fields (PAGE_SCOPE, REQUEST_SCOPE, SESSION_SCOPE,APPLICATION_SCOPE) corresponding to 4 scopes as well.

With the help of pageContext object , we can store / remove attributes from any of the four scopes.

Default scope is PAGE but an overloaded API of setAttribute() , getAttribute() and removeAttribute() is provided which takes an additional argument (scope)

For Example

<body>
 <%
  pageContext.setAttribute("attribute", "value");
  pageContext.setAttribute("attribute", "value",PageContext.REQUEST_SCOPE);
  pageContext.getAttribute("attribute");
  pageContext.getAttribute("attribute",PageContext.REQUEST_SCOPE);
  pageContext.removeAttribute("attribute");
  pageContext.removeAttribute("attribute",PageContext.REQUEST_SCOPE);
 %>
</body>

10.2.9 JSP exception

JSP exception implicit object is instance of java.lang.Throwable class and used to provide exception details in JSP error pages.

This implicit object is available only on error pages and to make any JSP page as an error page , we need to add isErrorPage directive on the page

<body>
 <%@page isErrorPage="true" %>
 <%
  exception.printStackTrace();
  exception.getCause();
 %>
</body>

10.3 JSP implicit objects example

Lets create one implicitObjects.jsp file to use all implicit variables. To see config and application objects working we need to add context parameters and init parameters in web.xml .

a) Create one web.xml inside WEB-INF directory with below content

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>JSP Tutorial</display-name>
 <context-param>
   <param-name>MyContextParam</param-name>
   <param-value>Context Parameters are applicable for whole application</param-value>
 </context-param>
 <servlet>
   <description>Implicit Objects</description>
   <servlet-name>ImplicitObjectsExample</servlet-name>
   <jsp-file>/implicitObjects.jsp</jsp-file>
   <init-param>
    <description>config parameters</description>
    <param-name>MyInitParam</param-name>
    <param-value>Init Params are JSP specific</param-value>
   </init-param>
 </servlet>
 <servlet-mapping>
   <servlet-name>ImplicitObjectsExample</servlet-name>
   <url-pattern>/implicitObjects.jsp</url-pattern>
 </servlet-mapping>
</web-app>

If we need to set init params for JSP , we need to define servlet tags similar to Servlet configuration with the only change (highlighted) . Instead of servlet class name , we need to use <jsp-file> tag to define the path of jsp which will be executed on hitting the corresponding url mentioned in mapping .

b) Create implicitObjects.jsp inside WebContent directory with below content

<html>
  <head>
   <title> Implicit Object Example </title>
  </head>
  <body>
   <% response.getWriter().print("Writing Content using response object") ;%>
   <br/>
   Port Number (using request object) :: <%= request.getLocalPort() %>
   <br/>
   Session ID (using session object ) ::<%= session.getId() %>
   <br/>
   <% out.println("Message getting printed (using out object )") ; %>
   <br/>
   Init parameter (using application object) :: <%= application.getInitParameter("MyContextParam") %>
   <br/>
   Init parameter (using config object) :: <%= config.getInitParameter("MyInitParam") %>
   <br/>
   Class Name (using page object) :: <%= page.getClass() %>
   <br/>
   <%
     pageContext.setAttribute("pageContextVariable", "Variable Value",PageContext.REQUEST_SCOPE);
   %>
   Attribute Value (using page context) :: <%= pageContext.getAttribute("pageContextVariable", PageContext.REQUEST_SCOPE)%>
   <br/>
  </body>
</html>

Result- Access implicitObjects.jsp using http://localhost:8080/jsp-tutorial/implicitObjects.jsp

Like us on Facebook