javaweb发展历程

简介

1995年java语言的诞生,刚好赶上了网络时代,java三大军:JavaSE(桌面程序),JavaME(手机端),JavaEE(web服务器),由于java在桌面程序和手机端的开发,都赶不上其他语言写的好,很快阵亡,然而发现,java简直就是为服务器而生的语言,由于项目都是发布在linux服务器上的,而java很重要的特性就是与平台无关性,因为java程序都是用在java虚拟机上运行的,所以不管是什么平台,只要安装了jvm就可以运行java程序,做到了一次编译,到处运行(Write Once,Run anyway),所以java语言很快就占领了web市场。javaweb就是用java语言编写的web应用,最开始开发初期,sun公司(现在已被Oracle公司收购)推出了Servlet技术,提供了开发web应用的基础,下面感受一下servlet

package com.hut.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 类说明
 *
 * @Author 茶茶
 * @Create 2020-04-23 21:27 新建
 */
public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();//获得响应流
        out.println("Hello,Servlet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

}

写好了自己的servlet类之后,只需要将该servlet类注册到web容器里,也就是web.xml里

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0"
         metadata-complete="true">

<!--    注册Servlet-->
    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>com.hut.servlet.HelloServlet</servlet-class>
    </servlet>
<!--    Servlet的映射路径-->
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

</web-app>

