Saturday, June 21, 2008

What are Implicit Objects in JSP?


What are Implicit Objects in JSP?

Implicit Objects in JSP are those objects which are created/instantiated implicitly by the JSP Engine (part of the underlying Web Container) as a convenience feature for the JSP page writers who can use them without having to instantiate them. These implicit objects implement the interfaces defined in the JSP/Servlet APIs. There are nine implicit objects in JSP, which are:-

  • request - an object of the class implementing the HttpServletRequest interface which is used for requesting a web resource - dynamic or static. This implicit object has a 'request' scope.
  • response - an object of the class implementing the HttpServletResponse interface which is used to build the response to be sent to the client. This implicit object has a 'page' scope.
  • pageContext - an object of the class which encapsulates the implementation-dependent features of the abstract class PageContext. This object defines the context for the JSP page and it provides a single API to manage various scoped namespaces - getAttributeNamesInScope(int scope) for getting an Enumeration of all the attributes of the specified scope OR getAttributesScope(java.lang.String name) for getting the scope where a given attribute is defined. A PageContext instance is obtained by a JSP implementation class by calling the JspFactory.getPageContext() method and is released by calling the method JspFactory.releasePageContext(). This implicit object has a 'page' scope.
  • application - an object of the class which implements the ServletContext interface. This represents the context of the JSP page's servlet. That means it'll be unique per JVM per web application. Read more about ServletContext in this article - ServletContext vs ServletConfig >>. This implicit object has an 'application' scope.
  • out - an object of the class which encapsulates the implementation-dependent features of the abstract class JspWriter. The abstract class JspWriter has some of the functionalities found in the java.io.BufferedWriter and java.io.PrintWriter classes. But the methods of this class throw java.io.IOException whereas the methods of the PrintWriter class never throw any exception (the caller needs to inquire by using checkError() method). This implicit object has a 'page' scope. If the JSP page directive selects autoflush="true" then all the contents of the buffer are automatically flushed in case of an overflow.
  • config - an object of the class which implements the ServletConfig interface. This object serves as the ServletConfig object for the JSP page's servlet. This implicit object has a 'page' scope.
  • page - an object of the class which implements the HttpJspPage interface. This implicit object is synonymous to the 'this' operator for the JSP page and evidently it has a 'page' scope.
  • session - an object of the class which implements the HttpSession interface. This implicit object has a 'session' scope and it encapsulates the session in which the underlying JSP page has been instantiated.
  • exception - an object of the type Throwable. This implicit object has a 'page' scope and it's used to capture the uncaught Throwable exception which caused the invokation the error page.

An important point to note here is that all these Container supplied implicit objects are accessible only in the Container generated _jspService() method and not in any user defined method of the JSP page.

Update[June 24, 2008]: Read more - Error/Exception Handling in JSP>> for understanding how the implcit object 'exception' is built, passed, accessed, and used by using several error handling mechanisms available for JSPs.



Share/Save/Bookmark


3 comments:

Lavnish said...

why is exception object of type java.lang.Throwable ? why not java.lang.Exception ??

Geek said...

Because the JSP page may throw a JspException due to exceptions of type Error as the root cause (though very rarely - may be due to JVM failure, error in a native method, etc.) as well and if we had the type of the implicit object 'exception' as Exception, we would not have been able to capture such an error.

You may like to go through the post (http://geekexplains.blogspot.com/2008/06/errorexception-handling-in-jsp-using.html) to read more about it.

Thanks for visiting/posting!

Unknown said...

Thanks for such a nice info