Friday, May 21, 2010

JSP Implicit Objects

Implicit objects in jsp are the objects that are created by the container automatically and the container makes them available to the developers, the developer do not need to create them explicitly. Since these objects are created automatically by the container and are accessed using standard variables; hence, they are called implicit objects. The implicit objects are parsed by the container and inserted into the generated servlet code. They are available only within the jspService method and not in any declaration. Implicit objects are used for different purposes. Our own methods (user defined methods) can't access them as they are local to the service method and are created at the conversion time of a jsp into a servlet. But we can pass them to our own method if we wish to use them locally in those functions.

There are nine implicit objects. Here is the list of all the implicit objects:


Object Class
application javax.servlet.ServletContext
config javax.servlet.ServletConfig
exception java.lang.Throwable
out javax.servlet.jsp.JspWriter
page java.lang.Object
PageContext javax.servlet.jsp.PageContext
request javax.servlet.ServletRequest
response javax.servlet.ServletResponse
session javax.servlet.http.HttpSession


Application: These objects has an application scope. These objects are available at the widest context level, that allows to share the same information between the JSP page's servlet and any Web components with in the same application.
Config: These object has a page scope and is an instance of javax.servlet.ServletConfig class. Config object allows to pass the initialization data to a JSP page's servlet. Parameters of this objects can be set in the deployment descriptor (web.xml) inside the element . The method getInitParameter() is used to access the initialization parameters.
Exception: This object has a page scope and is an instance of java.lang.Throwable class. This object allows the exception data to be accessed only by designated JSP "error pages."
Out: This object allows us to access the servlet's output stream and has a page scope. Out object is an instance of javax.servlet.jsp.JspWriter class. It provides the output stream that enable access to the servlet's output stream.
Page: This object has a page scope and is an instance of the JSP page's servlet class that processes the current request. Page object represents the current page that is used to call the methods defined by the translated servlet class. First type cast the servlet before accessing any method of the servlet through the page.
Pagecontext: PageContext has a page scope. Pagecontext is the context for the JSP page itself that provides a single API to manage the various scoped attributes. This API is extensively used if we are implementing JSP custom tag handlers. PageContext also provides access to several page attributes like including some static or dynamic resource.
Request: Request object has a request scope that is used to access the HTTP request data, and also provides a context to associate the request-specific data. Request object implements javax.servlet.ServletRequest interface. It uses the getParameter() method to access the request parameter. The container passes this object to the _jspService() method.
Response: This object has a page scope that allows direct access to the HTTPServletResponse class object. Response object is an instance of the classes that implements the javax.servlet.ServletResponse class. Container generates to this object and passes to the _jspService() method as a parameter.
Session: Session object has a session scope that is an instance of javax.servlet.http.HttpSession class. Perhaps it is the most commonly used object to manage the state contexts. This object persist information across multiple user connection.
-----------------------------------------
<% // Check if attribute has been set Object o = pageContext.getAttribute("com.mycompany.name1", PageContext.PAGE_SCOPE); if (o == null) { // The attribute com.mycompany.name1 may not have a value or may have the value null } // Save data pageContext.setAttribute("com.mycompany.name1", "value0"); // PAGE_SCOPE is the default pageContext.setAttribute("com.mycompany.name1", "value1", PageContext.PAGE_SCOPE); pageContext.setAttribute("com.mycompany.name2", "value2", PageContext.REQUEST_SCOPE); pageContext.setAttribute("com.mycompany.name3", "value3", PageContext.SESSION_SCOPE); pageContext.setAttribute("com.mycompany.name4", "value4", PageContext.APPLICATION_SCOPE); %>

<%-- Show the values --%>
<%= pageContext.getAttribute("com.mycompany.name1") %> <%-- PAGE_SCOPE --%>
<%= pageContext.getAttribute("com.mycompany.name1", PageContext.PAGE_SCOPE) %>
<%= pageContext.getAttribute("com.mycompany.name2", PageContext.REQUEST_SCOPE) %>
<%= pageContext.getAttribute("com.mycompany.name3", PageContext.SESSION_SCOPE) %>
<%= pageContext.getAttribute("com.mycompany.name4", PageContext.APPLICATION_SCOPE) %>

No comments: