Peter is correct.PageContext
供应处理页面的范围。消费者不应该在这个范围之外引用这些实例,这隐含地意味着实例不应该在当前线程之外访问。从JSP 2.2 specification
例JSP处理代码:
public class foo implements Servlet {
// ...
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
JspFactory factory = JspFactory.getDefaultFactory();
PageContext pageContext = factory.getPageContext(
this,
request,
response,
null, // errorPageURL
false, // needsSession
JspWriter.DEFAULT_BUFFER,
true // autoFlush
);
// initialize implicit variables for scripting env ...
HttpSession session = pageContext.getSession();
JspWriter out = pageContext.getOut();
Object page = this;
try {
// body of translated JSP here ...
} catch (Exception e) {
out.clear();
pageContext.handlePageException(e);
} finally {
out.close();
factory.releasePageContext(pageContext);
}
}
的PageContext
实例是如何配置的(从池或实例创建)是容器的实现细节。
这是很好的知道,但那么为什么PageContext有生命周期方法,如果它不是回收它们? – murungu 2012-03-06 10:02:55
@murungu:容器可能会使用页面上下文对象池。容器调用这些方法来指示页面上下文使用的开始和结束。页面上下文应该进行必要的初始化或清理。这些方法并不是由JSP页面作者调用的。 – 2012-03-06 10:07:40