深入剖析jsp的工作原理

来源:互联网 发布:易安卓播放器源码 编辑:程序博客网 时间:2024/04/30 04:45

在前面的几篇博客中已经简单的说明了一下jsp,通过解读tomcat将jsp页面翻译成的java源文件,对jsp的运行原理有了一个新的认识。我们都知道,我们运行一个jsp文件,服务器会将此页面翻译成一个java文件,然后调用jdk中的javac命令进行编译为class文件,之后再通过jvm运行.class,最后返回html给客户端的。

好的,让我们来看下一个简单的jsp页面被翻译成java文件是什么样子的吧。

package com.yc.source;


/*
 * Generated by the Jasper component of Apache Tomcat
 * Version: Apache Tomcat/7.0.23
 * Generated at: 2015-04-23 15:26:18 UTC
 * Note: The last modified time of this file was set to
 *       the last modified time of the source file after
 *       generation to assist with modification tracking.
 */




import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
public final class index extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {
  private static final javax.servlet.jsp.JspFactory _jspxFactory =
          javax.servlet.jsp.JspFactory.getDefaultFactory();
  private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.tomcat.InstanceManager _jsp_instancemanager;
  public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
    return _jspx_dependants;
  }
  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
  }


  public void _jspDestroy() {
  }
  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {
    final javax.servlet.jsp.PageContext pageContext;
    javax.servlet.http.HttpSession session = null;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.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;
      out.write('\r');
      out.write('\n');
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
      out.write("\r\n");
      out.write("\r\n");
      out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
      out.write("<html>\r\n");
      out.write("  <head>\r\n");
      out.write("    <base href=\"");
      out.print(basePath);
      out.write("\">\r\n");
      out.write("    \r\n");
      out.write("    <title>My JSP 'index.jsp' starting page</title>\r\n");
      out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");
      out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");
      out.write("\t<meta http-equiv=\"expires\" content=\"0\">    \r\n");
      out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n");
      out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n");
      out.write("\t<!--\r\n");
      out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n");
      out.write("\t-->\r\n");
      out.write("  </head>\r\n");
      out.write("  \r\n");
      out.write("  <body>\r\n");
      out.write("    \tget<hr/>\r\n");
      out.write("    \t\r\n");
      out.write("    \t<a href=\"doLogin.jsp?uname=a\">登陆</a>\r\n");
      out.write("    \t<br/>\r\n");
      out.write("    \t\r\n");
      out.write("    \tpost<hr/>\r\n");
      out.write("    \t<form action=\"doLogin.jsp\" method=\"post\">\r\n");
      out.write("    \t\t<filedset>\r\n");
      out.write("    \t\t\t\t<label for=\"uname\">用户名:</label><input type=\"text\" name=\"uname\" id=\"uname\"> <br />\r\n");
      out.write("      \t\t</fieldset>\r\n");
      out.write("      \t\t<fieldset>\r\n");
      out.write("      \t\t\t<label for=\"password\">密码:</label><input type=\"password\" name=\"password\" id=\"password\" /><br />\r\n");
      out.write("      \t\t</fieldset>\r\n");
      out.write("    \t\t<input type=\"submit\" value=\"提交\" />\r\n");
      out.write("    \t</form>\r\n");
      out.write("  </body>\r\n");
      out.write("</html>\r\n");
    } catch (java.lang.Throwable t) {
      if (!(t instanceof javax.servlet.jsp.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实际上是被编译成一个java类继承了servlet的实现类,我们能得出访问jsp实际上就是访问servlet这个结论的,事实上,实际上,浏览器向服务器发出请求,不管是请求什么资源,其实都是在访问servlet的。那么既然这样,为什么需要jsp,其实和mvc架构有关,只能说是分工不同吧,这点我会在之后的博客详述。

好,从代码中我们看到了原有的jsp被翻译成index extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent,通过文档我们知道了HttpJspBase是继承自httpservlet类的,所以其具有了inti(),destory()等方法。接下来是

javax.servlet.jsp.JspFactory _jspxFactory= javax.servlet.jsp.JspFactory.getDefaultFactory();//获取容器实现的一个JspFactory对象的引用,查阅文档知道了JspFactory是javax.servlet.jsp包中定义的一个抽象类,其中就定义了2个静态的方法set/getDefaultFactory(),但是set方法是JSP容器(tomcat)在实例化该页面的时候就置入的了,所以可以直接通过get方法得到该工厂的实例的了。在代码中我们还能看到el的实例的创建等等。然后是里面最重要的一个方法jsp_service(),这个方法也是服务器自动调用的,我们在该方法中就得到很多有用的信息,首先是得到pageContext对象 pageContext = _jspxFactory.getPageContext(this, request, response,null, true, 8192, true);当时我觉得实例化工厂对象好像没什么作用,但看到此处的时候才知道错了,由这个方法我们可以看到,session,application都是通过pageContext调用的方法得到的,这时候知道了终于知道了为什么可以在html页面中直接使用了out那些内置对象了,到此页面的Servlet的环境已经设置完毕啦,接下来的代码都是对页面进行解析了。然后输出结果,虽然对于编写jsp应用,我们并不需要容器中的运行过程,但是作为一个IT人员,如果连原理都不知道的话就不一定能对其有很深的认识和理解吧


0 0
原创粉丝点击