java.lang.Object | +--javax.servlet.jsp.PageContext
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 the pageContext automatically.
The PageContext
class is an abstract class, designed to be extended to provide implementation dependent implementations thereof, by conformant JSP engine runtime environments. A PageContext instance is obtained by a JSP implementation class by calling the JspFactory.getPageContext() method, and is released by calling JspFactory.releasePageContext().
An example of how PageContext, JspFactory, and other classes can be used within a JSP Page Implementation object is given elsewhere.
The PageContext provides a number of facilities to the page/component author and page implementor, including:
Methods Intended for Container Generated Code
Some methods are intended to be used by the code generated by the container, not by code written by JSP page authors, or JSP tag library authors.
The methods supportinglifecycle areinitialize()
andrelease()
The following methods enable themanagement of nested JspWriter streams to implement Tag Extensions:pushBody()
andpopBody()
Methods Intended for JSP authors
Some methods provideuniform access to the diverse objects representing scopes. The implementation must use the underlying Servlet machinery corresponding to that scope, so information can be passed back and forth between Servlets and JSP pages. The methods are:setAttribute()
,getAttribute()
,findAttribute()
,removeAttribute()
,getAttributesScope()
andgetAttributeNamesInScope()
.
The following methods provideconvenient access to implicit objects:
getOut()
,getException()
,getPage()
getRequest()
,getResponse()
,getSession()
,getServletConfig()
andgetServletContext()
. The following methods provide support forforwarding, inclusion and error handling:forward()
,include()
, andhandlePageException()
.
APPLICATION Name used to store ServletContext in PageContext name table. | |
APPLICATION_SCOPE Application scope: named reference remains available in the ServletContext until it is reclaimed. | |
CONFIG Name used to store ServletConfig in PageContext name table. | |
EXCEPTION Name used to store uncaught exception in ServletRequest attribute list and PageContext name table. | |
OUT Name used to store current JspWriter in PageContext name table. | |
PAGE Name used to store the Servlet in this PageContext's nametables. | |
PAGE_SCOPE Page scope: (this is the default) the named reference remains available in this PageContext until the return from the current Servlet.service() invocation. | |
PAGECONTEXT Name used to store this PageContext in it's own name table. | |
REQUEST Name used to store ServletRequest in PageContext name table. | |
REQUEST_SCOPE Request scope: the named reference remains available from the ServletRequest associated with the Servlet until the current request is completed. | |
RESPONSE Name used to store ServletResponse in PageContext name table. | |
SESSION Name used to store HttpSession in PageContext name table. | |
SESSION_SCOPE Session scope (only valid if this page participates in a session): the named reference remains available from the HttpSession (if any) associated with the Servlet until the HttpSession is invalidated. |
PageContext() |
findAttribute(java.lang.String name) Searches for the named attribute in page, request, session (if valid), and application scope(s) in order and returns the value associated or null. | |
forward(java.lang.String relativeUrlPath) This method is used to re-direct, or "forward" the current ServletRequest and ServletResponse to another active component in the application. | |
getAttribute(java.lang.String name) Return the object associated with the name in the page scope or null if not found. | |
getAttribute(java.lang.String name, int scope) Return the object associated with the name in the specified scope or null if not found. | |
getAttributeNamesInScope(int scope) Enumerate all the attributes in a given scope | |
getAttributesScope(java.lang.String name) Get the scope where a given attribute is defined. | |
getException() The current value of the exception object (an Exception). | |
getOut() The current value of the out object (a JspWriter). | |
getPage() The current value of the page object (a Servlet). | |
getRequest() The current value of the request object (a ServletRequest). | |
getResponse() The current value of the response object (a ServletResponse). | |
getServletConfig() The ServletConfig instance. | |
getServletContext() The ServletContext instance. | |
getSession() The current value of the session object (an HttpSession). | |
handlePageException(java.lang.Exception e) This method is intended to process an unhandled "page" level exception by redirecting the exception to either the specified error page for this JSP, or if none was specified, to perform some implementation dependent action. | |
handlePageException(java.lang.Throwable t) This method is identical to the handlePageException(Exception), except that it accepts a Throwable. | |
include(java.lang.String relativeUrlPath) Causes the resource specified to be processed as part of the current ServletRequest and ServletResponse being processed by the calling Thread. | |
initialize(Servlet servlet,ServletRequest request,ServletResponse response, java.lang.String errorPageURL, boolean needsSession, int bufferSize, boolean autoFlush) The initialize method is called to initialize an uninitialized PageContext so that it may be used by a JSP Implementation class to service an incoming request and response within it's _jspService() method. | |
popBody() Return the previous JspWriter "out" saved by the matching pushBody(), and update the value of the "out" attribute in the page scope attribute namespace of the PageConxtext | |
pushBody() Return a new BodyContent object, save the current "out" JspWriter, and update the value of the "out" attribute in the page scope attribute namespace of the PageContext | |
release() This method shall "reset" the internal state of a PageContext, releasing all internal references, and preparing the PageContext for potential reuse by a later invocation of initialize(). | |
removeAttribute(java.lang.String name) Remove the object reference associated with the given name, look in all scopes in the scope order. | |
removeAttribute(java.lang.String name, int scope) Remove the object reference associated with the specified name in the given scope. | |
setAttribute(java.lang.String name, java.lang.Object attribute) Register the name and object specified with page scope semantics. | |
setAttribute(java.lang.String name, java.lang.Object o, int scope) register the name and object specified with appropriate scope semantics |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
public static final intPAGE_SCOPE
public static final intREQUEST_SCOPE
public static final intSESSION_SCOPE
public static final intAPPLICATION_SCOPE
public static final java.lang.StringPAGE
public static final java.lang.StringPAGECONTEXT
public static final java.lang.StringREQUEST
public static final java.lang.StringRESPONSE
public static final java.lang.StringCONFIG
public static final java.lang.StringSESSION
public static final java.lang.StringOUT
public static final java.lang.StringAPPLICATION
public static final java.lang.StringEXCEPTION
publicPageContext()
public abstract voidinitialize(Servlet servlet,ServletRequest request,ServletResponse response, java.lang.String errorPageURL, boolean needsSession, int bufferSize, boolean autoFlush) throws java.io.IOException, java.lang.IllegalStateException, java.lang.IllegalArgumentException
The initialize method is called to initialize an uninitialized PageContext so that it may be used by a JSP Implementation class to service an incoming request and response within it's _jspService() method.
This method is typically called from JspFactory.getPageContext() in order to initialize state.
This method is required to create an initial JspWriter, and associate the "out" name in page scope with this newly created object.
This method should not be used by page or tag library authors.
servlet
- The Servlet that is associated with this PageContextrequest
- The currently pending request for this Servletresponse
- The currently pending response for this ServleterrorPageURL
- The value of the errorpage attribute from the page directive or nullneedsSession
- The value of the session attribute from the page directivebufferSize
- The value of the buffer attribute from the page directiveautoFlush
- The value of the autoflush attribute from the page directivejava.io.IOException
- during creation of JspWriterjava.lang.IllegalStateException
- if out not correctly initializedpublic abstract voidrelease()
This method shall "reset" the internal state of a PageContext, releasing all internal references, and preparing the PageContext for potential reuse by a later invocation of initialize(). This method is typically called from JspFactory.releasePageContext().
Subclasses shall envelope this method.
This method should not be used by page or tag library authors.
public abstract voidsetAttribute(java.lang.String name, java.lang.Object attribute)
name
- the name of the attribute to setattribute
- the object to associate with the namejava.lang.NullPointerException
- if the name or object is nullpublic abstract voidsetAttribute(java.lang.String name, java.lang.Object o, int scope)
name
- the name of the attribute to seto
- the object to associate with the namescope
- the scope with which to associate the name/objectjava.lang.NullPointerException
- if the name or object is nulljava.lang.IllegalArgumentException
- if the scope is invalidpublic abstract java.lang.ObjectgetAttribute(java.lang.String name)
name
- the name of the attribute to getjava.lang.NullPointerException
- if the name is nulljava.lang.IllegalArgumentException
- if the scope is invalidpublic abstract java.lang.ObjectgetAttribute(java.lang.String name, int scope)
name
- the name of the attribute to setscope
- the scope with which to associate the name/objectjava.lang.NullPointerException
- if the name is nulljava.lang.IllegalArgumentException
- if the scope is invalidpublic abstract java.lang.ObjectfindAttribute(java.lang.String name)
public abstract voidremoveAttribute(java.lang.String name)
name
- The name of the object to remove.public abstract voidremoveAttribute(java.lang.String name, int scope)
name
- The name of the object to remove.scope
- The scope where to look.public abstract intgetAttributesScope(java.lang.String name)
public abstract java.util.EnumerationgetAttributeNamesInScope(int scope)
public abstractJspWritergetOut()
public abstractHttpSessiongetSession()
public abstract java.lang.ObjectgetPage()
public abstractServletRequestgetRequest()
public abstractServletResponsegetResponse()
public abstract java.lang.ExceptiongetException()
public abstractServletConfiggetServletConfig()
public abstractServletContextgetServletContext()
public abstract voidforward(java.lang.String relativeUrlPath) throwsServletException, java.io.IOException
This method is used to re-direct, or "forward" the current ServletRequest and ServletResponse to another active component in the application.
If the relativeUrlPath begins with a "/" then the URL specified is calculated relative to the DOCROOT of the ServletContext
for this JSP. If the path does not begin with a "/" then the URL specified is calculated relative to the URL of the request that was mapped to the calling JSP.
It is only valid to call this method from a Thread
executing within a _jspService(...)
method of a JSP.
Once this method has been called successfully, it is illegal for the calling Thread
to attempt to modify the ServletResponse
object. Any such attempt to do so, shall result in undefined behavior. Typically, callers immediately return from _jspService(...)
after calling this method.
relativeUrlPath
- specifies the relative URL path to the target resource as described aboveServletException
- java.io.IOException
- java.lang.IllegalArgumentException
- if target resource URL is unresolvablejava.lang.IllegalStateException
- if ServletResponse
is not in a state where a forward can be performedjava.lang.SecurityException
- if target resource cannot be accessed by callerpublic abstract voidinclude(java.lang.String relativeUrlPath) throwsServletException, java.io.IOException
Causes the resource specified to be processed as part of the current ServletRequest and ServletResponse being processed by the calling Thread. The output of the target resources processing of the request is written directly to the ServletResponse output stream.
The current JspWriter "out" for this JSP is flushed as a side-effect of this call, prior to processing the include.
If the relativeUrlPath begins with a "/" then the URL specified is calculated relative to the DOCROOT of the ServletContext
for this JSP. If the path does not begin with a "/" then the URL specified is calculated relative to the URL of the request that was mapped to the calling JSP.
It is only valid to call this method from a Thread
executing within a _jspService(...)
method of a JSP.
relativeUrlPath
- specifies the relative URL path to the target resource to be includedServletException
- java.io.IOException
- java.lang.IllegalArgumentException
- if the target resource URL is unresolvablejava.lang.SecurityException
- if target resource cannot be accessed by callerpublic abstract voidhandlePageException(java.lang.Exception e) throwsServletException, java.io.IOException
This method is intended to process an unhandled "page" level exception by redirecting the exception to either the specified error page for this JSP, or if none was specified, to perform some implementation dependent action.
A JSP implementation class shall typically clean up any local state prior to invoking this and will return immediately thereafter. It is illegal to generate any output to the client, or to modify any ServletResponse state after invoking this call.
This method is kept for backwards compatiblity reasons. Newly generated code should use PageContext.handlePageException(Throwable).
e
- the exception to be handledServletException
- java.io.IOException
- java.lang.NullPointerException
- if the exception is nulljava.lang.SecurityException
- if target resource cannot be accessed by callerhandlePageException(Throwable)
public abstract voidhandlePageException(java.lang.Throwable t) throwsServletException, java.io.IOException
This method is identical to the handlePageException(Exception), except that it accepts a Throwable. This is the preferred method to use as it allows proper implementation of the errorpage semantics.
This method is intended to process an unhandled "page" level exception by redirecting the exception to either the specified error page for this JSP, or if none was specified, to perform some implementation dependent action.
A JSP implementation class shall typically clean up any local state prior to invoking this and will return immediately thereafter. It is illegal to generate any output to the client, or to modify any ServletResponse state after invoking this call.
t
- the throwable to be handledServletException
- java.io.IOException
- java.lang.NullPointerException
- if the exception is nulljava.lang.SecurityException
- if target resource cannot be accessed by callerhandlePageException(Exception)
publicBodyContentpushBody()
publicJspWriterpopBody()