The purpose of JSP is to make writing web pages simpler. JSP achieves its purpose by providing a set of objects and API’s to developers. These set of objects are created internally by the JSP engine during JSP translation phase. The implicit objects may not be that much used with the advent of of JSTL, EL and Custom tags. But still there importance can’t be denied. In this tutorial, we shall see the list of implicit objects provided by JSP and how are they injected in JSP code by JSP engine. We shall also see the scenario where we can make use of these implicit objects in an application:

List of implicit objects

out: The out implicit variable refers to an instance of javax.servlet.jsp.JspWriter class.
exception: The exception object which helps in handling the exceptions raised from other JSP pages. Relevant information messages can be displayed by using this object.
request: This reference variable points to an instance of HttpServletRequest class.
response: Similar to response variable, it points to an instance of HttpServletResponse class.
session: Each user has its session in server and HttpSession object is exposed as session variable.
application: application refers to a object of ServletContext class.
config: config refers to a object of ServletConfig class.
page: page logically refers to current JSP page and is of type HttpJspPage.
pageContext: pageContext provides a context information as we shall see shortly.

Usage of implicit objects

out: If you want to write some data back to web browser, use the out implicit object to do so. The method print and println are generally used to send character data to the requester. Even the HTML written in the JSP pages is translated to Java code with the help of out object.

exception: There is an attribute named isErrorPage. If the value of this attribute is true then the exception object is available in the JSP for handling the exceptions which are raised by other JSP pages. Those other JSP pages need to specify the errorPage as the current JSP for which isErrorPage is true.

request: HttpServletRequest is very frequently referred to for getting the request parameters and getting a reference to session object. request also provides a context for setting custom attribute data.

response: HttpServletResponse is also used for multiple purposes including setting HTTP headers, cookies and sending response data.

session: HttpSession refers to user session. For every user, a session object is created and user related data is stored in this session object.

application: ServletContext contains application wide initialization information. The application object refers to an object of ServletContext and hence can access this information.

config: ServletConfig is specific to individual servlets (remember JSP is a servlet under the hood). The config object refers to an objct of ServletConfig and can access the servlet init parameters.

page: The page implicit object refers to this instance which is object of HttpJspPage. When a JSP page is translated into a servlet, the servlet extends HttpJspPage class. The real world usage of page implicit object is very limited and is very rarely required to be used.

pageContext: As you would see below that pageContext implicit object is of type javax.servlet.jsp.PageContext and is initialized by the following code:

pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);

Moreover, all other implicit objects are contained within the pageContext object. So all implicit objects are initialized after initialization of pageContext. The pageContext object can be used to store custom attributes with scope as current page only. This means that those attributes won’t be available on subsequent pages.

JSP Translation source code

Consider the following JSP page which uses some of the implicit objects listed above:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% response.sendRedirect("welcome.htm"); %>

When translated, the above JSP page will have the implicit objects created automatically as shown below in the Java code generated by Jasper in Tomcat:

/*
 * Generated by the Jasper component of Apache Tomcat
 * Version: Apache Tomcat/7.0.27
 * Generated at: 2012-06-14 16:59:28 UTC
 * Note: The last modified time of this file was set to
 *       the last modified time of the source file after
 *       generation to assist with modification tracking.
 */
package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