可以看见在web.xml注册了该HelloServlet类,并去了个名字叫做hello,再将该servlet映射到/hello请求,这样客户端只要发送/hello请求,就能找到该servlet类执行相应的代码,这就是一个简单的servlet。然而大型的web应用,都对应了很多的servlet类执行各种各样的请求,下面请欣赏一下下面的代码,简单看看就好,主要是看一下恶心程度(删除了部分代码)

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 {

      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<!DOCTYPE html>\r\n");
      out.write("<html lang=\"en\">\r\n");
      out.write("    <head>\r\n");
      out.write("        <meta charset=\"UTF-8\" />\r\n");
      out.write("        <title>");
      out.print(request.getServletContext().getServerInfo() );
      out.write("</title>\r\n");
      out.write("        <link href=\"favicon.ico\" rel=\"icon\" type=\"image/x-icon\" />\r\n");
      out.write("        <link href=\"favicon.ico\" rel=\"shortcut icon\" type=\"image/x-icon\" />\r\n");
      out.write("        <link href=\"tomcat.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n");
      out.write("    </head>\r\n");
      out.write("\r\n");
      out.write("    <body>\r\n");
      out.write("        <div id=\"wrapper\">\r\n");
      out.write("            <div id=\"navigation\" class=\"curved container\">\r\n");
      out.write("                <span id=\"nav-home\"><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("\">Home</a></span>\r\n");
      out.write("                <span id=\"nav-hosts\"><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("\">Documentation</a></span>\r\n");
      out.write("                <span id=\"nav-config\"><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("config/\">Configuration</a></span>\r\n");
      out.write("                <span id=\"nav-examples\"><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatExamplesUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("\">Examples</a></span>\r\n");
      out.write("                <span id=\"nav-wiki\"><a href=\"https://wiki.apache.org/tomcat/FrontPage\">Wiki</a></span>\r\n");
      out.write("                <span id=\"nav-lists\"><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html\">Mailing Lists</a></span>\r\n");
      out.write("                <span id=\"nav-help\"><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("findhelp.html\">Find Help</a></span>\r\n");
      out.write("                <br class=\"separator\" />\r\n");
      out.write("            </div>\r\n");
      out.write("            <div id=\"asf-box\">\r\n");
      out.write("                <h1>");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${pageContext.servletContext.serverInfo}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("</h1>\r\n");
      out.write("            </div>\r\n");
      out.write("            <div id=\"upper\" class=\"curved container\">\r\n");
      out.write("                <div id=\"congrats\" class=\"curved container\">\r\n");
      out.write("                    <h2>If you're seeing this, you've successfully installed Tomcat. Congratulations!</h2>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div id=\"notice\">\r\n");
      out.write("                    <img src=\"tomcat.png\" alt=\"[tomcat logo]\" />\r\n");
      out.write("                    <div id=\"tasks\">\r\n");
      out.write("                        <h3>Recommended Reading:</h3>\r\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("security-howto.html\">Security Considerations How-To</a></h4>\r\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("manager-howto.html\">Manager Application How-To</a></h4>\r\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("cluster-howto.html\">Clustering/Session Replication How-To</a></h4>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div id=\"actions\">\r\n");
      out.write("                    <div class=\"button\">\r\n");
      out.write("                        <a class=\"container shadow\" href=\"/manager/status\"><span>Server Status</span></a>\r\n");
      out.write("                    </div>\r\n");
      out.write("                    <div class=\"button\">\r\n");
      out.write("                        <a class=\"container shadow\" href=\"/manager/html\"><span>Manager App</span></a>\r\n");
      out.write("                    </div>\r\n");
      out.write("                    <div class=\"button\">\r\n");
      out.write("                        <a class=\"container shadow\" href=\"/host-manager/html\"><span>Host Manager</span></a>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <br class=\"separator\" />\r\n");
      out.write("            </div>\r\n");
      out.write("            <div id=\"middle\" class=\"curved container\">\r\n");
      out.write("                <h3>Developer Quick Start</h3>\r\n");
      out.write("                <div class=\"col25\">\r\n");
      out.write("                    <div class=\"container\">\r\n");
      out.write("                        <p><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("setup.html\">Tomcat Setup</a></p>\r\n");
      out.write("                        <p><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("appdev/\">First Web Application</a></p>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div class=\"col25\">\r\n");
      out.write("                    <div class=\"container\">\r\n");
      out.write("                        <p><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("realm-howto.html\">Realms &amp; AAA</a></p>\r\n");
      out.write("                        <p><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("jndi-datasource-examples-howto.html\">JDBC DataSources</a></p>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div class=\"col25\">\r\n");
      out.write("                    <div class=\"container\">\r\n");
      out.write("                        <p><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatExamplesUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("\">Examples</a></p>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div class=\"col25\">\r\n");
      out.write("                    <div class=\"container\">\r\n");
      out.write("                        <p><a href=\"https://wiki.apache.org/tomcat/Specifications\">Servlet Specifications</a></p>\r\n");
      out.write("                        <p><a href=\"https://wiki.apache.org/tomcat/TomcatVersions\">Tomcat Versions</a></p>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <br class=\"separator\" />\r\n");
      out.write("            </div>\r\n");
      out.write("            <div id=\"lower\">\r\n");
      out.write("                <div id=\"low-manage\" class=\"\">\r\n");
      out.write("                    <div class=\"curved container\">\r\n");
      out.write("                        <h3>Managing Tomcat</h3>\r\n");
      out.write("                        <p>For security, access to the <a href=\"/manager/html\">manager webapp</a> is restricted.\r\n");
      out.write("                        Users are defined in:</p>\r\n");
      out.write("                        <pre>$CATALINA_HOME/conf/tomcat-users.xml</pre>\r\n");
      out.write("                        <p>In Tomcat 9.0 access to the manager application is split between\r\n");
      out.write("                           different users. &nbsp; <a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("manager-howto.html\">Read more...</a></p>\r\n");
      out.write("                        <br />\r\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("RELEASE-NOTES.txt\">Release Notes</a></h4>\r\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("changelog.html\">Changelog</a></h4>\r\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("migration.html\">Migration Guide</a></h4>\r\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("security.html\">Security Notices</a></h4>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div id=\"low-docs\" class=\"\">\r\n");
      out.write("                    <div class=\"curved container\">\r\n");
      out.write("                        <h3>Documentation</h3>\r\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("\">Tomcat 9.0 Documentation</a></h4>\r\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("config/\">Tomcat 9.0 Configuration</a></h4>\r\n");
      out.write("                        <h4><a href=\"https://wiki.apache.org/tomcat/FrontPage\">Tomcat Wiki</a></h4>\r\n");
      out.write("                        <p>Find additional important configuration information in:</p>\r\n");
      out.write("                        <pre>$CATALINA_HOME/RUNNING.txt</pre>\r\n");
      out.write("                        <p>Developers may be interested in:</p>\r\n");
      out.write("                        <ul>\r\n");
      out.write("                            <li><a href=\"https://tomcat.apache.org/bugreport.html\">Tomcat 9.0 Bug Database</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("api/index.html\">Tomcat 9.0 JavaDocs</a></li>\r\n");
      out.write("                            <li><a href=\"https://github.com/apache/tomcat/tree/master\">Tomcat 9.0 Git Repository at GitHub</a></li>\r\n");
      out.write("                        </ul>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div id=\"low-help\" class=\"\">\r\n");
      out.write("                    <div class=\"curved container\">\r\n");
      out.write("                        <h3>Getting Help</h3>\r\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("faq/\">FAQ</a> and <a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html\">Mailing Lists</a></h4>\r\n");
      out.write("                        <p>The following mailing lists are available:</p>\r\n");
      out.write("                        <ul>\r\n");
      out.write("                            <li id=\"list-announce\"><strong><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html#tomcat-announce\">tomcat-announce</a><br />\r\n");
      out.write("                                Important announcements, releases, security vulnerability notifications. (Low volume).</strong>\r\n");
      out.write("                            </li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html#tomcat-users\">tomcat-users</a><br />\r\n");
      out.write("                                User support and discussion\r\n");
      out.write("                            </li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html#taglibs-user\">taglibs-user</a><br />\r\n");
      out.write("                                User support and discussion for <a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("taglibs/\">Apache Taglibs</a>\r\n");
      out.write("                            </li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html#tomcat-dev\">tomcat-dev</a><br />\r\n");
      out.write("                                Development mailing list, including commit messages\r\n");
      out.write("                            </li>\r\n");
      out.write("                        </ul>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <br class=\"separator\" />\r\n");
      out.write("            </div>\r\n");
      out.write("            <div id=\"footer\" class=\"curved container\">\r\n");
      out.write("                <div class=\"col20\">\r\n");
      out.write("                    <div class=\"container\">\r\n");
      out.write("                        <h4>Other Downloads</h4>\r\n");
      out.write("                        <ul>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("download-connectors.cgi\">Tomcat Connectors</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("download-native.cgi\">Tomcat Native</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("taglibs/\">Taglibs</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("deployer-howto.html\">Deployer</a></li>\r\n");
      out.write("                        </ul>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div class=\"col20\">\r\n");
      out.write("                    <div class=\"container\">\r\n");
      out.write("                        <h4>Other Documentation</h4>\r\n");
      out.write("                        <ul>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("connectors-doc/\">Tomcat Connectors</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("connectors-doc/\">mod_jk Documentation</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("native-doc/\">Tomcat Native</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("deployer-howto.html\">Deployer</a></li>\r\n");
      out.write("                        </ul>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div class=\"col20\">\r\n");
      out.write("                    <div class=\"container\">\r\n");
      out.write("                        <h4>Get Involved</h4>\r\n");
      out.write("                        <ul>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("getinvolved.html\">Overview</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("source.html\">Source Repositories</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html\">Mailing Lists</a></li>\r\n");
      out.write("                            <li><a href=\"https://wiki.apache.org/tomcat/FrontPage\">Wiki</a></li>\r\n");
      out.write("                        </ul>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div class=\"col20\">\r\n");
      out.write("                    <div class=\"container\">\r\n");
      out.write("                        <h4>Miscellaneous</h4>\r\n");
      out.write("                        <ul>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("contact.html\">Contact</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("legal.html\">Legal</a></li>\r\n");
      out.write("                            <li><a href=\"https://www.apache.org/foundation/sponsorship.html\">Sponsorship</a></li>\r\n");
      out.write("                            <li><a href=\"https://www.apache.org/foundation/thanks.html\">Thanks</a></li>\r\n");
      out.write("                        </ul>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div class=\"col20\">\r\n");
      out.write("                    <div class=\"container\">\r\n");
      out.write("                        <h4>Apache Software Foundation</h4>\r\n");
      out.write("                        <ul>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("whoweare.html\">Who We Are</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("heritage.html\">Heritage</a></li>\r\n");
      out.write("                            <li><a href=\"https://www.apache.org\">Apache Home</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("resources.html\">Resources</a></li>\r\n");
      out.write("                        </ul>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <br class=\"separator\" />\r\n");
      out.write("            </div>\r\n");
      out.write("            <p class=\"copyright\">Copyright &copy;1999-");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${year}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write(" Apache Software Foundation.  All Rights Reserved</p>\r\n");
      out.write("        </div>\r\n");
      out.write("    </body>\r\n");
      out.write("\r\n");
      out.write("</html>\r\n");
  }
}

