JSP开始

JSP开始

在我们开始做web程序时,我们已经发现,在后端写前端代码是一件非常难受的事情,写着不方便,修改也不方便,有没有一个技术,可以把前后端分离开,让它帮我们去做这个前端代码翻译成后端的呢? 这个技术就是JSP

当我们启动服务器时我们可以看到这样一个东西

  • Dcatalina.base=/Users/leojackasher/Library/Caches/JetBrains/IntelliJIdea2023.2/tomcat/3e69f6af-7bb2-4fc9-aa67-46d855f11965
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158

    当我们打开这个地址我们可以看到jsp里面会有帮我们翻译的前端代码

    + ```java
    /*
    * Generated by the Jasper component of Apache Tomcat
    * Version: Apache Tomcat/10.1.19
    * Generated at: 2024-03-16 06:10:37 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 jakarta.servlet.*;
    import jakarta.servlet.http.*;
    import jakarta.servlet.jsp.*;

    public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent,
    org.apache.jasper.runtime.JspSourceImports,
    org.apache.jasper.runtime.JspSourceDirectives {

    private static final jakarta.servlet.jsp.JspFactory _jspxFactory =
    jakarta.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.LinkedHashSet<>(3);
    _jspx_imports_packages.add("jakarta.servlet");
    _jspx_imports_packages.add("jakarta.servlet.http");
    _jspx_imports_packages.add("jakarta.servlet.jsp");
    _jspx_imports_classes = null;
    }

    private volatile jakarta.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 boolean getErrorOnELNotFound() {
    return false;
    }

    public jakarta.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 jakarta.servlet.http.HttpServletRequest request, final jakarta.servlet.http.HttpServletResponse response)
    throws java.io.IOException, jakarta.servlet.ServletException {

    if (!jakarta.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 jakarta.servlet.jsp.PageContext pageContext;
    jakarta.servlet.http.HttpSession session = null;
    final jakarta.servlet.ServletContext application;
    final jakarta.servlet.ServletConfig config;
    jakarta.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    jakarta.servlet.jsp.JspWriter _jspx_out = null;
    jakarta.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("<!DOCTYPE html>\n");
    out.write("<html>\n");
    out.write("<head>\n");
    out.write(" <title>JSP - Hello World</title>\n");
    out.write("</head>\n");
    out.write("<body>\n");
    out.write("<h1>");
    out.print( "Hello World!" );
    out.write("\n");
    out.write("</h1>\n");
    out.write("<br/>\n");
    out.write("<a href=\"hello-servlet\">Hello Servlet</a>\n");
    out.write("</body>\n");
    out.write("</html>");
    } catch (java.lang.Throwable t) {
    if (!(t instanceof jakarta.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会被服务器翻译生产java文件,编译成class文件执行

  • 仔细观看该文件第一个类,继承的HttpJspBase

  • public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
    
    1
    2
    3
    4
    5
    6

    我们可以打开源码看看,这个HttpJspbase到底是什么,

    + ```java
    public abstract class HttpJspBase extends HttpServlet implements HttpJspPage
    //原来最终还是继承的HttpServlet,依旧是一个servlet对象
    我们再回到JSP.java文件
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    out.write("\n");
    out.write("<!DOCTYPE html>\n");
    out.write("<html>\n");
    out.write("<head>\n");
    out.write(" <title>JSP - Hello World</title>\n");
    out.write("</head>\n");
    out.write("<body>\n");
    out.write("<h1>");
    out.print( "Hello World!" );
    out.write("\n");
    out.write("</h1>\n");
    out.write("<br/>\n");
    out.write("<a href=\"hello-servlet\">Hello Servlet</a>\n");
    out.write("</body>\n");
    out.write("</html>");
    //这是核心打印的地方,所以jsp文件里面都会打印出来

JSP内置对象和Page使用

前面我们有使用过<%@page session=“false”%>,用于关闭JSP自动创建session对象,那么这样的属性还有哪些?

  • <%@page errorPage=""%>
    //当jsp出现异常时,自动跳转页面
    
    1
    2
    3
    4

    + ```jsp
    <%@page iserrorPage="true"%>
    //将报错信息输出到命令台
  • <%@page conentText="text/html; charset=utf-8"%>
    

我们已经用了几个对象了,request, response,session,exception,printWriter,

还有pageContent,config,servletContext,page


JSP开始
http://example.com/2024/04/07/JSP开始/
作者
Jack Asher
发布于
2024年4月7日
许可协议