您的位置:首页 > Web前端 > JavaScript

保存JSP页面状态的4个内置对象

2017-01-12 17:15 330 查看
我们必须清楚,真正在Java Web服务器内运行的是用Java语言实现的Servlet类,程序员编写的JSP页面只是Web服务器生成一个Servlet类的“材料”。根据JSP页面生成Servlet类,然后由Servlet提供HTTP服务。

当我们把Web项目部署到Tomcat服务器上,而且创建了一个JSP页面,就会在Tomcat的work \Canalina\localhost\项目名称\org\apache\jsp路径下,找到JSP页面生成的Servlet类文件。在文件中可以看到如下部分代码:

//用于用户请求的方法
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {

PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
response.setContentType("text/html;charset=utf-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
//以下是对JSP页面内容的处理
out.write("\r\n");
out.write("\r\n");
out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n");
out.write("<html xmlns


从Servlet类代码中可以看出,Web服务器执行Servlet类时,因为先定义了8个对象(JSP页面的Java脚本中可以使用的是9个内置对象,只有当页面的page指令的isErrorPage属性为true时,才可以使用exception对象),然后再处理JSP页面内容,所以这9个对象在执行JSP页面的Java脚本时都可以使用。其中,有4个对象是用于保存JSP页面状态的。

pageContext对象根据当前用户的请求和服务器的响应,保存当前项目在服务器中所处的环境(上下文),这个环境是项目中的各个页面共享的。

PageContext pageContext = null;

pageContext = _jspxFactory.getPageContext(this, request, response,null, true, 8192, true);

application对象根据pageContext所代表的环境,保存当前项目(一个Web应用)中的一些公用的数据,这些数据可以被该项目中的所有JSP页面共享。

ServletContext application = null;

application = pageContext.getServletContext();

onfig对象根据pageContext所代表的环境,保存从web.xml文件中读取的环境设置和服务器配置信息。

ServletConfig config = null;

config = pageContext.getServletConfig();

session对象也是根据pageContext所代表的环境,保存一个各户端在该项目中的一个活动(会话)的信息。

HttpSession session = null;

session = pageContext.getSession();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息