然后就会发现这特喵的不就是我们现在写的html页面,没错,但是只是相似,以前的开发,页面的每一行都是通过一个out对象来输出,也就是后端开发人员需要用这样的形式来进行开发,一个请求对应一个servlet,很多的请求,就得写很多这样的恶心代码,并且没有分层,业务,数据以及页面渲染都是写在一个servlet类里,所以为了简化我们的开发,就出现了jsp的页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>

所以jsp页面的出现就是为了减轻开发人员的压力,究其本质一个jsp页面就是一个servlet类,不信可以看看


image.png

这就发现,项目发布之后,一个个jsp页面就被编译成了一个个xxx_jsp.java的java类,再编译成字节码文件,看看里面的东西

/*
 * Generated by the Jasper component of Apache Tomcat
 * Version: Apache Tomcat/9.0.34
 * Generated at: 2020-04-22 14:45: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 {

    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;
    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, false, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\r\n");
      out.write("\r\n");

java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");
request.setAttribute("year", sdf.format(new java.util.Date()));
request.setAttribute("tomcatUrl", "https://tomcat.apache.org/");
request.setAttribute("tomcatDocUrl", "/docs/");
request.setAttribute("tomcatExamplesUrl", "/examples/");

      out.write("\r\n");
      out.write("<!DOCTYPE html>\r\n");
      out.write("<html lang=\"en\">\r\n");
      out.write("    <head>\r\n");
      out.write("        <meta charset=\"UTF-8\" />\r\n");
      out.write("        <title>");
      out.print(request.getServletContext().getServerInfo() );
      out.write("</title>\r\n");
      out.write("        <link href=\"favicon.ico\" rel=\"icon\" type=\"image/x-icon\" />\r\n");
      out.write("        <link href=\"favicon.ico\" rel=\"shortcut icon\" type=\"image/x-icon\" />\r\n");
      out.write("        <link href=\"tomcat.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n");
      out.write("    </head>\r\n");
      out.write("\r\n");
      out.write("    <body>\r\n");
      out.write("        <div id=\"wrapper\">\r\n");
      out.write("            <div id=\"navigation\" class=\"curved container\">\r\n");
      out.write("                <span id=\"nav-home\"><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("\">Home</a></span>\r\n");
      out.write("                <span id=\"nav-hosts\"><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("\">Documentation</a></span>\r\n");
      out.write("                <span id=\"nav-config\"><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("config/\">Configuration</a></span>\r\n");
      out.write("                <span id=\"nav-examples\"><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatExamplesUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("\">Examples</a></span>\r\n");
      out.write("                <span id=\"nav-wiki\"><a href=\"https://wiki.apache.org/tomcat/FrontPage\">Wiki</a></span>\r\n");
      out.write("                <span id=\"nav-lists\"><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html\">Mailing Lists</a></span>\r\n");
      out.write("                <span id=\"nav-help\"><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("findhelp.html\">Find Help</a></span>\r\n");
      out.write("                <br class=\"separator\" />\r\n");
      out.write("            </div>\r\n");
      out.write("            <div id=\"asf-box\">\r\n");
      out.write("                <h1>");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${pageContext.servletContext.serverInfo}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("</h1>\r\n");
      out.write("            </div>\r\n");
      out.write("            <div id=\"upper\" class=\"curved container\">\r\n");
      out.write("                <div id=\"congrats\" class=\"curved container\">\r\n");
      out.write("                    <h2>If you're seeing this, you've successfully installed Tomcat. Congratulations!</h2>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div id=\"notice\">\r\n");
      out.write("                    <img src=\"tomcat.png\" alt=\"[tomcat logo]\" />\r\n");
      out.write("                    <div id=\"tasks\">\r\n");
      out.write("                        <h3>Recommended Reading:</h3>\r\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("security-howto.html\">Security Considerations How-To</a></h4>\r\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("manager-howto.html\">Manager Application How-To</a></h4>\r\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("cluster-howto.html\">Clustering/Session Replication How-To</a></h4>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div id=\"actions\">\r\n");
      out.write("                    <div class=\"button\">\r\n");
      out.write("                        <a class=\"container shadow\" href=\"/manager/status\"><span>Server Status</span></a>\r\n");
      out.write("                    </div>\r\n");
      out.write("                    <div class=\"button\">\r\n");
      out.write("                        <a class=\"container shadow\" href=\"/manager/html\"><span>Manager App</span></a>\r\n");
      out.write("                    </div>\r\n");
      out.write("                    <div class=\"button\">\r\n");
      out.write("                        <a class=\"container shadow\" href=\"/host-manager/html\"><span>Host Manager</span></a>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <br class=\"separator\" />\r\n");
      out.write("            </div>\r\n");
      out.write("            <div id=\"middle\" class=\"curved container\">\r\n");
      out.write("                <h3>Developer Quick Start</h3>\r\n");
      out.write("                <div class=\"col25\">\r\n");
      out.write("                    <div class=\"container\">\r\n");
      out.write("                        <p><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("setup.html\">Tomcat Setup</a></p>\r\n");
      out.write("                        <p><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("appdev/\">First Web Application</a></p>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div class=\"col25\">\r\n");
      out.write("                    <div class=\"container\">\r\n");
      out.write("                        <p><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("realm-howto.html\">Realms &amp; AAA</a></p>\r\n");
      out.write("                        <p><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("jndi-datasource-examples-howto.html\">JDBC DataSources</a></p>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div class=\"col25\">\r\n");
      out.write("                    <div class=\"container\">\r\n");
      out.write("                        <p><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatExamplesUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("\">Examples</a></p>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div class=\"col25\">\r\n");
      out.write("                    <div class=\"container\">\r\n");
      out.write("                        <p><a href=\"https://wiki.apache.org/tomcat/Specifications\">Servlet Specifications</a></p>\r\n");
      out.write("                        <p><a href=\"https://wiki.apache.org/tomcat/TomcatVersions\">Tomcat Versions</a></p>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <br class=\"separator\" />\r\n");
      out.write("            </div>\r\n");
      out.write("            <div id=\"lower\">\r\n");
      out.write("                <div id=\"low-manage\" class=\"\">\r\n");
      out.write("                    <div class=\"curved container\">\r\n");
      out.write("                        <h3>Managing Tomcat</h3>\r\n");
      out.write("                        <p>For security, access to the <a href=\"/manager/html\">manager webapp</a> is restricted.\r\n");
      out.write("                        Users are defined in:</p>\r\n");
      out.write("                        <pre>$CATALINA_HOME/conf/tomcat-users.xml</pre>\r\n");
      out.write("                        <p>In Tomcat 9.0 access to the manager application is split between\r\n");
      out.write("                           different users. &nbsp; <a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("manager-howto.html\">Read more...</a></p>\r\n");
      out.write("                        <br />\r\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("RELEASE-NOTES.txt\">Release Notes</a></h4>\r\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("changelog.html\">Changelog</a></h4>\r\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("migration.html\">Migration Guide</a></h4>\r\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("security.html\">Security Notices</a></h4>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div id=\"low-docs\" class=\"\">\r\n");
      out.write("                    <div class=\"curved container\">\r\n");
      out.write("                        <h3>Documentation</h3>\r\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("\">Tomcat 9.0 Documentation</a></h4>\r\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("config/\">Tomcat 9.0 Configuration</a></h4>\r\n");
      out.write("                        <h4><a href=\"https://wiki.apache.org/tomcat/FrontPage\">Tomcat Wiki</a></h4>\r\n");
      out.write("                        <p>Find additional important configuration information in:</p>\r\n");
      out.write("                        <pre>$CATALINA_HOME/RUNNING.txt</pre>\r\n");
      out.write("                        <p>Developers may be interested in:</p>\r\n");
      out.write("                        <ul>\r\n");
      out.write("                            <li><a href=\"https://tomcat.apache.org/bugreport.html\">Tomcat 9.0 Bug Database</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("api/index.html\">Tomcat 9.0 JavaDocs</a></li>\r\n");
      out.write("                            <li><a href=\"https://github.com/apache/tomcat/tree/master\">Tomcat 9.0 Git Repository at GitHub</a></li>\r\n");
      out.write("                        </ul>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div id=\"low-help\" class=\"\">\r\n");
      out.write("                    <div class=\"curved container\">\r\n");
      out.write("                        <h3>Getting Help</h3>\r\n");
      out.write("                        <h4><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("faq/\">FAQ</a> and <a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html\">Mailing Lists</a></h4>\r\n");
      out.write("                        <p>The following mailing lists are available:</p>\r\n");
      out.write("                        <ul>\r\n");
      out.write("                            <li id=\"list-announce\"><strong><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html#tomcat-announce\">tomcat-announce</a><br />\r\n");
      out.write("                                Important announcements, releases, security vulnerability notifications. (Low volume).</strong>\r\n");
      out.write("                            </li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html#tomcat-users\">tomcat-users</a><br />\r\n");
      out.write("                                User support and discussion\r\n");
      out.write("                            </li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html#taglibs-user\">taglibs-user</a><br />\r\n");
      out.write("                                User support and discussion for <a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("taglibs/\">Apache Taglibs</a>\r\n");
      out.write("                            </li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html#tomcat-dev\">tomcat-dev</a><br />\r\n");
      out.write("                                Development mailing list, including commit messages\r\n");
      out.write("                            </li>\r\n");
      out.write("                        </ul>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <br class=\"separator\" />\r\n");
      out.write("            </div>\r\n");
      out.write("            <div id=\"footer\" class=\"curved container\">\r\n");
      out.write("                <div class=\"col20\">\r\n");
      out.write("                    <div class=\"container\">\r\n");
      out.write("                        <h4>Other Downloads</h4>\r\n");
      out.write("                        <ul>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("download-connectors.cgi\">Tomcat Connectors</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("download-native.cgi\">Tomcat Native</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("taglibs/\">Taglibs</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("deployer-howto.html\">Deployer</a></li>\r\n");
      out.write("                        </ul>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div class=\"col20\">\r\n");
      out.write("                    <div class=\"container\">\r\n");
      out.write("                        <h4>Other Documentation</h4>\r\n");
      out.write("                        <ul>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("connectors-doc/\">Tomcat Connectors</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("connectors-doc/\">mod_jk Documentation</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("native-doc/\">Tomcat Native</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("deployer-howto.html\">Deployer</a></li>\r\n");
      out.write("                        </ul>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div class=\"col20\">\r\n");
      out.write("                    <div class=\"container\">\r\n");
      out.write("                        <h4>Get Involved</h4>\r\n");
      out.write("                        <ul>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("getinvolved.html\">Overview</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("source.html\">Source Repositories</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("lists.html\">Mailing Lists</a></li>\r\n");
      out.write("                            <li><a href=\"https://wiki.apache.org/tomcat/FrontPage\">Wiki</a></li>\r\n");
      out.write("                        </ul>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div class=\"col20\">\r\n");
      out.write("                    <div class=\"container\">\r\n");
      out.write("                        <h4>Miscellaneous</h4>\r\n");
      out.write("                        <ul>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("contact.html\">Contact</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("legal.html\">Legal</a></li>\r\n");
      out.write("                            <li><a href=\"https://www.apache.org/foundation/sponsorship.html\">Sponsorship</a></li>\r\n");
      out.write("                            <li><a href=\"https://www.apache.org/foundation/thanks.html\">Thanks</a></li>\r\n");
      out.write("                        </ul>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <div class=\"col20\">\r\n");
      out.write("                    <div class=\"container\">\r\n");
      out.write("                        <h4>Apache Software Foundation</h4>\r\n");
      out.write("                        <ul>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("whoweare.html\">Who We Are</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("heritage.html\">Heritage</a></li>\r\n");
      out.write("                            <li><a href=\"https://www.apache.org\">Apache Home</a></li>\r\n");
      out.write("                            <li><a href=\"");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write("resources.html\">Resources</a></li>\r\n");
      out.write("                        </ul>\r\n");
      out.write("                    </div>\r\n");
      out.write("                </div>\r\n");
      out.write("                <br class=\"separator\" />\r\n");
      out.write("            </div>\r\n");
      out.write("            <p class=\"copyright\">Copyright &copy;1999-");
      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${year}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
      out.write(" Apache Software Foundation.  All Rights Reserved</p>\r\n");
      out.write("        </div>\r\n");
      out.write("    </body>\r\n");
      out.write("\r\n");
      out.write("</html>\r\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);
    }
  }
}

是不是很神奇,可以看到其中有这样的代码

    final javax.servlet.jsp.PageContext pageContext;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
  • pageContext 页面上下文对象 类型 javax.servlet.jsp.PageContext 作用域 Page
  • application 应用程序对象 类型 javax.servlet.ServletContext 作用域 Application
  • config 配置对象 类型 javax.servlet.ServletConfig 作用域 Page
  • out 输出对象 类型 javax.servlet.jsp.JspWriter 作用域 Page
  • page 页面对象 类型 javax.lang.Object 作用域 Page
    除了上面的5个内置对象之外,还有下面这4个内置对象
  • request 请求对象  类型 javax.servlet.ServletRequest 作用域 Request
  • response 响应对象 类型 javax.servlet.SrvletResponse 作用域 Page
  • session 会话对象 类型 javax.servlet.http.HttpSession 作用域 Session
  • exception 例外对象 类型 javax.lang.Throwable 作用域 page
    这就是传说中的jsp的9大内置对象,除了这9大内置对象,这些内置对象中用来存东西的有,pageContext、application、request、session,这四个对象可以用来存放东西,既然用来存,自然就有这些存的数据的作用吧,其中
  • pageContext的作用域是当前页,
  • application的作用域是当前应用程序,在整个web应用中生效,所以很多地方都能获得该对象,
  • request的作用域就是一次请求中有效,一次请求包括的转发(forward),但是不包括重定向,转发跟重定向不一样
  • session的作用域就是一次会话中有效,一次会话的意思就是一个从打开浏览器到关闭浏览器就是一次会话
    以上就是jsp的9大内置对象和4大作用域。

如此这般,将页面从servlet类中剥离开发,使用jsp简化页面的开发,但是本质还是servlet页面,那页面剥离出去后,我们的业务逻辑,调用数据库,数据,以及页面的转发还在里面,职责不单一,所以就将这些职责一一剥离出去,架构中的一句话,没有什么是加一层解决不了的,如果一层解决不了,那就再加一层,于是就将业务逻辑代码放在了service层中,访问数据库的代码放在了dao层中,servlet的职责就变成了只是接收前端jsp页面传过来的数据,调用service层方法,service层方法再调用dao层方法,查到数据再一层层的返回,servlet层就可以将数据返回给前端jsp页面进行渲染,最后返回给用户,至此,我们的MVC模型以及三层架构行程,看图


image.png

其中有一个javabean,也有其他的叫法,比如pojo实体类,从数据库里查出来的数据都是一个个的对象对不对,并且有相同的属性,于是就写一个类跟数据库表的字段进行映射,这样返回的数据都是可以保存在对应的实体类中,这个实体类是可以在各个层中都可以被使用的。
现在,就是javaweb的一个整体结构

讲一讲cookie和session:二者都是用来保存一些数据用的

cookie的字面意思是曲奇饼,小饼干,在java中我们可以将之理解为一个信物
session:会议,会话
cookie保存的数据是在客户端的浏览器里,一些简单的数据
session保存的数据实在服务器端里,一些重要的数据,比如用户的登录信息
看代码:
cookie的

package com.hut.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * 类说明
 * 如果出现编码问题,就可以使用编码以及解码
 * @Author 茶茶
 * @Create 2020-04-24 16:35 新建
 */
