JSP脚本中的九大内置对象详解(上)

来源:互联网 发布:苹果笔记本装mac系统 编辑:程序博客网 时间:2024/06/07 20:56

原理:我们都知道,jsp在第一次被请求时会被编译成servlet,实际上是由该servlet来处理用户请求。

了解了上述内容,我们再来说说九大内置对象的实质。它们都是Servlet API中接口的实例,在jsp编译成servlet时进行了默认初始化,所以我们才能在jsp脚本中直接使用它们。

直接上代码:

  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=GBK");
      pageContext = _jspxFactory.getPageContext(this, request, response,
         "error.jsp", true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\r\n");
      out.write("\r\n");
      out.write("<!doctype html>\r\n");
      out.write("<html>\r\n");
      out.write("\t<head>\r\n");
      out.write("\t\t<title>webDEMO欢迎您</title>\r\n");
      out.write("\t</head>\r\n");
      out.write("\t<body>\r\n");
      out.write("\t\t");
      out.write("\r\n");
      out.write("\t\t");
      out.write("\r\n");
      out.write("\t\t<!-- html注释 -->\r\n");
      out.write("\t\t<p>webDEMO欢迎您");
new Date();//从这里可以看出,jsp脚本中的内容会编译进该处理方法中
      out.write("<br/>");
      out.print((++count) );
      out.write("<br/>");
      out.print(info() );
      out.write("<br/>");
      out.print(getServletInfo() );
      out.write("</p>\r\n");
      out.write("\t\t");

   String name = config.getInitParameter("name");//int a=1/0;
  
      out.write("\r\n");
      out.write("\t\t");
      out.print(name );
      out.write("\r\n");
      out.write("\t</body>\r\n");
      out.write("</html>");
    } catch (Throwable t) {
      if (!(t instanceof SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try { out.clearBuffer(); } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }

每一个jsp编译成的servlet中都有这一段,看红色标注部分,应该能理解为啥能在jsp脚本中直接使用这九大内置对象了吧。还有一点需要注意,这里只初始化了八个内置对象,对于exception,当我们在jsp中指定其isErrorPage=“true”时,才能看到其初始化的代码。

 

0 0
原创粉丝点击