• Jsp和Servlet关系


    为什么会出现Jsp?

    其实对于服务器来说它只认识Servlet,我们完全可以在Servlet用resp.getWriter().write("");画出网页的界面,但是仅仅一个很简单的界面就要重复的书写resp.getWriter().write(""),并且这还没有加上js、css以及jquery。

    但是我们在Servlet写的是java代码可以有逻辑的判断,但是在html、css中无法加入逻辑导致页面是静态的。为了解决这一问题出现了jsp,在jsp中可以书写Java代码、js代码、html代码等。

    我们都知道浏览器发送请求到服务器,服务器通过解析其中Servlet的别名找到对应的Servlet并执行其中的service方法做出处理和响应,那么对于服务器来说它只认识Servlet,那么我们编辑的Jsp到底是如何被服务器识别的?

    实际上服务器还是将.jsp当作Servlet的别名在你局部配置下的web.xml找有没有对应的Servlet,如果没有找到对应的Servlet就去Tomcat的公共配置下的web.xml(Tomcat安装目录conf下的web.xml)找对应的Servlet。

    会找到如下内容:

    org.apache.jasper.servlet.JspServlet将对应的jsp文件转为Servlet。

    那么转的Servlet在什么地方呢?打开Tomcat下的work文件夹一直点到:找你正在运行的项目的jsp

    测试的jsp:

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    666
    </body>
    </html> 

    被编译好的Java文件:

    /*
     * Generated by the Jasper component of Apache Tomcat
     * Version: Apache Tomcat/8.5.46
     * Generated at: 2019-10-16 09:05:10 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 index_jsp extends org.apache.jasper.runtime.HttpJspBase
        implements org.apache.jasper.runtime.JspSourceDependent,
                     org.apache.jasper.runtime.JspSourceImports {
    
      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 static final java.util.Set<java.lang.String> _jspx_imports_packages;
    
      private static final java.util.Set<java.lang.String> _jspx_imports_classes;
    
      static {
        _jspx_imports_packages = new java.util.HashSet<>();
        _jspx_imports_packages.add("javax.servlet");
        _jspx_imports_packages.add("javax.servlet.http");
        _jspx_imports_packages.add("javax.servlet.jsp");
        _jspx_imports_classes = null;
      }
    
      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 java.util.Set<java.lang.String> getPackageImports() {
        return _jspx_imports_packages;
      }
    
      public java.util.Set<java.lang.String> getClassImports() {
        return _jspx_imports_classes;
      }
    
      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 java.lang.String _jspx_method = request.getMethod();
        if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
          response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD");
          return;
        }
    
        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=ISO-8859-1");
          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("<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    ");
          out.write("<html>
    ");
          out.write("<head>
    ");
          out.write("<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    ");
          out.write("<title>Insert title here</title>
    ");
          out.write("</head>
    ");
          out.write("<body>
    ");
          out.write("666
    ");
          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);
        }
      }
    }

      

    但是我们发现这个类并没有继承HttpServlet而是继承了org.apache.jasper.runtime.HttpJspBase并且里面也没有service方法而是_jspservice方法。那么Tomcat就会到它的父类中找service方法并进行调用。

    实际上它的父类org.apache.jasper.runtime.HttpJspBase继承了HttpServlet。

     父类中Service方法就是调用了子类的_jspservice方法(所以说实际上Tomcat执行的就是_jspservice方法)

    总结:实际上jsp的本质还是servlet,只不过是为了方面花界面所单独拉出来的一部分而已,到最终的实现还是会回到Servlet当中。

    展望:jsp已经是一门比较落后的技术来,因为html、css、js是前端写的,而后端要在其中加入逻辑判断,那么首先你一定得认识前端语言的代码,否则你如何嵌套?这样很不利于前后端的分离。之所以学习是因为很多框架和项目中依然会用到它,但是最终还是会被时代所淘汰。

    不一样的烟火
  • 相关阅读:
    code review
    设计原则
    知识点介绍
    REST API
    第四章 模块化React和Redux应用
    第3章 从Flux到Redux
    第二章 设计高质量的React组件
    React和Jquery比较
    第一章 React新的前端思维方式
    封装一个获取module.exports内容的方法
  • 原文地址:https://www.cnblogs.com/cstdio1/p/11687114.html
Copyright © 2020-2023  润新知