public class CookieDemo03 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");

        Cookie cookie = new Cookie("name", URLEncoder.encode("dh","utf-8"));
        resp.addCookie(cookie);
        PrintWriter out = resp.getWriter();
        Cookie[] cookies = req.getCookies();
        if (cookies != null){
            for (Cookie c : cookies) {
                if (c.getName().equals("name")){
                    System.out.println(c.getValue());
                    out.write("名字:" + URLDecoder.decode(c.getValue(),"utf-8"));
                }
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

}

只要new一个cookie的对象,把数据存到cookie里,再将cookie存在resp这个响应对象里,就能返回给前端,cookie是保存在客户端的,看图


image.png

出现了乱码因为编码不一致

package com.hut.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * 类说明
 *
 * @Author 茶茶
 * @Create 2020-04-24 17:07 新建
 */
public class SessionDemo01 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //解决中文乱码问题
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //获取session
        HttpSession session = req.getSession();

        //给session存东西
        session.setAttribute("name","dh");

        //获取session的id
        String sessionId = session.getId();
        System.out.println(sessionId);

        //判读session是不是新的
        if (session.isNew()){
            resp.getWriter().write("session是新创建的" + sessionId);
        }else {
            resp.getWriter().write("session已经存在" + sessionId);
        }

        //session做的事情
//        Cookie cookie = new Cookie("JSESSIONID",sessionId);
//        resp.addCookie(cookie);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

}

