Role of PageContext in custom tags

In this post, you will learn:

  • What is PageContext?
  • What is the role of PageContext in custom tags?

What is PageContext?

PageContext is an interface of the JSP API. Implementation of it, is provided by server vendors. An object of type PageContext is created by the server for each request of a JSP page to store the references of the Servlet & JSP API objects which participate in the processing of the request. Following diagram describes which objects are contained by the PageContext.

pageContext

Description of the diagram:

1.0 – A request is sent by the user for a JSP page.

1.1 – A PageContext object is created by the sever for the request and references of request, response, session, config, context and page objects are stored in it.

1.2PageContext and its components are made available as implicit objects to the _jspService() method of the auto generated servlet for the JSP.

Role of PageContext in custom tags:

As the components of the PageContext are made available to JSP programmer in the _jspService() method, the PageContext is rarely used by the programmer in the JSP itself. It doesn’t mean that this object is useless. It is required when the request processing is delegated by the JSP to an external component such as custom tag. In order to process the request the external component need the same environment which is available to a JSP i.e. all the implicit objects of JSP are required by the external component. Following analogy will help you understand this concept.

You may be familiar with “Man Ki Baat” monthly radio broadcast of Mr. Narendra Modi. To record his “Man Ki Baat”, Mr. Modi don’t go to the Akashvani Bhawan rather a studio is setup at his official residence. Because of this studio, Mr. Modi can record his boradcast from his residence itself. Similarly because of PageContext object, a custom tag can do the request processing in spite of being an external object. Following diagram describes the role of PageContext in custom tags.

pageContext_customTag

Description of the diagram:

1.0 – Tag Processor is activated when a custom tag is encountered during the processing of JSP request.

1.1 – The Tag object is created by the Tag Processor.

1.2 – Reference of the PageContext object is provided to the tag object as parameter of setPageContext() life cycle method.

1.3 – Other life cycle methods are invoked.

1.4 – During processing the Tag object obtains the references of implicit objects so that output can be generated for the tag.

Commonly used methods of PageContext:

1. getRequest() – It is used to obtain the reference of the ServletRequest object from the PageContext. It has following syntax:

public ServletRequest getRequest();

2. getResponse() – It is used to obtain the reference of the ServletResponse object from the PageContext. It has following syntax:

public ServletResponse getResponse();

3. getOut() – It is used to obtain the reference of the out object from the PageContext. It has following syntax:

public JspWriter getOut();

4. getSession() – It is used to obtain the reference of the HttpSession object from the PageContext. It has following syntax:

public HttpSession getSession();

5. getServletConfig() – It is used to obtain the reference of the ServletConfig object from the PageContext. It has following syntax:

public ServletConfig getServletConfig();

6. getServletContext() – It is used to obtain the reference of the ServletContext object from the PageContext. It has following syntax:

public ServletContext getServletContext();

7. getAttribute() – It is used to search an attribute in request, session and application scope. It has following syntax:

public Object getAttribute(String attributeName);

8. setAttribute() – It is used to set an attribute in request, session and application scope. It has following syntax:

public void setAttribute(String attributeName, Object value, int scope);

9. include() – It is used to include the contents of the given component to the response of current request. It has following syntax:

public void include(String componentURL);

10. forward() – It is used to forward the request to the given component. It has following syntax:

public void forward(String componentURL);


Its time to recap the main concepts:

  1. PageContext is a JSP API interface. Its implementation is provided by server vendors.
  2. For each JSP request, a PageContext object is created by the server.
  3. This object act as a container of JSP implicit objects.
  4. With the help of PageContext object, implicit objects are made available to external components such as custom tags so that they can process the request just like a Servlet or JSP.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *