Saturday, November 1, 2014

Implicit Objects In JSP

JavaServer Pages (JSP) technology allows you to easily create web content that has both static and dynamic components. JSP technology makes available all the dynamic capabilities of Java Servlet technology but provides a more natural approach to creating static content. In this article, we look into the 9 implicit object of JSP with explanation and examples.

In a web application, multiple web components collaborate with each other and share information by means of objects that are maintained as attributes of four scope objects. You access these attributes by using the getAttribute and setAttribute methods of the class representing the scope

Note In addition to the standard servlet request, session, and application scopes, JSP adds a fourth scope called page scope.


A JSP page can access some specific objects through scripting variables. These objects are provided by the JSP container and are called implicit objects. These implicit objects can be accessed in scriptlets, in expressions, or as part of the EL expressions.



  • application
    The implicit application object provides a reference to the javax.servlet.ServletContext interface. The ServletContext interface is used to provide access to any context-initialization parameters that have been configured for the JSP page via the deployment descriptor of the web application. The ServletContext object and parameters stored in them by the web container are available to the entire web application. The application object provides the developer of the JSP page with access to the ServletContext object.
    In short, application provides access to web context. Its use is similar to that of ServletContext.
  • config
    Similar to the application object, the config object provides a reference to the ServletConfig interface of the web application. The ServletConfig interface is used to provide access to any initialization parameters that have been configured for the JSP page via the deployment descriptor of the web application. The config object provides the JSP developer with access to the ServletConfig object.
    In short, config provides initialization information used by the JSP container. Its use is similar to that of the class ServletConfig.
  • exception
    The implicit exception object is available to JSP to handle the error conditions and report the runtime exceptions using the errorPage page directive. Exception represents an instance of the Throwable object and contains error information. This variable is available only from the JSP error page
  • out
    The implicit out object represents an instance of the JspWriter class that is used to write character data to the response stream. out represents the output write stream JspWriter. This variable points at the same object as HttpServletResponse.getWriter() in servlets. For example, a simplest JSP that returns a result from a Java class called CurrencyConverter might look like this
    <html>
       <body>
         <% out.println(CurrencyConverter.getDollarRate()); %>
       </body>
    </html>
  • page
    The JSP implicit page object is an instance of the Object class and represents the current JSP page. In short, page represents the instance of the JSP’s servlet.
  • pageContext
    A pageContext provides context information by providing access to all the namespaces associated with a JSP page and to several page attributes. Also, it contains the reference to implicit objects. pageContext represents the JSP context and is used with Tag Libraries
  • request
    The request object is an instance of the javax.servlet.http.HttpServletRequest interface. It represents the client request. The request implicit object is generally used to get request parameters, request attributes, header information, and query string values.
  • response
    The implicit response object is an instance of the javax.servlet.http.HttpServletResponse interface and represents the response to be given to the client. The implicit response object is generally used to set the response content type, add cookies, and redirect the response.
  • session
    The JSP implicit session object is an instance of a Java class that implements the javax.servlet.http.HttpSession interface. It is used to store session state for a client.



Examples of Common Implicit Objects

getServletContext().setAttribute("blog", JavaLatte);
This sets the book attribute in a ServletContext without using implicit objects.

request.setAttribute("blog", JavaLatte);
This sets the book attribute in a request object. The request object is also an implicit object in JSP. Hence, setting the attribute in a servlet is similar to setting the attribute in a JSP page.

request.getSession().setAttribute("book", book);
This sets the book attribute in the session without using implicit objects

application.setAttribute("book" book);
This sets the book attribute in the ServletContext using the application implicit object.

request.setAttribute("book" book);
This sets the book attribute in a request object. request is also an implicit object in JSP. Hence, setting the attribute in JSP is similar to setting the attribute in a servlet.

session.setAttribute("book" book);
This sets the book attribute in the session using a session implicit object

pageContext.setAttribute("book" book);
This sets the book attribute in PageContext using a pageContext implicit object. There is no equivalent of pageContext in a servlet. A PageContext instance provides access to all the namespaces associated with a JSP page, provides access to several page attributes, and provides a layer above the implementation details. Implicit objects are added to the pageContext automatically.



If you know anyone who has started learning java, why not help them out! Just share this post with them. Thanks for studying today!...

2 comments:

  1. perfect explanation about java programming .its very useful.thanks for your valuable information.java training in chennai | java training in velachery

    ReplyDelete
  2. Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more java training institutes in chennai | java j2ee training institutes in velachery

    ReplyDelete