PageContext (Java EE 5)

Java EE API


javax.servlet.jsp Class PageContext

java.lang.Object
  extended by javax.servlet.jsp.JspContext
      extended by javax.servlet.jsp.PageContext

public abstract class PageContext
extends JspContext

Extends: JspContext

PageContext 扩展了 JspContext,提供有关何时在 Servlet 环境中使用 JSP 技术的有用上下文信息。

PageContext 实例提供对与某个 JSP 页面关联的所有名称空间的访问,也提供对几个页面属性以及实现细节之上的某个层的访问。隐式对象是自动添加到 pageContext 的。

PageContext 类是一个抽象类,该类设计用于扩展,以便由相应 JSP 引擎运行时环境提供该类的与实现相关的实现。PageContext 实例由 JSP 实现类通过调用 JspFactory.getPageContext() 方法获得,该实例可通过调用 JspFactory.releasePageContext() 得到释放。

在其他地方给出了如何在 JSP Page Implementation 对象中使用 PageContext、JspFactory 和其他类的示例。

PageContext 为页面/组件作者和页面实现者提供了许多工具,其中包括:

  • 管理各种范围名称空间的单个 API
  • 许多便捷 API,用于访问各种公共对象
  • 获取 JspWriter 以供输出的机制
  • 通过页面管理会话使用的机制
  • 向脚本环境公开页面指令属性的机制
  • 将当前请求转发或包含到应用程序中其他活动组件的机制
  • 处理 errorpage 异常处理的机制

供容器生成的代码使用的方法

一些方法应该由容器生成的代码使用,而不应该由 JSP 页面作者或 JSP 标记库作者编写的代码使用。

支持生命周期的方法是 initialize()release()

以下方法使嵌套管理的 JspWriter 流能够实现标记扩展:pushBody()

供 JSP 作者使用的方法

以下方法提供对隐式对象的便捷访问getException()getPage()getRequest()getResponse()getSession()getServletConfig()getServletContext()

以下方法提供对转发、包含和错误处理的支持:forward()include()handlePageException()

英文文档:

PageContext extends JspContext to provide useful context information for when JSP technology is used in a Servlet environment.

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.

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:

  • 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

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 supporting lifecycle are initialize() and release()

The following methods enable the management of nested JspWriter streams to implement Tag Extensions: pushBody()

Methods Intended for JSP authors

The following methods provide convenient access to implicit objects: getException(), getPage() getRequest(), getResponse(), getSession(), getServletConfig() and getServletContext().

The following methods provide support for forwarding, inclusion and error handling: forward(), include(), and handlePageException().


Field Summary
static String
static int
static String
static String
static String
static String
static int
static String
static String
static int
static String
static String
static int
 
Constructor Summary
 
Method Summary
abstract  void
 ErrorData
abstract  Exception
abstract  Object
abstract  ServletRequest
abstract  ServletResponse
abstract  ServletConfig
abstract  ServletContext
abstract  HttpSession
abstract  void
abstract  void
abstract  void
abstract  void
abstract  void
 BodyContent
abstract  void
 
Methods inherited from class javax.servlet.jsp.JspContext
 
Methods inherited from class java.lang.Object
 

Field Detail

英文文档:

PAGE_SCOPE

public static final int 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.

See Also:
Constant Field Values


英文文档:

REQUEST_SCOPE

public static final int REQUEST_SCOPE
Request scope: the named reference remains available from the ServletRequest associated with the Servlet until the current request is completed.

See Also:
Constant Field Values


英文文档:

SESSION_SCOPE

public static final int 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.

See Also:
Constant Field Values


英文文档:

APPLICATION_SCOPE

public static final int APPLICATION_SCOPE
Application scope: named reference remains available in the ServletContext until it is reclaimed.

See Also:
Constant Field Values


英文文档:

PAGE

public static final String PAGE
Name used to store the Servlet in this PageContext's nametables.

See Also:
Constant Field Values


英文文档:

PAGECONTEXT

public static final String PAGECONTEXT
Name used to store this PageContext in it's own name table.

See Also:
Constant Field Values


英文文档:

REQUEST

public static final String REQUEST
Name used to store ServletRequest in PageContext name table.

See Also:
Constant Field Values


英文文档:

RESPONSE

public static final String RESPONSE
Name used to store ServletResponse in PageContext name table.

See Also:
Constant Field Values


英文文档:

CONFIG

public static final String CONFIG
Name used to store ServletConfig in PageContext name table.

See Also:
Constant Field Values


英文文档:

SESSION

public static final String SESSION
Name used to store HttpSession in PageContext name table.

See Also:
Constant Field Values


英文文档:

OUT

public static final String OUT
Name used to store current JspWriter in PageContext name table.

See Also:
Constant Field Values