从req请求对象中获取一个session对象,不需要将session对象保存在resp响应对象里,因为session是保存在服务器端的,下面有一个这样的场景:就是有一个用户登录,然后判断用户名密码是否存在数据库并且都正确,都正确我们就将该用户的信息保存在一个session里,那用户跳转了一个新页面,服务器是怎么判断该用户还是刚才那个存在session里的用户呢?
原因是这样的,每有一个用户过来,我们就将该用户保存在一个session里,然后给该session一个ID,最后再将ID保存在cookie里,返回给客户端,这个ID的名字就是叫做JSESSIONID,我们可以看到


image.png

我们可以看到在同一个web项目里,只要浏览器不关闭,任何页面都会带有一个JSESSIONID,这个JSESSIONID就代表了你这个客户端,然后每次你发请求到服务器,都会带上这个JSESSIONID,注意这个JSESSIONID只在该浏览器里这一次会话,就是还未关闭浏览器有效,就好比你用同一个360浏览器登录QQ空间,只有这个浏览器未关闭,该JSESSIONID一直存在,并且只属于这一个QQ空间的web项目,浏览器同时还可以登录网易云音乐等等,所以可以这样理解,一个JSESSIONID属于的是一次会话中的同一个WEB资源中的信息,再次请求带上JSESSIONID,发给服务器,服务器根据JSESSIONID查找到只属于你的那个JSESSIONID,区别session是靠ID区别的,同名不一定是同一个session,比如有两个用户,两个客户端,登录同一个web应用,是两台电脑,然后后台session存用户的键都是userInfo,但是区分这两个用户,是不是还是得靠JSESSIONID来区分。这样既保障了登录用户的数据安全,又能区分不同的用户,使得大家不会混淆起来,如果将用户的信息存在cookie里,是不是就不安全,可以抓包抓取cookie的数据啊,账号和密码都在cookie里,很不安全,所以cookie只存一个JSESSIONID代表该用户,将用户信息存在服务器里,就相当安全啊


