• JSP系列教材 (二)- 转译为Servlet


     

    为什么JSP可以在html中运行java代码? 这是因为JSP被转译成了Servlet

    步骤1:执行过程
    步骤2:hello_jsp.java 为什么是Servlet

    步骤 1 : 执行过程

    1. 把 hello.jsp转译为hello_jsp.java
    2. hello_jsp.java 位于
    d: omcatworkCatalinalocalhost\_orgapachejsp
    3. hello_jsp.java是一个servlet
    4. 把hello_jsp.java 编译为hello_jsp.class
    5. 执行hello_jsp,生成html
    6. 通过http协议把html 响应返回给浏览器

    执行过程

    步骤 2 : hello_jsp.java 为什么是Servlet

    Servlet章节中,我们说HelloServlet是一个Servlet,不是因为它的类名里有一个"Servlet",而是因为它继承了 HttpServlet

    打开转译hello.jsp 后得到的hello_jsp.java,可以发现它继承了类
    (D: omcatworkCatalinalocalhost\_orgapachejsphello_jsp.java)

    org.apache.jasper.runtime.HttpJspBase


    而HttpJspBase 继承了HttpServlet

    所以我们说hello_.jsp.java 是一个Servlet

    /*

     * Generated by the Jasper component of Apache Tomcat

     * Version: Apache Tomcat/7.0.64

     * Generated at: 2017-01-07 01:45:17 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.*;

    public final class hello_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 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, finaljavax.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,

                    nulltrue8192true);

          _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("你好 JSP ");

          out.write(" ");

          out.write("<br> ");

          out.write(" ");

          out.print(new Date().toLocaleString());

        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);

        }

      }

    }


    更多内容,点击了解: https://how2j.cn/k/jsp/jsp-transfer/574.html

  • 相关阅读:
    初学mongodb笔记
    git学习【转载】
    json序列化与反序列化
    ES6函数比对ES5函数
    前端js脚本与防止js脚本
    js函数整合队列顺序执行插件
    padding-使用必记
    css小技巧
    三分钟教会你开密码箱
    百度地图Marker优化方案
  • 原文地址:https://www.cnblogs.com/Lanht/p/12615392.html
Copyright © 2020-2023  润新知