英文文档:

APPLICATION

public static final String APPLICATION
Name used to store ServletContext in PageContext name table.

See Also:
Constant Field Values


英文文档:

EXCEPTION

public static final String EXCEPTION
Name used to store uncaught exception in ServletRequest attribute list and PageContext name table.

See Also:
Constant Field Values

Constructor Detail

public PageContext()
唯一的构造方法。(由子类构造方法调用,通常是隐式的)。
英文文档:

PageContext

public PageContext()
Sole constructor. (For invocation by subclass constructors, typically implicit.)

Method Detail

abstract public void initialize(Servlet servlet, ServletRequest request, ServletResponse response, String errorPageURL, boolean needsSession, int bufferSize, boolean autoFlush) throws java.io.IOException, IllegalStateException, IllegalArgumentException

调用 initialize 方法初始化未被初始化的 PageContext,这样 JSP 实现类就可以使用它为其 _jspService() 方法中的传入请求和响应提供服务。

此方法通常从 JspFactory.getPageContext() 调用,以便初始化状态。

此方法必须创建一个初始 JspWriter,并将页面范围中的 "out" 名称与新创建的对象相关联。

页面或标记库作者不应该使用此方法。

servlet 与此 PageContext 关联的 Servlet
request 此 Servlet 当前挂起的请求
response 此 Servlet 当前挂起的响应
errorPageURL 页面指令中 errorpage 属性的值,或者为 null
needsSession 页面指令中 session 属性的值
bufferSize 页面指令中 buffer 属性的值
autoFlush 页面指令中 autoflush 属性的值
Throwsjava.io.IOException: 在创建 JspWriter 期间
ThrowsIllegalStateException: 如果未正确初始化 out
ThrowsIllegalArgumentException: 如果某个给定参数无效

英文文档:

initialize

public abstract void initialize(Servlet servlet,
                                ServletRequest request,
                                ServletResponse response,
                                String errorPageURL,
                                boolean needsSession,
                                int bufferSize,
                                boolean autoFlush)
                         throws IOException,
                                IllegalStateException,
                                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.

Parameters:
servlet - The Servlet that is associated with this PageContext
request - The currently pending request for this Servlet
response - The currently pending response for this Servlet
errorPageURL - The value of the errorpage attribute from the page directive or null
needsSession - The value of the session attribute from the page directive
bufferSize - The value of the buffer attribute from the page directive
autoFlush - The value of the autoflush attribute from the page directive
Throws:
IOException - during creation of JspWriter
IllegalStateException - if out not correctly initialized
IllegalArgumentException - If one of the given parameters is invalid

abstract public void release()

此方法将“重置”PageContext 的内部状态,释放所有内部引用,并为以后 initialize() 调用中可能的重用准备 PageContext。此方法通常从 JspFactory.releasePageContext() 调用。

子类将封装此方法。

页面或标记库作者不应该使用此方法。

英文文档:

release

public abstract void 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(). 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.


abstract public HttpSession getSession()
会话对象的当前值(一个 HttpSession)。
return 此 PageContext 的 HttpSession,或者返回 null
英文文档:

getSession

public abstract HttpSession getSession()
The current value of the session object (an HttpSession).

Returns:
the HttpSession for this PageContext or null

abstract public Object getPage()
页面对象的当前值(在 Servlet 环境中,这是一个 javax.servlet.Servlet 实例)。
return 与此 PageContext 关联的页面实现类实例
英文文档:

getPage

public abstract Object getPage()
The current value of the page object (In a Servlet environment, this is an instance of javax.servlet.Servlet).

Returns:
the Page implementation class instance associated with this PageContext

abstract public ServletRequest getRequest()
请求对象的当前值(一个 ServletRequest)。
return 此 PageContext 的 ServletRequest
英文文档:

getRequest

public abstract ServletRequest getRequest()
The current value of the request object (a ServletRequest).

Returns:
The ServletRequest for this PageContext

abstract public ServletResponse getResponse()
响应对象的当前值(一个 ServletResponse)。
return 此 PageContext 的 ServletResponse
英文文档:

getResponse

public abstract ServletResponse getResponse()
The current value of the response object (a ServletResponse).

Returns:
the ServletResponse for this PageContext

abstract public Exception getException()
异常对象的当前值(一个 Exception)。
return 以 errorpage 的形式传递到此 PageContext 的任何异常
英文文档:

getException

public abstract Exception getException()
The current value of the exception object (an Exception).

Returns:
any exception passed to this as an errorpage

abstract public ServletConfig getServletConfig()
ServletConfig 实例。
return 此 PageContext 的 ServletConfig
英文文档:

getServletConfig

public abstract ServletConfig getServletConfig()
The ServletConfig instance.

