深入分析JSP

首先,面试的时候经常会被问到JSP和Servlet有什么关系?

从表面上看,JSP处理页面展示,Servlet处理页面跳转;更进一步就是有些面试宝典上说得:JSP编译后是类Servlet文件

那么到底两者是什么关系?见下面分析

一个简单的JSP文件HelloWorld.jsp

<%@ page buffer="16kb" %>
<%! int a = 0; %>
<%
 String message = "Hello World!";
 out.print(a);
%>
<%=message%>
 
将它拷贝到Tomcat下\webapps\ROOT下
经过Tomcat编译后看一下文件夹在我本地是\apache-tomcat-6.0.14\work\Catalina\localhost\_\org\apache\jsp,
你会发现有个HelloWorld_jsp.java,内容如下:
package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

public final class HelloWorld_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

 int a = 0; 
  private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();

  private static java.util.List _jspx_dependants;

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.AnnotationProcessor _jsp_annotationprocessor;

  public Object getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
  }

  public void _jspDestroy() {
  }

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

      out.write('\r');
      out.write('\n');
      out.write('\r');
      out.write('\n');

 String message = "Hello World!";
 out.print(a);

      out.write('\r');
      out.write('\n');
      out.print(message);
      out.write('\r');
      out.write('\n');
    } 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);
    }
  }
}
  发现这个类的方法_jspInit()、_ jspDestroy()、_jspService()和Servlet中的init()、destory()、service()非常像,其实这个文件就是一个servlet。
  下面说明几点:
  1)这个类extends org.apache.jasper.runtime.HttpJspBase,而HttpJspBase extends javax.servlet.http.HttpServlet
  2)我们知道JSP的内置对象可以在页面上直接用,原理何在?看一下上面的_jspService()方法中的变量声明 包含6个对象除了request、reponse和exception,并进行了初始化
  3)可以看到_jspService()处理主要的页面输出,对比JSP的代码
  4)在JSP中我们设定了缓存<%@ page buffer="16kb" %> 在HelloWorld_jsp.java中体现在pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 16384, true);
  

相关推荐