为什么说JSP就是Servlet

Copyright©Zi10ng

Jsp和Servlet是学习JavaEE绕不过的弯,那么为什么Jsp就是Servlet呢?

原因

平常在测试代码的时候,我们可能在servlet的派生类中写上这么一行代码:

response.getWriter().write("

测试成功

");

这样字符串中的html语句就会通过servlet输出到浏览器的页面上。

早期的程序员非常苦逼,他们不像我们做测试输出这样一两行代码,而是需要把整个html页面通过servlet输出到浏览器中,这无疑加重了程序员的负担。

所以此时Jsp横空出世,把需要展现成html的页面全部封装起来,减轻我们的负担。

证据

不妨打开CATALINA_BASE的目录,我的是C:\Users\xxx\.IntelliJIdea2018.2\system\tomcat\Tomcat_9_0_17_Servlet4,然后找到work\Catalina\localhost\heima_servlet\org\apache\jsp,就会发现有两个文件,一个叫index_jsp.java,另外一个是index_jsp.class
对比一下index.jsp和index_jsp.java:


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 {

    if (!javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
      final java.lang.String _jspx_method = request.getMethod();
      if ("OPTIONS".equals(_jspx_method)) {
        response.setHeader("Allow","GET, HEAD, POST, OPTIONS");
        return;
      }
      if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method)) {
        response.setHeader("Allow","GET, HEAD, POST, OPTIONS");
        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS");
        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=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("\n");
      out.write("\n");
      out.write("\n");
      out.write("  \n");
      out.write("    $Title$\n");
      out.write("  \n");
      out.write("  \n");
      out.write("  
\n"); out.write(" \n"); out.write("\n"); out.write(" game\n"); out.write(" study\n"); out.write("\n"); out.write(" \n"
); out.write(" $END$\n"); out.write(" \n"); out.write("\n"); } 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); } } }

index.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$title>
  head>
  <body>
  
<input type="checkbox" name="hobby" value="game">game <input type="checkbox" name="hobby" value="study">study form> $END$ body> html>

更深一步

既然说jsp是servlet,那么jsp的类也一定会继承servlet类,并且一定会有service方法!
我们发现,jsp继承了org.apache.jasper.runtime.HttpJspBase,查阅这个类的源码可以发现这样一行代码:public abstract class HttpJspBase extends HttpServlet implements HttpJspPage
由此我们得知,jsp通过继承httpServlet,然后继承了servlet类

这样,我们就知道为什么:

<% bala %>  在jsp编译后的service方法中
<%! bala %> 在jsp编译后的成员位置
<%= bala %> 会输出到页面上

这也是为什么jsp内置对象的由来

说句题外话,现如今,Java的生态库异常强大,封装的框架也非常之多。前辈们一层层的抽象封装使得学习成本变得越来越低,所以我们身为程序员不应该只注重于CURD,而是多去思考面纱后面的原因,将会变得异常重要!

你可能感兴趣的:(Java,EE)