Returns:
the ServletConfig for this PageContext

abstract public ServletContext getServletContext()
ServletContext 实例。
return 此 PageContext 的 ServletContext
英文文档:

getServletContext

public abstract ServletContext getServletContext()
The ServletContext instance.

Returns:
the ServletContext for this PageContext

abstract public void forward(String relativeUrlPath) throws ServletException, java.io.IOException

此方法用于将当前 ServletRequest 和 ServletResponse 重定向或“转发”到应用程序中的另一个活动组件。

如果 relativeUrlPath 以 "/" 开始,那么指定的 URL 将相对于此 JSP ServletContext 的 DOCROOT 进行计算。如果路径不以 "/" 开始,那么指定的 URL 将相对于映射到调用 JSP 的请求的 URL 进行计算。

只有通过在 JSP 的 _jspService(...) 方法中执行的 Thread 调用此方法才是有效的。

在成功调用此方法后,调用 Thread 试图修改 ServletResponse 对象是不合法的。任何这样的尝试都将导致不确定的行为。通常,调用者在调用此方法后将立刻从 _jspService(...) 返回。

relativeUrlPath 指定到目标资源的相对 URL 路径(如上所述)
ThrowsIllegalStateException: 如果 ServletResponse 并不处于可以执行转发的状态
ThrowsServletException: 如果转发到的页面抛出 ServletException
Throwsjava.io.IOException: 如果在转发时发生 I/O 错误
英文文档:

forward

public abstract void forward(String relativeUrlPath)
                      throws ServletException,
                             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.

Parameters:
relativeUrlPath - specifies the relative URL path to the target resource as described above
Throws:
IllegalStateException - if ServletResponse is not in a state where a forward can be performed
ServletException - if the page that was forwarded to throws a ServletException
IOException - if an I/O error occurred while forwarding

abstract public void include(String relativeUrlPath) throws ServletException, java.io.IOException

导致将指定资源作为正由调用 Thread 处理的当前 ServletRequest 和 ServletResponse 的一部分进行处理。将请求的目标资源处理的输出直接写入 ServletResponse 输出流。

在处理 include 之前,作为此调用的副作用,将刷新此 JSP 的当前 JspWriter "out"。

如果 relativeUrlPath 以 "/" 开始,那么指定的 URL 将相对于此 JSP ServletContext 的 DOCROOT 进行计算。如果路径不以 "/" 开始,那么指定的 URL 将相对于映射到调用 JSP 的请求的 URL 进行计算。

只有通过在 JSP 的 _jspService(...) 方法中执行的 Thread 调用此方法才是有效的。

relativeUrlPath 指定到要包含的目标资源的相对 URL 路径
ThrowsServletException: 如果转发到的页面抛出 ServletException
Throwsjava.io.IOException: 如果在转发时发生 I/O 错误
英文文档:

include

public abstract void include(String relativeUrlPath)
                      throws ServletException,
                             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.

Parameters:
relativeUrlPath - specifies the relative URL path to the target resource to be included
Throws:
ServletException - if the page that was forwarded to throws a ServletException
IOException - if an I/O error occurred while forwarding

abstract public void include(String relativeUrlPath, boolean flush) throws ServletException, java.io.IOException

导致将指定资源作为正由调用 Thread 处理的当前 ServletRequest 和 ServletResponse 的一部分进行处理。将请求的目标资源处理的输出直接写入调用 getOut() 返回的当前 JspWriter。

如果 flush 为 true,那么在处理 include 之前,作为此调用的副作用,将刷新此 JSP 的当前 JspWriter "out"。否则,不刷新 JspWriter "out"。

如果 relativeUrlPath 以 "/" 开始,那么指定的 URL 将相对于此 JSP ServletContext 的 DOCROOT 进行计算。如果路径不以 "/" 开始,那么指定的 URL 将相对于映射到调用 JSP 的请求的 URL 进行计算。

只有通过在 JSP 的 _jspService(...) 方法中执行的 Thread 调用此方法才是有效的。

relativeUrlPath 指定到要包含的目标资源的相对 URL 路径
flush 如果在处理 include 之前要刷新 JspWriter,则该参数为 true;如果不刷新 JspWriter,则该参数为 false。
ThrowsServletException: 如果转发到的页面抛出 ServletException
Throwsjava.io.IOException: 如果在转发时发生 I/O 错误
since2.0
英文文档:

include

public abstract void include(String relativeUrlPath,
                             boolean flush)
                      throws ServletException,
                             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 current JspWriter returned by a call to getOut().

If flush is true, The current JspWriter "out" for this JSP is flushed as a side-effect of this call, prior to processing the include. Otherwise, the JspWriter "out" is not flushed.

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.

