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
Most Recent jsp Faqs
- How to get the real requested URI from inside the error page?
- Can I define a method in a JSP page?
- What is the difference between request.getParameter() and request.getAttribute()?
- What is difference between page and pageContext in JSP pages?
- How to config a JSP file in web.xml?
- What is the difference between jsp:forward and response.sendRedirect?
- How to retrieve values from HTML form input elements in JSP?
Most Viewed jsp Faqs
- How to config a JSP file in web.xml?
- What is difference between page and pageContext in JSP pages?
- How to disable browser caching for a specific JSP?
- How to retrieve values from HTML form input elements in JSP?
- What is the difference between jsp:forward and response.sendRedirect?
- What is the difference between request.getParameter() and request.getAttribute()?
- How to get the current JSP page name?