image.png

讲一讲javaweb里的两个对象:HttpServletRequest req和HttpServletResponse resp

这两个对象极其的重要,req对象代表了用户的一次请求,resp对象代表了一次响应


image.png

请求:请求头和请求体,里面封装了这一次请求的数据发送给服务器
响应:响应头和响应体,服务器接收到一次请求,根据这次请求,做出一次响应

讲一讲转发和重定向

转发不会改变url路径,但是重定向会改变url路径,为什么呢?


image.png

以上就是javaweb的一些知识,从最开始的servlet开发,到后来出现jsp页面,出现MVC模型以及三层架构,现在来讲讲框架。

讲一件拦截器和监听器

在使用servlet进行开发的时候,是不是就只要让servlet类继承HttpServlet,重写doGet和doPost方法就可以啦,那拦截器和监听器也是同样的道理,拦截器只需要实现一些filter接口,实现里面的方法就好了,拦截器的作用就是拦截所有进来的请求,做一些相应的操作,也会拦截所有出去的响应,这就可以很好的帮我们解决字符编码的问题呀
拦截器代码

package com.hut.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * 类说明
 * 过滤器
 * @Author 茶茶
 * @Create 2020-04-24 21:53 新建
 */
public class CharacterEncodingFilter implements Filter {

    //初始化:web服务器启动,就已经初始化了,随时等待过滤对象出现
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("CharacterEncodingFilter初始化了");
    }

    //Chain : 链
    /*
    1.过滤中的所有代码,在过滤特定请求的时候都会执行
    2.必须要让过滤器继续进行
        filterChain.doFilter(servletRequest,servletResponse);
     */
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        servletResponse.setContentType("text/html;charset=utf-8");

        System.out.println("CharacterEncodingFilter执行前");
        filterChain.doFilter(servletRequest,servletResponse);//让我们的程序继续走,如果不写,请求就被拦截了
        System.out.println("CharacterEncodingFilter执行后");

    }

    //销毁
    public void destroy() {
        System.out.println("CharacterEncodingFilter销毁了");
    }

}