public final class redirect_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

  private static final javax.servlet.jsp.JspFactory _jspxFactory =
          javax.servlet.jsp.JspFactory.getDefaultFactory();

  private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.tomcat.InstanceManager _jsp_instancemanager;

  public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
  }

  public void _jspDestroy() {
  }

  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {

    final javax.servlet.jsp.PageContext pageContext;
    javax.servlet.http.HttpSession session = null;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;


    try {
      response.setContentType("text/html;charset=UTF-8");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			null, true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write('\r');
      out.write('\n');
 response.sendRedirect("welcome.htm"); 
    } catch (java.lang.Throwable t) {
      if (!(t instanceof javax.servlet.jsp.SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try { out.clearBuffer(); } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}

As stated above and repeated here too, pageContext contains a refers to most of other implicit objects. Once pageContext is initialized, other implicit objects are also initialized.

Related Posts

What are the various JSP Implicit Objects admin Java EE
The purpose of JSP is to make writing web pages simpler. JSP achieves its purpose by providing a set of objects and API's to developers. These set of objects are created internally by the JSP engine during JSP translation phase. The implicit objects may not be that much used...
The purpose of JSP is to make writing web pages simpler. JSP achieves its purpose by providing a set of objects and API's to developers. These set of objects are created internally by the JSP engine during <a href="http://www.javaexperience.com/understanding-jsp-lifecycle/">JSP translation phase</a>. The implicit objects may not be that much used with the advent of of JSTL, EL and Custom tags. But still there importance can't be denied. In this tutorial, we shall see the list of implicit objects provided by JSP and how are they injected in JSP code by JSP engine. We shall also see the scenario where we can make use of these implicit objects in an application: <h2>List of implicit objects</h2> <a href="#out">out:</a> The out implicit variable refers to an instance of javax.servlet.jsp.JspWriter class. <a href="#exception">exception:</a> The exception object which helps in handling the exceptions raised from other JSP pages. Relevant information messages can be displayed by using this object. <a href="#request">request:</a> This reference variable points to an instance of HttpServletRequest class. <a href="#response">response:</a> Similar to response variable, it points to an instance of HttpServletResponse class. <a href="#session">session:</a> Each user has its session in server and HttpSession object is exposed as session variable. <a href="#application">application:</a> application refers to a object of ServletContext class. <a href="#config">config:</a> config refers to a <a href="http://www.javaexperience.com/servletconfig-and-sevletcontext/">object of ServletConfig class</a>. <a href="#page">page:</a> page logically refers to current JSP page and is of type HttpJspPage. <a href="#pagecontext">pageContext:</a> pageContext provides a context information as we shall see shortly. <h2>Usage of implicit objects</h2> <a name="out">out:</a> If you want to write some data back to web browser, use the out implicit object to do so. The method print and println are generally used to send character data to the requester. Even the HTML written in the JSP pages is translated to Java code with the help of out object. <a name="exception">exception:</a> There is an attribute named isErrorPage. If the value of this attribute is true then the exception object is available in the JSP for handling the exceptions which are raised by other JSP pages. Those other JSP pages need to specify the errorPage as the current JSP for which isErrorPage is true. <a name="request">request:</a> HttpServletRequest is very frequently referred to for getting the request parameters and getting a reference to session object. request also provides a context for setting custom attribute data. <a name="response">response:</a> HttpServletResponse is also used for multiple purposes including setting HTTP headers, cookies and sending response data. <a name="session">session:</a> HttpSession refers to user session. For every user, a session object is created and user related data is stored in this session object. <a name="application">application:</a> ServletContext contains application wide initialization information. The application object refers to an object of ServletContext and hence can access this information. <a name="config">config:</a> ServletConfig is specific to individual servlets (remember <a href="http://www.javaexperience.com/why-no-constructor-defined-in-servlet/">JSP is a servlet</a> under the hood). The config object refers to an objct of ServletConfig and can access the servlet init parameters. <a name="page">page:</a> The page implicit object refers to this instance which is object of HttpJspPage. When a JSP page is translated into a servlet, the servlet extends HttpJspPage class. The real world usage of page implicit object is very limited and is very rarely required to be used. <a name="pagecontext">pageContext:</a> As you would see below that pageContext implicit object is of type javax.servlet.jsp.PageContext and is initialized by the following code: <blockquote> pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true); </blockquote> Moreover, all other implicit objects are contained within the pageContext object. So all implicit objects are initialized after initialization of pageContext. The pageContext object can be used to store custom attributes with scope as current page only. This means that those attributes won't be available on subsequent pages. <h2>JSP Translation source code</h2> Consider the following JSP page which uses some of the implicit objects listed above: 1 When translated, the above JSP page will have the implicit objects created automatically as shown below in the Java code generated by Jasper in Tomcat: 1 As stated above and repeated here too, pageContext contains a refers to most of other implicit objects. Once pageContext is initialized, other implicit objects are also initialized.
Top 10 Java EE books
Enable gzip in Tomcat and WAMP
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Latest posts by admin (see all)

Comments

comments