Parameters:
relativeUrlPath - specifies the relative URL path to the target resource to be included
flush - True if the JspWriter is to be flushed before the include, or false if not.
Throws:
ServletException - if the page that was forwarded to throws a ServletException
IOException - if an I/O error occurred while forwarding
Since:
JSP 2.0

abstract public void handlePageException(Exception e) throws ServletException, java.io.IOException

此方法旨在通过将异常转发到此 JSP 的指定错误页面处理未处理的“页面”级异常。如果无法转发(比如因为已经提交了响应),则应该使用与实现相关的机制调用错误页面(例如,“包括”错误页面)。

如果页面中没有定义错误页面,则应该重新抛出异常,以便标准的 servlet 错误处理接管。

在调用此方法前,JSP 实现类通常应该清除本地状态,并在调用此方法后立刻返回。向客户端生成任何输出,或者调用此方法后修改任何 ServletResponse 状态都是非法的。

保留此方法只是为了向后兼容。新生成的代码应该使用 PageContext.handlePageException(Throwable)。

e 要处理的异常
ThrowsServletException: 如果调用错误页面时发生错误
Throwsjava.io.IOException: 如果调用错误页面时发生 I/O 错误
ThrowsNullPointerException: 如果异常为 null
See also handlePageException(Throwable)

英文文档:

handlePageException

public abstract void handlePageException(Exception e)
                                  throws ServletException,
                                         IOException

This method is intended to process an unhandled 'page' level exception by forwarding the exception to the specified error page for this JSP. If forwarding is not possible (for example because the response has already been committed), an implementation dependent mechanism should be used to invoke the error page (e.g. "including" the error page instead).

If no error page is defined in the page, the exception should be rethrown so that the standard servlet error handling takes over.

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).

Parameters:
e - the exception to be handled
Throws:
ServletException - if an error occurs while invoking the error page
IOException - if an I/O error occurred while invoking the error page
NullPointerException - if the exception is null
See Also:
handlePageException(Throwable)

abstract public void handlePageException(Throwable t) throws ServletException, java.io.IOException

此方法旨在通过将异常转发到此 JSP 的指定错误页面处理未处理的“页面”级异常。如果无法转发(比如因为已经提交了响应),则应该使用与实现相关的机制调用错误页面(例如,“包括”错误页面)。

如果页面中没有定义错误页面,则应该重新抛出异常,以便标准的 servlet 错误处理接管。

此方法旨在通过将异常重定向到此 JSP 的指定错误页面(如果未指定该页面,则执行一些与实现相关的操作)处理未处理的“页面”级异常。

在调用此方法前,JSP 实现类通常应该清除本地状态,并在调用此方法后立刻返回。向客户端生成任何输出,或者调用此方法后修改任何 ServletResponse 状态都是非法的。

t 要处理的 throwable
ThrowsServletException: 如果调用错误页面时发生错误
Throwsjava.io.IOException: 如果调用错误页面时发生 I/O 错误
ThrowsNullPointerException: 如果异常为 null
See also handlePageException(Exception)

英文文档:

handlePageException

public abstract void handlePageException(Throwable t)
                                  throws ServletException,
                                         IOException

This method is intended to process an unhandled 'page' level exception by forwarding the exception to the specified error page for this JSP. If forwarding is not possible (for example because the response has already been committed), an implementation dependent mechanism should be used to invoke the error page (e.g. "including" the error page instead).

If no error page is defined in the page, the exception should be rethrown so that the standard servlet error handling takes over.

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.

Parameters:
t - the throwable to be handled
Throws:
ServletException - if an error occurs while invoking the error page
IOException - if an I/O error occurred while invoking the error page
NullPointerException - if the exception is null
See Also:
handlePageException(Exception)

public BodyContent pushBody()
返回一个新的 BodyContent 对象,保存当前 "out" JspWriter,并更新 PageContext 的页面范围属性名称空间中的 "out" 属性的值。
return 新的 BodyContent
英文文档:

pushBody

public BodyContent 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.

Returns:
the new BodyContent

public ErrorData getErrorData()
提供对错误信息的便捷访问。
return 包含有关错误的信息的 ErrorData 实例,这些信息是根据 Servlet 规范从请求属性中获得的。如果没有错误页面(也就是说,如果页面指令的 isErrorPage 属性没有设置为 "true"),则该信息是无意义的。
since2.0
英文文档:

getErrorData

public ErrorData getErrorData()
Provides convenient access to error information.

Returns:
an ErrorData instance containing information about the error, as obtained from the request attributes, as per the Servlet specification. If this is not an error page (that is, if the isErrorPage attribute of the page directive is not set to "true"), the information is meaningless.
Since:
JSP 2.0


Submit a bug or feature

Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.

一看就知道只有菜鸟才干这么无知的事啦。

PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!