• jsp的执行流程


    1.在tomcat的web.xml中, 配置了一个JspServlet, 访问路径是*.jsp. 所有以.jsp结尾的路径都归JspServlet负责. JspServlet帮忙将所有的jsp文件转换为java代码, java代码编译成class, 运行后信息就被输出到了浏览器.

    2. 当客户端访问*.jsp的路径时, 被JspServlet拦下来, 将jsp文件转换为java代码, 然后编译为class文件, 执行, 页面就看到效果了.具体代码见本节最后

    3.分析jsp的执行流程, 需要查看服务器的work目录, 当jsp被访问时, 会在当前目录下, 生成对应的java文件和class文件.

    访问路径:C:UsersAdministrator.IntelliJIdea2018.3system omcatTomcat_7_0_79_jspworkCatalinalocalhost1_hello_jsp_war_explodedorgapachejsp

    4. jsp的执行流程文字描述:

    > 客户端访问jsp资源: http://localhost:8080/项目/index.jsp

    > 被JspServlet匹配成功, 执行JspServlet

    > JspServlet被称之为JSP引擎,JspServlet会查找index.jsp资源文件, 将jsp文件翻译为java文件, 命名为index_jsp.java

    > index_jsp.java定义了一个类, 叫index_jsp, 继承了HttpJspBase类, HttpJspBase有继承了HttpServlet. 所以说, jsp本质上就是一个Servlet.

    > 在index_jsp.java中, 并没有直接定义service方法, 而是重写了父类中的_jspService方法, 这个方法会被父类中的service方法自动调用.

    > 其实_jspService方法中就是将jsp文件中所有的内容转换成了java输出, 结构是out.write();

    > index_jsp.java被编译为index_jsp.class, 运行后, 页面就看到效果了.

    5.常见疑问

    1.为什么称jsp转化的java代码为servlet,并没有直接继承HttpServlet啊?

    答:由代码可知,HttpJspBase继承了HttpServlet,而error500_jsp.java由继承了HttpJspBase,所以说error500_jsp.java是一个servlet

    2.error500_jsp.java中并没有定义service方法,那是如何进行调用的呢?

    答:由代码可知,error500_jsp.java的父类 (public abstract class HttpJspBase extends HttpServlet implements HttpJspPage)

    存在service,init,destroy等方法,还有一个抽象方法_jspService,无方法体,service方法无法调用本类的抽象方法_jspService,而

    其子类error500_jsp.java正好重写了父类的_jspService(),这就导致父类调用_jspService()方法时,会跑到子类error500_jsp.java中去调用该重写的方法。

    有关文件代码

    tomcat中的web.xml中关于jsp的设置

    当客户端访问*.jsp的路径时, 被JspServlet拦下来, 将jsp文件转换为java代码, 然后编译为class文件, 执行, 页面就看到效果了

    <servlet>
        <servlet-name>jsp</servlet-name>
        <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
        <init-param>
            <param-name>fork</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>xpoweredBy</param-name>
            <param-value>false</param-value>
        </init-param>
        <load-on-startup>3</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>jsp</servlet-name>
        <url-pattern>*.jsp</url-pattern>
        <url-pattern>*.jspx</url-pattern>
    </servlet-mapping>

    通过JspServlet将jsp转化成的servlet

    error500.jsp--->error500_jsp.java

    /*
     * Generated by the Jasper component of Apache Tomcat
     * Version: Apache Tomcat/7.0.79
     * Generated at: 2019-05-09 07:08:40 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.
     */
    package org.apache.jsp;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.jsp.*;
    
    public final class error500_jsp 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 volatile javax.el.ExpressionFactory _el_expressionfactory;
      private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager;
    
      public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
        return _jspx_dependants;
      }
    
      public javax.el.ExpressionFactory _jsp_getExpressionFactory() {
        if (_el_expressionfactory == null) {
          synchronized (this) {
            if (_el_expressionfactory == null) {
              _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
            }
          }
        }
        return _el_expressionfactory;
      }
    
      public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() {
        if (_jsp_instancemanager == null) {
          synchronized (this) {
            if (_jsp_instancemanager == null) {
              _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
            }
          }
        }
        return _jsp_instancemanager;
      }
    
      public void _jspInit() {
      }
    
      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");
          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("<h1>500</h1>");
        } catch (java.lang.Throwable t) {
          if (!(t instanceof javax.servlet.jsp.SkipPageException)){
            out = _jspx_out;
            if (out != null && out.getBufferSize() != 0)
              try {
                if (response.isCommitted()) {
                  out.flush();
                } else {
                  out.clearBuffer();
                }
              } catch (java.io.IOException e) {}
            if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
            else throw new ServletException(t);
          }
        } finally {
          _jspxFactory.releasePageContext(_jspx_page_context);
        }
      }
    }

    HttpJspBase的相关代码

    error500_jsp extends org.apache.jasper.runtime.HttpJspBase

    由代码可知,HttpJspBase继承了HttpServlet,而error500_jsp.java由继承了HttpJspBase,所以说error500_jsp.java是一个servlet

    package org.apache.jasper.runtime;
    
    import java.io.IOException;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.jsp.HttpJspPage;
    import org.apache.jasper.compiler.Localizer;
    
    public abstract class HttpJspBase extends HttpServlet
      implements HttpJspPage
    {
      private static final long serialVersionUID = 1L;
    
      public final void init(ServletConfig config)
        throws ServletException
      {
        super.init(config);
        jspInit();
        _jspInit();
      }
    
      public String getServletInfo()
      {
        return Localizer.getMessage("jsp.engine.info");
      }
    
      public final void destroy()
      {
        jspDestroy();
        _jspDestroy();
      }
    
      public final void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
      {
        _jspService(request, response);
      }
    
      public void jspInit()
      {
      }
    
      public void _jspInit()
      {
      }
    
      public void jspDestroy()
      {
      }
    
      protected void _jspDestroy()
      {
      }
    
      public abstract void _jspService(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
        throws ServletException, IOException;
    }
  • 相关阅读:
    2017年3月10号课堂笔记
    2017年3月8号课堂笔记
    2017年3月6号课堂笔记
    2017年3月3号课堂笔记
    第7讲:设计PE型病毒2
    第6讲:设计PE型病毒1
    第5讲:HOOK 任务管理器 第2种方法注入
    第4讲:HOOK 任务管理器 无法结束进程
    第3讲:导入表的定位和读取操作
    第2讲:搜索PEB结构获取kernel32.dll的基址暴力搜索内存空间获得 Api 的线性地址
  • 原文地址:https://www.cnblogs.com/chenxiaoge/p/13335474.html
Copyright © 2020-2023  润新知