监听器,是不是就是监听一个个对象的动作,从对象的出生,到对象的死亡,可以一直监听着,并对于对象的每一个动作进行一些事件,比如HttpSession对象的产生,就可以监听网站在线人数+1等等。
监听器代码

package com.hut.listen;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * 类说明
 * 在线人数监听
 * @Author 茶茶
 * @Create 2020-04-24 22:53 新建
 */
public class OnlineCountListen implements HttpSessionListener {

    //创建session监听
    //一旦一个session被创建了就执行该方法
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        ServletContext servletContext = httpSessionEvent.getSession().getServletContext();
        System.out.println(httpSessionEvent.getSession().getId());
        Integer onlineCount = (Integer) servletContext.getAttribute("OnlineCount");
        if (onlineCount==null){
            onlineCount = new Integer(1);
        }else {
            int count = onlineCount.intValue();
            onlineCount = ++count;//人数+1
        }
        servletContext.setAttribute("OnlineCount",onlineCount);
    }

    //销毁session监听
    //一旦一个session被销毁了就执行该方法
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        ServletContext servletContext = httpSessionEvent.getSession().getServletContext();
        Integer onlineCount = (Integer) servletContext.getAttribute("OnlineCount");
        if (onlineCount==null){
            onlineCount = new Integer(0);
        }else {
            int count = onlineCount.intValue();
            onlineCount = --count;//人数+1
        }
        servletContext.setAttribute("OnlineCount",onlineCount);
    }

    /*
    session的销毁
    1、手动销毁   httpSessionEvent.getSession().invalidate();
    2、在web.xml中配置session的过期时间
     */

}

框架

框架的出现,为什么会出现框架,用来干嘛的?
三层架构是控制器层,业务层和数据持久层,也就是controller层,service层和dao层
首先controller层,以前是一个个的servlet类,每写一个servlet类就要马上去web.xml里注册一个servlet,那要是大项目,成百上千个servlet类就要注册成百上千个的servlet,那就太太太麻烦了,并且不容易维护,于是出现springmvc框架,就是来帮我们大大简化控制层的开发的,看图


image.png

springmvc的具体配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!--开启注解扫,只扫描@Controller注解的类-->
    <context:component-scan base-package="com.hut.controller">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter>
    </context:component-scan>

    <!--配置视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--过滤静态资源-->
    <mvc:resources mapping="/css/**" location="/statics/css"></mvc:resources>
    <mvc:resources mapping="/images/**" location="/statics/images"></mvc:resources>
    <mvc:resources mapping="/js/**" location="/statics/js"></mvc:resources>

    <!--开启SpringMVC注解的支持-->
    <mvc:annotation-driven></mvc:annotation-driven>

</beans>

springmvc只管我们的controller层,所以springmvc.xml配置文件只扫描controller包下@Controller注解,配置视图解析器的作用,控制器层里的方法只返回字符串,springmvc框架拿到返回的值后,拼接完整的路径/WEB-INF/pages/hello.jsp页面,所以这样子方法就只返回具体页面名字,就不用加具体的路径了,其次,web.xml是不是把所有的请求路径,包括资源路径都给拦截了,但是资源是不需要拦截的对不对,直接去找项目里的资源,所以资源路径要放行,因此过滤静态资源也是必须的,最后开启一下注解支持,使得能够使用注解开发,方便许多。
然后控制器层对应的流行框架是springmvc框架,那service层和dao层是不是也要分别对应一层框架,service层就对应spring框架,dao层对应mybatis框架,这就是传统企业开发的SSM框架

