FAQ


Advertisement



What is the pageContext implicit object?

A pageContext implicit object is used for storing and retrieving page-related information and sharing objects within the same translation unit and same request. Also used as a convenience class that maintains a table of all the other implicit objects.

  • It stores referece to the implicit objects. The following example shows how PageContext is used to populate other implicit objects.
    public void _jspService (HttpServletRequest request, HttpServletResponse response)
    throws java.io.IOException, ServletException {

    ...
    try {

    ...
    application = pageContext.getServletContext ();
    config = pageContext.getServletConfig ();
    session = pageContext.getSession ();
    out = pageContext.getOut ();
    ...

    } catch (Throwable t) {
    ...
    } finally {
    ...
    }
    }

  • Provides convenience methods to get and set attributes in different scopes. This example uses attributes to save and retrieve data in each of the four scopes:
    <%
    // Save data
    pageContext.setAttribute("attr1", "value0"); // PAGE_SCOPE is the default
    pageContext.setAttribute("attr2", "value1", PageContext.PAGE_SCOPE);
    pageContext.setAttribute("attr3", "value2", PageContext.REQUEST_SCOPE);
    pageContext.setAttribute("attr4", "value3", PageContext.SESSION_SCOPE);
    pageContext.setAttribute("attr5", "value4", PageContext.APPLICATION_SCOPE);
    %>

    <%-- Show the values --%>
    <%= pageContext.getAttribute("attr1") %>
    <%= pageContext.getAttribute("attr2", PageContext.PAGE_SCOPE) %>
    <%= pageContext.getAttribute("attr3", PageContext.REQUEST_SCOPE) %>
    <%= pageContext.getAttribute("attr4", PageContext.SESSION_SCOPE) %>
    <%= pageContext.getAttribute("attr5", PageContext.APPLICATION_SCOPE) %>
  • Provides convenience methods for transferring requests to other resources in your application:

    PageContext    
      void include (String relativeURL) Includes the output of another resource in the output of the current page.

    Same as ServletRequest.getRequestDispatcher ().include ();

      void forward (String relativeURL) Forwards the request to another resource.

    Same as

    ServletRequest.getRequestDispatcher ().forward ();

    For example, to forward a request to another resource from a servlet, we have to write the following to lines:

    RequestDispatcher rd = request.getRequestDispatcher ("other.jsp");
    rd.forward (request, response);

    In a JSP page, we can do that in just one line by using the pageContext variable:

    pageContext.forward ("other.jsp");

The pageContext object has a type of javax.servlet.jsp.PageContext and according to the API documents:

"A PageContext instance provides access to all the namespaces associated with a JSP page, provides access to several page attributes, as well as a layer above the implementation details. Implicit objects are added to the pageContext automatically"

A PageContext instance is obtained by a JSP implementation class by calling the JspFactory.getPageContext() method, and is released by calling JspFactory.releasePageContext().

  • a single API to manage the various scoped namespaces
  • a number of convenience API?s to access various public objects
  • a mechanism to obtain the JspWriter for output
  • a mechanism to manage session usage by the page
  • a mechanism to expose page directive attributes to the scripting environment
  • mechanisms to forward or include the current request to other active components in the application
  • a mechanism to handle errorpage exception processing


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |