zoukankan      html  css  js  c++  java
  • JSP的工作原理

    新建一个JSP页面,命名为index.jsp,代码编辑如下

    <%@page import="java.util.Date"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <%
            Date date = new Date();
        %>
        <h1>现在的时间是<%=date%></h1>
    </body>
    </html>

    1、第一次运行JSP页面时,JSP引擎会将JSP页面转化为index_jsp.java文件,文件代码如下所示

    /*
     * Generated by the Jasper component of Apache Tomcat
     * Version: Apache Tomcat/7.0.104
     * Generated at: 2020-07-30 10:48:29 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.*;
    import java.util.Date;
    
    public final class index_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; 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("
    ");
          out.write("
    ");
          out.write("<!DOCTYPE html>
    ");
          out.write("<html>
    ");
          out.write("<head>
    ");
          out.write("<meta charset="UTF-8">
    ");
          out.write("<title>Insert title here</title>
    ");
          out.write("</head>
    ");
          out.write("<body>
    ");
          out.write("	");
    
            Date date = new Date();
        
          out.write("
    ");
          out.write("	<h1>现在的时间是");
          out.print(date);
          out.write("</h1>
    ");
          out.write("</body>
    ");
          out.write("</html>");
        } 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);
        }
      }
    }

    2、该Java文件本质上是一个Servlet,每当用户发送向index.jsp页面发送请求时,就会调用_jspService方法处理请求。如果不是第一次请求,JSP引擎不会重复将JSP页面翻译成Java文件

    3、Java虚拟机将Java文件编译成字节码文件

     4、在Jsp页面中的HTML代码,在翻译成Java文件后,使用out.write语句进行输出。对于<% %>之间的脚本片段,直接翻译成Java代码;对于<%= %>之间的Jsp表达式,使用out.print语句进行输出。

  • 相关阅读:
    微信redirect_uri 回调错误,scope权限错误
    对“空间数据库”的理解
    空间数据库2
    PostgreSQL和MySQL
    shp文件和地理数据库文件的区别
    分布式 空间数据库
    Git使用教程
    栅格投影
    mapnik渲染原理
    高斯消元——浮点数模板
  • 原文地址:https://www.cnblogs.com/yanchaoyi/p/13411324.html
Copyright © 2011-2022 走看看