spring框架的配置文件和mybatis框架的配置文件,为了方便,一般将mybatis框架放进spring的配置文件里

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--在web.xml里加载这个spring的配置文件,服务器已启动便创建spring容器,然后将Service层和dao层的bean对象存储在容器中-->
    <!--Service层有自己的实现类,加载进spring容器-->
    <!--dao层没有让自己编写实现类,dao层的接口由MyBatis框架来帮我们实现,所以在下面配置MyBatis,在创建spring容器时,也加载MyBatis的配置文件,并将dao层的bean对象存进spring容器中-->

    <!--配置扫描包,管理service层和mapper层的bean对象-->
    <context:component-scan base-package="com.hut">
        <!--不扫描用注解@Controller的类-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
    </context:component-scan>

    <!--spring整个mybatis框架,加载mybatis的核心配置文件-->
    <!--配置连接池-->
    <!--导入c3p0依赖-->
    <!--com.mchange.v2.c3p0.ComboPooledDataSource在c3p0jar包下-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>

    <!--配置SqlSessionFactory工厂对象-->
    <!--导入mybatis-spring依赖-->
    <!--导入spring-jdbc依赖-->
    <!--org.mybatis.spring.SqlSessionFactoryBean在mybatis-spring包下-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--配置接口所在的包-->
    <!--导入mybatis-spring依赖-->
    <!--org.mybatis.spring.mapper.MapperScannerConfigurer在mybatis-spring包下-->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.hut.mapper"></property>
    </bean>

    <!--声明式事务管理-->
    <!--配置事务管理器-->
    <!--org.springframework.jdbc.datasource.DataSourceTransactionManager在spring-jdbc包下-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--配置事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" isolation="DEFAULT"/>
        </tx:attributes>
    </tx:advice>

    <!--配置AOP的增强-->
    <!--导入-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(public * com.hut.service.impl.*ServiceImpl.*(..))"></aop:advisor>
    </aop:config>

</beans>

首先同样的,spring配置文件,由于整合了mybatis框架的配置文件,所以spring的配置文件要扫描除了@Controller的注解,也就是只扫描@Service和@Respository这两个注解配置的类,然后还要配置一下AOP的增强,这就是Spring最重要的两点,IOC和AOP,然后配置一下MyBatis的配置,首先数据源是肯定要,因为你要连接数据库嘛,然后需要将SqlSessionFacrotyBean的对象放进spring容器里,最后配置一下,对应的dao层所在的位置,存放进MapperScannerConfigurer对象里,因为Mybatis不需要我们写JDBC的那些连接代码,只让我们专注于sql语句的编写和返回的数据集映射,所以dao层就是一个接口层,具体的实现由mybatis框架帮我们实现,这样大大的简化了我们的代码,最后如果有事务的要求,配置一些声明式事务管理,事务管理也需要数据源,还要配置一下事务的管理者,前面是开启事务管理,后面是一个事务管理者管理所有的事务,至此spring,springmvc,mybatis的配置文件都写好了,但是写好了归写好了,是不是没有应用上啊,启动一个项目,鬼知道要帮你加载这三个配置文件啊,所以像以前注册servlet一样,要注册到web.xml里面啊,因为启动项目,会默认读取web.xml里面的东西,所以只要将这些配置文件塞进web.xml里面,就可以啦

web.xml的配置

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!--配置spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件-->
  <!--解决方案1:将applicationContext.xml配置文件复制一份放在WEN-INF目录下-->
  <!--解决方案2:指定加载的类路径-->
  <!--org.springframework.web.context.ContextLoaderListener在spring-web包下-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--用方案2:设置配置文件的路径-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

  <!--配置前端控制器-->
  <!--org.springframework.web.servlet.DispatcherServlet在spring-webmvc包下-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--加载springmvc.xml配置文件-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <!--启动服务器,创建该servlet-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!--解决中文乱码的过滤器-->
  <!--org.springframework.web.filter.CharacterEncodingFilter在spring-web包下-->
  <filter>
    <filter-name>haracterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <!--设置编码集-->
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>haracterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

先配置一个监听器,当加载上下文的时候,加载上我们的spring的配置文件--->applicationContext.xml
然后配置一个Servlet,前端控制器,就不需要注册很多的servlet来控制所有的请求,加载这个前端控制器的时候,加个初始化参数,把springmvc.xml配置文件给加载了,这样我们的框架就被应用上去了,最后注册一个拦截器,帮我们处理乱码的问题,使用同一的编码格式UTF-8,所有的准备工作就做好了,我们就只要专注于web应用的业务开发就好了呀。

又来啦又来啦,SpringBoot它来啦

上面SSM框架的开发配置文件是不是超级繁琐,并且随着需求,配置文件里的东西越来越多,变成了配置地狱,并且出错了还不容器找到原因,于是就出现了SpringBoot,它还有个名字叫做SpringBoot全家桶,所以SpringBoot其实是帮我们开发人员减少配置文件用的啊,暂且先这么看待它,它帮我们把所有框架的配置全都配置好了,我们只管开发项目就好了,基本上就不用去管配置文件那些玩意了,因为那些配置让人头疼,而springboot直接帮我们把配置文件搞定了,爽不爽,上来就开始写业务,经理再也不用担心我配置文件出错啦。其实springboot不仅帮我们简化了配置,还内嵌了tomcat容器,使用SSM框架的时候,项目的发布还需要放在tomcat容器里,但是springboot连tomcat都帮我们内嵌了,只需要指定一下tomcat的端口号就行啦,所以springboot主要是用于微服务的开发。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,402评论 6 499
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,377评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,483评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,165评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,176评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,146评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,032评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,896评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,311评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,536评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,696评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,413评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,008评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,815评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,698评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,592评论 2 353

推荐阅读更多精彩内容