{"id":16420,"date":"2025-04-18T22:52:26","date_gmt":"2025-04-18T14:52:26","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/16420.html"},"modified":"2025-04-18T22:52:26","modified_gmt":"2025-04-18T14:52:26","slug":"springboot%e7%ac%ac%e4%b8%89%e7%ab%99%ef%bc%884%ef%bc%89%ef%bc%9a%e9%85%8d%e7%bd%ae%e5%b5%8c%e5%85%a5%e5%bc%8f%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%bd%bf%e7%94%a8%e5%a4%96%e7%bd%ae%e7%9a%84servlet","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/16420.html","title":{"rendered":"SpringBoot\u7b2c\u4e09\u7ad9\uff084\uff09\uff1a\u914d\u7f6e\u5d4c\u5165\u5f0f\u670d\u52a1\u5668&amp;\u4f7f\u7528\u5916\u7f6e\u7684Servlet\u5bb9\u5668"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"1.%20%E9%85%8D%E7%BD%AE%E5%B5%8C%E5%85%A5%E5%BC%8F%E6%9C%8D%E5%8A%A1%E5%99%A8-toc\" style=\"margin-left:0px\">1. \u914d\u7f6e\u5d4c\u5165\u5f0f\u670d\u52a1\u5668<\/p>\n<p id=\"1.1%20%E5%A6%82%E4%BD%95%E5%AE%9A%E5%88%B6%E5%92%8C%E4%BF%AE%E6%94%B9Servlet%E5%AE%B9%E5%99%A8%E7%9A%84%E7%9B%B8%E5%85%B3%E9%85%8D%E7%BD%AE-toc\" style=\"margin-left:40px\">1.1 \u5982\u4f55\u5b9a\u5236\u548c\u4fee\u6539Servlet\u5bb9\u5668\u7684\u76f8\u5173\u914d\u7f6e<\/p>\n<p id=\"1.server.port%3D8080-toc\" style=\"margin-left:80px\">1.server.port&#061;8080<\/p>\n<p id=\"2.%C2%A0server.context-path%3D%2Ftx-toc\" style=\"margin-left:80px\">2.\u00a0server.context-path&#061;\/tx<\/p>\n<p id=\"3.%C2%A0server.tomcat.uri-encoding%3DUTF-8-toc\" style=\"margin-left:80px\">3.\u00a0server.tomcat.uri-encoding&#061;UTF-8<\/p>\n<p id=\"1.2%C2%A0%E6%B3%A8%E5%86%8CServlet%E4%B8%89%E5%A4%A7%E7%BB%84%E4%BB%B6%E3%80%90Servlet%EF%BC%8CFilter%EF%BC%8CListener%E3%80%91-toc\" style=\"margin-left:40px\">1.2\u00a0\u6ce8\u518cServlet\u4e09\u5927\u7ec4\u4ef6\u3010Servlet&#xff0c;Filter&#xff0c;Listener\u3011<\/p>\n<p id=\"1.%C2%A0servlet-toc\" style=\"margin-left:80px\">1.\u00a0servlet<\/p>\n<p id=\"2.%C2%A0filter-toc\" style=\"margin-left:80px\">2.\u00a0filter<\/p>\n<p id=\"3.%20%E7%9B%91%E5%90%AC%E5%99%A8Listener-toc\" style=\"margin-left:80px\">3. \u76d1\u542c\u5668Listener<\/p>\n<p id=\"2.%20%E4%BD%BF%E7%94%A8%E5%A4%96%E7%BD%AE%E7%9A%84Servlet%E5%AE%B9%E5%99%A8-toc\" style=\"margin-left:0px\">2. \u4f7f\u7528\u5916\u7f6e\u7684Servlet\u5bb9\u5668<\/p>\n<p id=\"%E6%AD%A5%E9%AA%A4-toc\" style=\"margin-left:40px\">\u6b65\u9aa4<\/p>\n<p id=\"1%EF%BC%89%E3%80%81%E5%BF%85%E9%A1%BB%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AAwar%E9%A1%B9%E7%9B%AE%EF%BC%9B-toc\" style=\"margin-left:80px\">1&#xff09;\u5fc5\u987b\u521b\u5efa\u4e00\u4e2awar\u9879\u76ee&#xff1b;<\/p>\n<p id=\"2%EF%BC%89%E3%80%81%E5%B0%86%E5%B5%8C%E5%85%A5%E5%BC%8F%E7%9A%84Tomcat%E6%8C%87%E5%AE%9A%E4%B8%BAprovided%EF%BC%9B-toc\" style=\"margin-left:80px\">2&#xff09;\u5c06\u5d4c\u5165\u5f0f\u7684Tomcat\u6307\u5b9a\u4e3aprovided&#xff1b;<\/p>\n<p id=\"3%EF%BC%89%E9%85%8D%E7%BD%AE%E9%A1%B9%E7%9B%AE%E7%9A%84%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84-toc\" style=\"margin-left:80px\">3&#xff09;\u914d\u7f6e\u9879\u76ee\u7684\u76ee\u5f55\u7ed3\u6784<\/p>\n<p id=\"4%EF%BC%89%E9%83%A8%E7%BD%B2Tomcat-toc\" style=\"margin-left:80px\">4&#xff09;\u90e8\u7f72Tomcat<\/p>\n<p id=\"5%EF%BC%89%E5%BF%85%E9%A1%BB%E7%BC%96%E5%86%99%E4%B8%80%E4%B8%AASpringBootServletInitializer%E7%9A%84%E5%AD%90%E7%B1%BB%EF%BC%8C%E5%B9%B6%E8%B0%83%E7%94%A8configure%E6%96%B9%E6%B3%95-toc\" style=\"margin-left:80px\">5&#xff09;\u5fc5\u987b\u7f16\u5199\u4e00\u4e2aSpringBootServletInitializer\u7684\u5b50\u7c7b&#xff0c;\u5e76\u8c03\u7528configure\u65b9\u6cd5<\/p>\n<p id=\"6)%E5%90%AF%E5%8A%A8%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%B0%B1%E5%8F%AF%E4%BB%A5%E4%BD%BF%E7%94%A8%EF%BC%9B-toc\" style=\"margin-left:80px\">6)\u542f\u52a8\u670d\u52a1\u5668\u5c31\u53ef\u4ee5\u4f7f\u7528&#xff1b;<\/p>\n<hr id=\"hr-toc\" \/>\n<h2 id=\"1.%20%E9%85%8D%E7%BD%AE%E5%B5%8C%E5%85%A5%E5%BC%8F%E6%9C%8D%E5%8A%A1%E5%99%A8\">1. \u914d\u7f6e\u5d4c\u5165\u5f0f\u670d\u52a1\u5668<\/h2>\n<h3 id=\"1.1%20%E5%A6%82%E4%BD%95%E5%AE%9A%E5%88%B6%E5%92%8C%E4%BF%AE%E6%94%B9Servlet%E5%AE%B9%E5%99%A8%E7%9A%84%E7%9B%B8%E5%85%B3%E9%85%8D%E7%BD%AE\">1.1 \u5982\u4f55\u5b9a\u5236\u548c\u4fee\u6539Servlet\u5bb9\u5668\u7684\u76f8\u5173\u914d\u7f6e<\/h3>\n<p>\u4fee\u6539\u548cserver\u6709\u5173\u7684\u914d\u7f6e&#xff08;ServerProperties&#xff09;<\/p>\n<p>server.port&#061;8080<br \/>\nserver.context\u2010path&#061;\/tx<br \/>\nserver.tomcat.uri\u2010encoding&#061;UTF\u20108 <\/p>\n<h4 id=\"1.server.port%3D8080\">1.server.port&#061;8080<\/h4>\n<ul>\n<li>\u542b\u4e49&#xff1a;\u8be5\u914d\u7f6e\u9879\u7528\u4e8e\u6307\u5b9a Spring Boot \u5e94\u7528\u542f\u52a8\u65f6\u6240\u76d1\u542c\u7684\u7aef\u53e3\u53f7\u3002\u5728 Web \u5e94\u7528\u4e2d&#xff0c;\u670d\u52a1\u5668\u9700\u8981\u76d1\u542c\u4e00\u4e2a\u7279\u5b9a\u7684\u7aef\u53e3\u6765\u63a5\u6536\u5ba2\u6237\u7aef\u7684 HTTP \u8bf7\u6c42&#xff0c;\u8fd9\u91cc\u5c06\u7aef\u53e3\u53f7\u8bbe\u7f6e\u4e3a\u00a08080\u3002<\/li>\n<li>\u793a\u4f8b\u8bf4\u660e&#xff1a;\u5f53\u542f\u52a8\u5e94\u7528\u540e&#xff0c;\u5728\u6d4f\u89c8\u5668\u4e2d\u8f93\u5165\u00a0http:\/\/localhost:8080\u00a0&#xff08;\u5047\u8bbe\u5e94\u7528\u8fd0\u884c\u5728\u672c\u5730&#xff09;&#xff0c;\u5c31\u53ef\u4ee5\u8bbf\u95ee\u8be5\u5e94\u7528\u3002\u5982\u679c\u8be5\u7aef\u53e3\u5df2\u88ab\u5176\u4ed6\u5e94\u7528\u5360\u7528&#xff0c;Spring Boot \u5e94\u7528\u542f\u52a8\u65f6\u4f1a\u62a5\u9519&#xff0c;\u4f60\u9700\u8981\u4fee\u6539\u6b64\u7aef\u53e3\u53f7\u4e3a\u5176\u4ed6\u672a\u88ab\u5360\u7528\u7684\u503c&#xff0c;\u5982\u00a08081\u30019090\u00a0\u7b49\u3002<\/li>\n<\/ul>\n<h4 id=\"2.%C2%A0server.context-path%3D%2Ftx\">2.\u00a0server.context-path&#061;\/tx<\/h4>\n<ul>\n<li>\u542b\u4e49&#xff1a;\u6b64\u914d\u7f6e\u9879\u7528\u4e8e\u8bbe\u7f6e\u5e94\u7528\u7684\u4e0a\u4e0b\u6587\u8def\u5f84\u3002\u4e0a\u4e0b\u6587\u8def\u5f84\u662f URL \u4e2d\u4f4d\u4e8e\u4e3b\u673a\u540d\u548c\u7aef\u53e3\u53f7\u4e4b\u540e\u3001\u5177\u4f53\u8bf7\u6c42\u8def\u5f84\u4e4b\u524d\u7684\u90e8\u5206&#xff0c;\u5b83\u76f8\u5f53\u4e8e\u4e3a\u5e94\u7528\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u8bbf\u95ee\u524d\u7f00\u3002<\/li>\n<li>\u793a\u4f8b\u8bf4\u660e&#xff1a;\u82e5\u914d\u7f6e\u4e86\u00a0server.context-path&#061;\/tx&#xff0c;\u5f53\u5e94\u7528\u542f\u52a8\u540e&#xff0c;\u8981\u8bbf\u95ee\u5e94\u7528\u4e2d\u7684\u8d44\u6e90&#xff0c;URL \u5c31\u9700\u8981\u52a0\u4e0a\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u8def\u5f84\u3002\u4f8b\u5982&#xff0c;\u539f\u672c\u8bbf\u95ee\u5e94\u7528\u9996\u9875\u7684 URL \u662f\u00a0http:\/\/localhost:8080&#xff0c;\u73b0\u5728\u5219\u53d8\u4e3a\u00a0http:\/\/localhost:8080\/tx\u3002\u5982\u679c\u8981\u8bbf\u95ee\u67d0\u4e2a\u63a7\u5236\u5668\u4e2d\u6620\u5c04\u8def\u5f84\u4e3a\u00a0\/hello\u00a0\u7684\u65b9\u6cd5&#xff0c;\u5b8c\u6574\u7684 URL \u5c31\u662f\u00a0http:\/\/localhost:8080\/tx\/hello\u3002<\/li>\n<\/ul>\n<h4 id=\"3.%C2%A0server.tomcat.uri-encoding%3DUTF-8\">3.\u00a0server.tomcat.uri-encoding&#061;UTF-8<\/h4>\n<ul>\n<li>\u542b\u4e49&#xff1a;\u8be5\u914d\u7f6e\u9879\u7528\u4e8e\u6307\u5b9a Tomcat \u670d\u52a1\u5668\u5728\u5904\u7406\u8bf7\u6c42 URI \u65f6\u6240\u4f7f\u7528\u7684\u5b57\u7b26\u7f16\u7801\u3002\u5b57\u7b26\u7f16\u7801\u51b3\u5b9a\u4e86\u5982\u4f55\u5c06\u5b57\u8282\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u636e&#xff0c;\u8bbe\u7f6e\u4e3a\u00a0UTF-8\u00a0\u53ef\u4ee5\u786e\u4fdd\u5728\u5904\u7406\u5305\u542b\u4e2d\u6587\u3001\u7279\u6b8a\u7b26\u53f7\u7b49\u591a\u8bed\u8a00\u5b57\u7b26\u7684 URI \u65f6\u4e0d\u4f1a\u51fa\u73b0\u4e71\u7801\u95ee\u9898\u3002<\/li>\n<li>\u793a\u4f8b\u8bf4\u660e&#xff1a;\u5f53\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u8bf7\u6c42 URI \u4e2d\u5305\u542b\u4e2d\u6587\u53c2\u6570&#xff0c;\u5982\u00a0http:\/\/localhost:8080\/tx\/search?keyword&#061;\u4f60\u597d&#xff0c;\u670d\u52a1\u5668\u4f1a\u6309\u7167\u00a0UTF-8\u00a0\u7f16\u7801\u6765\u89e3\u6790\u8fd9\u4e2a URI&#xff0c;\u4ece\u800c\u6b63\u786e\u83b7\u53d6\u5230\u00a0keyword\u00a0\u53c2\u6570\u7684\u503c\u4e3a\u00a0\u4f60\u597d\u3002\u5982\u679c\u4e0d\u8bbe\u7f6e\u8fd9\u4e2a\u7f16\u7801&#xff0c;\u53ef\u80fd\u4f1a\u5bfc\u81f4\u89e3\u6790\u540e\u7684\u53c2\u6570\u503c\u51fa\u73b0\u4e71\u7801\u3002<\/li>\n<\/ul>\n<h3 id=\"1.2%C2%A0%E6%B3%A8%E5%86%8CServlet%E4%B8%89%E5%A4%A7%E7%BB%84%E4%BB%B6%E3%80%90Servlet%EF%BC%8CFilter%EF%BC%8CListener%E3%80%91\">1.2\u00a0\u6ce8\u518cServlet\u4e09\u5927\u7ec4\u4ef6\u3010Servlet&#xff0c;Filter&#xff0c;Listener\u3011<\/h3>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7531\u4e8eSpringBoot\u9ed8\u8ba4\u662f\u4ee5jar\u5305\u7684\u65b9\u5f0f\u542f\u52a8\u5d4c\u5165\u5f0f\u7684Servlet\u5bb9\u5668\u6765\u542f\u52a8SpringBoot\u7684web\u5e94\u7528&#xff0c;\u6ca1\u6709web.xml\u6587\u4ef6\u3002<\/p>\n<h4 id=\"1.%C2%A0servlet\">1.\u00a0servlet<\/h4>\n<p>\/\/\u6ce8\u518c\u4e09\u5927\u7ec4\u4ef6<br \/>\n&#064;Bean<br \/>\npublic ServletRegistrationBean myServlet(){<br \/>\n    ServletRegistrationBean registrationBean &#061; new ServletRegistrationBean(new<br \/>\n            MyServlet(),&#034;\/myServlet&#034;);<br \/>\n    return registrationBean;<br \/>\n}<br \/>\npackage com.qcby.servlet;<\/p>\n<p>import javax.servlet.ServletException;<br \/>\nimport javax.servlet.http.HttpServlet;<br \/>\nimport javax.servlet.http.HttpServletRequest;<br \/>\nimport javax.servlet.http.HttpServletResponse;<br \/>\nimport java.io.IOException;<br \/>\nimport java.io.PrintWriter;<\/p>\n<p>public class MyServlet extends HttpServlet{<br \/>\n    \/\/ \u91cd\u5199 doGet \u65b9\u6cd5\u6765\u5904\u7406 GET \u8bf7\u6c42<br \/>\n    &#064;Override<br \/>\n    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {<br \/>\n        \/\/ \u8bbe\u7f6e\u54cd\u5e94\u5185\u5bb9\u7c7b\u578b<br \/>\n        resp.setContentType(&#034;text\/html;charset&#061;UTF-8&#034;);<br \/>\n        \/\/ \u83b7\u53d6\u54cd\u5e94\u8f93\u51fa\u6d41<br \/>\n        PrintWriter out &#061; resp.getWriter();<br \/>\n        \/\/ \u8f93\u51fa\u54cd\u5e94\u5185\u5bb9<br \/>\n        out.println(&#034;&lt;html&gt;&#034;);<br \/>\n        out.println(&#034;&lt;head&gt;&lt;title&gt;My Servlet&lt;\/title&gt;&lt;\/head&gt;&#034;);<br \/>\n        out.println(&#034;&lt;body&gt;&#034;);<br \/>\n        out.println(&#034;&lt;h1&gt;\u8fd9\u662f\u81ea\u5b9a\u4e49 Servlet \u7684\u54cd\u5e94\u5185\u5bb9&lt;\/h1&gt;&#034;);<br \/>\n        out.println(&#034;&lt;\/body&gt;&#034;);<br \/>\n        out.println(&#034;&lt;\/html&gt;&#034;);<br \/>\n    }<\/p>\n<p>    \/\/ \u91cd\u5199 doPost \u65b9\u6cd5\u6765\u5904\u7406 POST \u8bf7\u6c42<br \/>\n    &#064;Override<br \/>\n    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {<br \/>\n        doGet(req, resp);<br \/>\n    }<br \/>\n} <\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u81ea\u5df1\u5199MyServlet\u7c7b\u7ee7\u627fHttpServlet\u7c7b\u6765\u5b9e\u73b0\u4e00\u4e2a\u6807\u51c6\u7684 Servlet&#xff0c;\u5e76\u4e14\u8981\u91cd\u5199doGet\u00a0\u6216\u00a0doPost\u00a0\u7b49\u65b9\u6cd5\u6765\u5904\u7406 HTTP \u8bf7\u6c42<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"177\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145220-680267242b746.png\" width=\"984\" \/><\/p>\n<h4 id=\"2.%C2%A0filter\">2.\u00a0filter<\/h4>\n<p>&#064;Bean<br \/>\npublic FilterRegistrationBean myFilter(){<br \/>\n    FilterRegistrationBean registrationBean &#061; new FilterRegistrationBean();<br \/>\n    registrationBean.setFilter(new MyFilter());<br \/>\n    registrationBean.setUrlPatterns(Arrays.asList(&#034;\/hello&#034;,&#034;\/myServlet&#034;));<br \/>\n    return registrationBean;<br \/>\n} <\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0MyFilter\u00a0\u7c7b\u9700\u8981\u5b9e\u73b0\u00a0javax.servlet.Filter\u00a0\u63a5\u53e3&#xff0c;\u5e76\u91cd\u5199\u63a5\u53e3\u4e2d\u7684\u00a0init\u3001doFilter\u00a0\u548c\u00a0destroy\u00a0\u65b9\u6cd5&#xff0c;\u8fd9\u6837\u624d\u80fd\u6210\u4e3a\u4e00\u4e2a\u6807\u51c6\u7684\u8fc7\u6ee4\u5668\u3002<\/p>\n<p>package com.qcby.servlet;<\/p>\n<p>import javax.servlet.*;<br \/>\nimport java.io.IOException;<\/p>\n<p>public class MyFilter implements Filter {<\/p>\n<p>    \/\/ \u8fc7\u6ee4\u5668\u521d\u59cb\u5316\u65b9\u6cd5&#xff0c;\u5728\u8fc7\u6ee4\u5668\u521b\u5efa\u65f6\u8c03\u7528<br \/>\n    &#064;Override<br \/>\n    public void init(FilterConfig filterConfig) throws ServletException {<br \/>\n        \/\/ \u53ef\u4ee5\u5728\u8fd9\u91cc\u8fdb\u884c\u4e00\u4e9b\u521d\u59cb\u5316\u64cd\u4f5c&#xff0c;\u4f8b\u5982\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u7b49<br \/>\n        System.out.println(&#034;MyFilter \u521d\u59cb\u5316&#034;);<br \/>\n    }<\/p>\n<p>    \/\/ \u8fc7\u6ee4\u65b9\u6cd5&#xff0c;\u6bcf\u6b21\u8bf7\u6c42\u5339\u914d\u5230\u8be5\u8fc7\u6ee4\u5668\u65f6\u90fd\u4f1a\u8c03\u7528<br \/>\n    &#064;Override<br \/>\n    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)<br \/>\n            throws IOException, ServletException {<br \/>\n        \/\/ \u5728\u8bf7\u6c42\u5904\u7406\u4e4b\u524d\u53ef\u4ee5\u8fdb\u884c\u4e00\u4e9b\u9884\u5904\u7406\u64cd\u4f5c&#xff0c;\u4f8b\u5982\u8bbe\u7f6e\u5b57\u7b26\u7f16\u7801<br \/>\n        request.setCharacterEncoding(&#034;UTF-8&#034;);<br \/>\n        response.setCharacterEncoding(&#034;UTF-8&#034;);<\/p>\n<p>        System.out.println(&#034;MyFilter \u5f00\u59cb\u8fc7\u6ee4\u8bf7\u6c42&#034;);<\/p>\n<p>        \/\/ \u8c03\u7528 FilterChain \u7684 doFilter \u65b9\u6cd5\u5c06\u8bf7\u6c42\u4f20\u9012\u7ed9\u4e0b\u4e00\u4e2a\u8fc7\u6ee4\u5668\u6216\u76ee\u6807 Servlet<br \/>\n        chain.doFilter(request, response);<\/p>\n<p>        System.out.println(&#034;MyFilter \u5904\u7406\u5b8c\u54cd\u5e94&#034;);<br \/>\n    }<\/p>\n<p>    \/\/ \u8fc7\u6ee4\u5668\u9500\u6bc1\u65b9\u6cd5&#xff0c;\u5728\u8fc7\u6ee4\u5668\u9500\u6bc1\u65f6\u8c03\u7528<br \/>\n    &#064;Override<br \/>\n    public void destroy() {<br \/>\n        \/\/ \u53ef\u4ee5\u5728\u8fd9\u91cc\u8fdb\u884c\u4e00\u4e9b\u8d44\u6e90\u91ca\u653e\u64cd\u4f5c&#xff0c;\u4f8b\u5982\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u7b49<br \/>\n        System.out.println(&#034;MyFilter \u9500\u6bc1&#034;);<br \/>\n    }<br \/>\n} <\/p>\n<h4 id=\"3.%20%E7%9B%91%E5%90%AC%E5%99%A8Listener\" style=\"background-color:transparent\">3. \u76d1\u542c\u5668Listener<\/h4>\n<p>\u76d1\u542c\u5668\u4f5c\u7528&#xff1a;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5728 Spring Boot \u5e94\u7528\u542f\u52a8\u671f\u95f4&#xff0c;\u6709\u591a\u4e2a\u5173\u952e\u9636\u6bb5&#xff0c;\u4f8b\u5982\u4e0a\u4e0b\u6587\u5237\u65b0\u524d\u3001\u5237\u65b0\u540e\u7b49\u3002\u901a\u8fc7\u81ea\u5b9a\u4e49\u76d1\u542c\u5668\u76d1\u542c\u8fd9\u4e9b\u4e8b\u4ef6&#xff0c;\u4f60\u53ef\u4ee5\u5728\u7279\u5b9a\u9636\u6bb5\u6267\u884c<span style=\"color:#ed7976\">\u521d\u59cb\u5316\u64cd\u4f5c<\/span>&#xff0c;\u50cf\u52a0\u8f7d\u914d\u7f6e\u3001\u521d\u59cb\u5316\u7f13\u5b58\u7b49\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5f53\u5e94\u7528\u5173\u95ed\u65f6&#xff0c;\u4f60\u53ef\u4ee5\u76d1\u542c\u76f8\u5e94\u4e8b\u4ef6&#xff0c;\u8fdb\u884c\u8d44\u6e90\u91ca\u653e\u3001\u6570\u636e\u4fdd\u5b58\u7b49\u6536\u5c3e\u5de5\u4f5c\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u501f\u52a9\u76d1\u542c\u5668&#xff0c;\u4f60\u53ef\u4ee5\u5b9e\u73b0\u4e8b\u4ef6\u9a71\u52a8\u67b6\u6784&#xff0c;\u628a\u4e0d\u540c\u4e1a\u52a1\u903b\u8f91\u89e3\u8026\u3002\u4f8b\u5982&#xff0c;\u5f53\u7528\u6237\u6ce8\u518c\u6210\u529f\u540e&#xff0c;\u89e6\u53d1\u4e00\u4e2a\u7528\u6237\u6ce8\u518c\u4e8b\u4ef6&#xff0c;\u5176\u4ed6\u76d1\u542c\u5668\u53ef\u4ee5\u76d1\u542c\u8be5\u4e8b\u4ef6\u5e76\u6267\u884c\u76f8\u5e94\u64cd\u4f5c&#xff0c;\u5982\u53d1\u9001\u6b22\u8fce\u90ae\u4ef6\u3001\u6dfb\u52a0\u79ef\u5206\u7b49\u3002<\/p>\n<p>&#064;Bean<br \/>\npublic ServletListenerRegistrationBean myListener(){<br \/>\n    ServletListenerRegistrationBean&lt;MyListener&gt; registrationBean &#061; new<br \/>\n            ServletListenerRegistrationBean&lt;&gt;(new MyListener());<br \/>\n    return registrationBean;<br \/>\n} <\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0MyListener\u00a0\u7c7b\u9700\u8981\u5b9e\u73b0 Servlet \u76d1\u542c\u5668\u63a5\u53e3&#xff0c;\u8fd9\u6837\u624d\u80fd\u53d1\u6325\u76d1\u542c\u5668\u7684\u4f5c\u7528\u3002Servlet \u76d1\u542c\u5668\u6709\u591a\u79cd\u7c7b\u578b&#xff0c;\u5e38\u89c1\u7684\u6709\u00a0ServletContextListener&#xff08;\u7528\u4e8e\u76d1\u542c\u00a0ServletContext\u00a0\u7684\u521b\u5efa\u548c\u9500\u6bc1&#xff09;\u3001HttpSessionListener&#xff08;\u7528\u4e8e\u76d1\u542c\u00a0HttpSession\u00a0\u7684\u521b\u5efa\u548c\u9500\u6bc1&#xff09;\u3001ServletRequestListener&#xff08;\u7528\u4e8e\u76d1\u542c\u00a0ServletRequest\u00a0\u7684\u521b\u5efa\u548c\u9500\u6bc1&#xff09;\u7b49<\/p>\n<p>package com.qcby.servlet;<\/p>\n<p>import javax.servlet.ServletContextEvent;<br \/>\nimport javax.servlet.ServletContextListener;<\/p>\n<p>public class MyListener implements ServletContextListener {<\/p>\n<p>    \/\/ \u5f53 ServletContext \u88ab\u521b\u5efa\u65f6\u8c03\u7528<br \/>\n    &#064;Override<br \/>\n    public void contextInitialized(ServletContextEvent sce) {<br \/>\n        \/\/ \u53ef\u4ee5\u5728\u8fd9\u91cc\u8fdb\u884c\u4e00\u4e9b\u521d\u59cb\u5316\u64cd\u4f5c&#xff0c;\u4f8b\u5982\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\u3001\u521d\u59cb\u5316\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u7b49<br \/>\n        System.out.println(&#034;ServletContext \u521d\u59cb\u5316&#xff0c;\u5e94\u7528\u542f\u52a8&#034;);<br \/>\n    }<\/p>\n<p>    \/\/ \u5f53 ServletContext \u88ab\u9500\u6bc1\u65f6\u8c03\u7528<br \/>\n    &#064;Override<br \/>\n    public void contextDestroyed(ServletContextEvent sce) {<br \/>\n        \/\/ \u53ef\u4ee5\u5728\u8fd9\u91cc\u8fdb\u884c\u4e00\u4e9b\u8d44\u6e90\u91ca\u653e\u64cd\u4f5c&#xff0c;\u4f8b\u5982\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u7b49<br \/>\n        System.out.println(&#034;ServletContext \u9500\u6bc1&#xff0c;\u5e94\u7528\u5173\u95ed&#034;);<br \/>\n    }<br \/>\n} <\/p>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\">contextInitialized\u00a0\u65b9\u6cd5&#xff1a;\u5f53\u00a0<span style=\"color:#000000\">ServletContext<\/span>\u00a0\u88ab\u521b\u5efa\u65f6\u8c03\u7528&#xff0c;\u901a\u5e38\u7528\u4e8e\u6267\u884c\u4e00\u4e9b\u521d\u59cb\u5316\u64cd\u4f5c&#xff0c;\u5982\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\u3001\u521d\u59cb\u5316\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u7b49\u3002<\/li>\n<li style=\"text-align:left\">contextDestroyed\u00a0\u65b9\u6cd5&#xff1a;\u5f53\u00a0<span style=\"color:#000000\">ServletContext<\/span>\u00a0\u88ab\u9500\u6bc1\u65f6\u8c03\u7528&#xff0c;\u901a\u5e38\u7528\u4e8e\u6267\u884c\u4e00\u4e9b\u8d44\u6e90\u91ca\u653e\u64cd\u4f5c&#xff0c;\u5982\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u7b49\u3002<\/li>\n<\/ul>\n<p> <span style=\"color:#000000\"><span style=\"background-color:#ffffff\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5c06\u00a0<\/span><\/span>MyListener<br \/>\n <span style=\"color:#000000\"><span style=\"background-color:#ffffff\">\u00a0\u6ce8\u518c\u4e3a\u76d1\u542c\u5668\u3002\u8fd9\u6837&#xff0c;\u5f53\u5e94\u7528\u542f\u52a8\u548c\u5173\u95ed\u65f6&#xff0c;<\/span><\/span>MyListener<br \/>\n <span style=\"color:#000000\"><span style=\"background-color:#ffffff\">\u00a0\u4e2d\u7684\u00a0<\/span><\/span>contextInitialized<br \/>\n <span style=\"color:#000000\"><span style=\"background-color:#ffffff\">\u00a0\u548c\u00a0<\/span><\/span>contextDestroyed<br \/>\n <span style=\"color:#000000\"><span style=\"background-color:#ffffff\">\u00a0\u65b9\u6cd5\u4f1a\u76f8\u5e94\u5730\u88ab\u8c03\u7528\u3002<\/span><\/span> <\/p>\n<p> <span style=\"color:#333333\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0SpringBoot\u5e2e\u6211\u4eec\u81ea\u52a8SpringMVC\u7684\u65f6\u5019&#xff0c;\u81ea\u52a8\u7684\u6ce8\u518cSpringMVC\u7684\u524d\u7aef\u63a7\u5236\u5668DispatcherServlet&#xff1b;<\/span> <\/p>\n<p> <span style=\"color:#333333\">DispatcherServletAutoConfiguration\u4e2d&#xff1a;<\/span> <\/p>\n<p> <img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"787\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145220-6802672446f08.png\" width=\"1248\" \/> <\/p>\n<hr \/>\n<h2 id=\"2.%20%E4%BD%BF%E7%94%A8%E5%A4%96%E7%BD%AE%E7%9A%84Servlet%E5%AE%B9%E5%99%A8\">2. \u4f7f\u7528\u5916\u7f6e\u7684Servlet\u5bb9\u5668<\/h2>\n<p>\u5d4c\u5165\u5f0fServlet\u5bb9\u5668&#xff1a;\u5e94\u7528\u6253\u6210\u53ef\u6267\u884c\u7684jar<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4f18\u70b9&#xff1a;\u7b80\u5355\u3001\u4fbf\u643a&#xff1b;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7f3a\u70b9&#xff1a;\u9ed8\u8ba4\u4e0d\u652f\u6301JSP\u3001\u4f18\u5316\u5b9a\u5236\u6bd4\u8f83\u590d\u6742.&#xff1b;<\/p>\n<p>\u5916\u7f6e\u7684Servlet\u5bb9\u5668&#xff1a;\u5916\u9762\u5b89\u88c5Tomcat&#8212;\u5e94\u7528war\u5305\u7684\u65b9\u5f0f\u6253\u5305&#xff1b;<\/p>\n<h3 id=\"%E6%AD%A5%E9%AA%A4\">\u6b65\u9aa4<\/h3>\n<h4 id=\"1%EF%BC%89%E3%80%81%E5%BF%85%E9%A1%BB%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AAwar%E9%A1%B9%E7%9B%AE%EF%BC%9B\">1&#xff09;\u5fc5\u987b\u521b\u5efa\u4e00\u4e2awar\u9879\u76ee&#xff1b;<\/h4>\n<p>jar\u5305\u548cwar\u5305&#xff1a;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0springboot\u7684jar\u5305\u662f\u4e00\u4e2a<span style=\"color:#ed7976\">\u53ef\u6267\u884c\u7684\u5f52\u6863\u6587\u4ef6<\/span>&#xff0c;\u5b83\u9075\u5faa \u201c\u80d6 JAR\u201d&#xff08;Fat JAR&#xff09;\u7684\u7406\u5ff5&#xff0c;\u5373\u5c06\u5e94\u7528\u7a0b\u5e8f\u4ee3\u7801\u3001\u4f9d\u8d56\u7684\u6240\u6709\u5e93&#xff08;\u5305\u62ec Spring Boot \u81ea\u8eab\u7684\u5e93&#xff09;\u4ee5\u53ca<span style=\"color:#6eaad7\">\u5d4c\u5165\u5f0f\u670d\u52a1\u5668<\/span>&#xff08;\u5982 Tomcat\u3001Jetty \u7b49&#xff09;\u90fd\u6253\u5305\u5728\u4e00\u4e2a JAR \u6587\u4ef6\u4e2d\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WAR&#xff08;Web Application Archive&#xff09;\u5305\u662f\u4f20\u7edf\u7684<span style=\"color:#ed7976\"> Web \u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u683c\u5f0f<\/span>&#xff0c;\u4e3b\u8981\u7528\u4e8e\u90e8\u7f72\u5230<span style=\"color:#6eaad7\">\u5916\u90e8\u7684 Servlet \u5bb9\u5668<\/span>&#xff08;\u5982 Tomcat\u3001WebLogic \u7b49&#xff09;\u4e2d\u3002\u5b83\u7684\u7ed3\u6784\u9075\u5faa\u6807\u51c6\u7684 Web \u5e94\u7528\u7a0b\u5e8f\u76ee\u5f55\u7ed3\u6784&#xff0c;\u4e0d\u5305\u542b\u5d4c\u5165\u5f0f\u670d\u52a1\u5668\u3002<\/p>\n<p>&#xff08;\u5229\u7528idea\u521b\u5efa\u597d\u76ee\u5f55\u7ed3\u6784&#xff09;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"885\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145220-68026724a01b2.png\" width=\"1251\" \/><\/p>\n<h4 id=\"2%EF%BC%89%E3%80%81%E5%B0%86%E5%B5%8C%E5%85%A5%E5%BC%8F%E7%9A%84Tomcat%E6%8C%87%E5%AE%9A%E4%B8%BAprovided%EF%BC%9B\">2&#xff09;\u5c06\u5d4c\u5165\u5f0f\u7684Tomcat\u6307\u5b9a\u4e3aprovided&#xff1b;<\/h4>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5982\u679c\u8981\u5c06 Spring Boot \u9879\u76ee\u6253\u5305\u6210 WAR \u5305&#xff0c;\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u989d\u5916\u7684\u914d\u7f6e\u3002\u4ee5 Maven \u4e3a\u4f8b&#xff0c;\u9700\u8981\u5728pom.xml\u4e2d\u8fdb\u884c\u5982\u4e0b\u914d\u7f6e&#xff1a;<\/p>\n<p> &lt;packaging&gt;war&lt;\/packaging&gt;<\/p>\n<p>&lt;dependencies&gt;<br \/>\n    &lt;!&#8211; \u6392\u9664\u5d4c\u5165\u5f0fTomcat\u4f9d\u8d56 &#8211;&gt;<br \/>\n    &lt;dependency&gt;<br \/>\n        &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br \/>\n        &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;<br \/>\n        &lt;exclusions&gt;<br \/>\n            &lt;exclusion&gt;<br \/>\n                &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br \/>\n                &lt;artifactId&gt;spring-boot-starter-tomcat&lt;\/artifactId&gt;<br \/>\n            &lt;\/exclusion&gt;<br \/>\n        &lt;\/exclusions&gt;<br \/>\n    &lt;\/dependency&gt; <\/p>\n<p>&lt;dependency&gt;<br \/>\n   &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br \/>\n   &lt;artifactId&gt;spring-boot-starter-tomcat&lt;\/artifactId&gt;<br \/>\n   &lt;scope&gt;provided&lt;\/scope&gt;<br \/>\n&lt;\/dependency&gt; <\/p>\n<h4 id=\"3%EF%BC%89%E9%85%8D%E7%BD%AE%E9%A1%B9%E7%9B%AE%E7%9A%84%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84\">3&#xff09;\u914d\u7f6e\u9879\u76ee\u7684\u76ee\u5f55\u7ed3\u6784<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"336\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145221-680267250c122.png\" width=\"630\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1009\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145221-6802672523f52.png\" width=\"1540\" \/><\/p>\n<p>\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"926\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145221-68026725cb50c.png\" width=\"1146\" \/><\/p>\n<p>\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"640\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145222-680267263f5ea.png\" width=\"1180\" \/><\/p>\n<p>\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"744\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145222-680267268be9e.png\" width=\"520\" \/><\/p>\n<p>\u6216\u8005\u5982\u679c\u5728Maven\u9879\u76ee\u91cc&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u521b\u5efa\u8fd9\u4e09\u4e2a\u5305\u548cxml\u6587\u4ef6&#xff0c;\u7136\u540e\u518d\u6253\u5f00\u521a\u624d\u90a3\u4e2a\u4f4d\u7f6e&#xff08;\u914d\u7f6e\u76ee\u5f55\u7ed3\u6784&#xff09;\u914d\u7f6e<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"741\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145222-68026726a6bf3.png\" width=\"1115\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"546\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145223-680267270faaf.png\" width=\"693\" \/>\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"495\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145223-680267272e985.png\" width=\"753\" \/><\/p>\n<h4 id=\"4%EF%BC%89%E9%83%A8%E7%BD%B2Tomcat\">4&#xff09;\u90e8\u7f72Tomcat<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"296\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145223-680267274bb1f.png\" width=\"591\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"977\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145223-6802672762a03.png\" width=\"1527\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"388\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145223-68026727cf057.png\" width=\"1496\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"476\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145224-68026728129ef.png\" width=\"1257\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"737\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145224-6802672850c56.png\" width=\"705\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"459\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145224-6802672870627.png\" width=\"1122\" \/><\/p>\n<h4 id=\"5%EF%BC%89%E5%BF%85%E9%A1%BB%E7%BC%96%E5%86%99%E4%B8%80%E4%B8%AASpringBootServletInitializer%E7%9A%84%E5%AD%90%E7%B1%BB%EF%BC%8C%E5%B9%B6%E8%B0%83%E7%94%A8configure%E6%96%B9%E6%B3%95\">5&#xff09;\u5fc5\u987b\u7f16\u5199\u4e00\u4e2aSpringBootServletInitializer\u7684\u5b50\u7c7b&#xff0c;\u5e76\u8c03\u7528configure\u65b9\u6cd5<\/h4>\n<p>public class ServletInitializer extends SpringBootServletInitializer {<\/p>\n<p>   &#064;Override<br \/>\n   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {<br \/>\n      \/\/\u4f20\u5165SpringBoot\u5e94\u7528\u7684\u4e3b\u7a0b\u5e8f<br \/>\n      return application.sources(RomateservletApplication.class);<br \/>\n   }<\/p>\n<p>} <\/p>\n<h4 id=\"6)%E5%90%AF%E5%8A%A8%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%B0%B1%E5%8F%AF%E4%BB%A5%E4%BD%BF%E7%94%A8%EF%BC%9B\">6)\u542f\u52a8\u670d\u52a1\u5668\u5c31\u53ef\u4ee5\u4f7f\u7528&#xff1b;<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1158\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145224-68026728aa11b.png\" width=\"2200\" \/><\/p>\n<p><span style=\"color:#ed7976\">\u539f\u7406<\/span><\/p>\n<p>\u00a0\u00a0<span style=\"color:#6eaad7\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0jar\u5305&#xff1a;<\/span><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6267\u884cSpringBoot\u4e3b\u7c7b\u7684main\u65b9\u6cd5&#xff0c;\u542f\u52a8ioc\u5bb9\u5668&#xff0c;\u521b\u5efa\u5d4c\u5165\u5f0f\u7684Servlet\u5bb9\u5668&#xff1b;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color:#6eaad7\">\u00a0\u00a0war\u5305&#xff1a;<\/span><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u542f\u52a8\u670d\u52a1\u5668&#xff0c;\u670d\u52a1\u5668\u542f\u52a8SpringBoot\u5e94\u7528\u3010SpringBootServletInitializer\u3011&#xff0c;\u542f\u52a8ioc\u5bb9\u5668&#xff1b;<\/p>\n<\/p>\n<p>\u5728yml\u914d\u7f6e\u6587\u4ef6\u91cc&#xff0c;\u8bbe\u7f6e\u89c6\u56fe\u89e3\u6790\u5668\u524d\u7f00&#xff0c;\u540e\u7f00<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"303\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145225-68026729d3042.png\" width=\"534\" \/><\/p>\n<p>\u5916\u7f6e\u7684Servlet\u5bb9\u5668\u662f\u8bbf\u95eejsp\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf13\u6b21\u3002Servlet \u76d1\u542c\u5668\u6709\u591a\u79cd\u7c7b\u578b\uff0c\u5e38\u89c1\u7684\u6709 ServletContextListener\uff08\u7528\u4e8e\u76d1\u542c ServletContext \u7684\u521b\u5efa\u548c\u9500\u6bc1\uff09\u3001HttpSessionListener\uff08\u7528\u4e8e\u76d1\u542c HttpSession \u7684\u521b\u5efa\u548c\u9500\u6bc1\uff09\u3001ServletRequestListener\uff08\u7528\u4e8e\u76d1\u542c ServletRequest \u7684\u521b\u5efa\u548c\u9500\u6bc1\uff09\u7b49\u3002\u7531\u4e8eSpringBoot\u9ed8\u8ba4\u662f\u4ee5jar\u5305\u7684\u65b9\u5f0f\u542f\u52a8\u5d4c\u5165\u5f0f\u7684Servlet\u5bb9\u5668\u6765\u542f\u52a8SpringBoot\u7684web\u5e94\u7528\uff0c\u6ca1\u6709web.xml\u6587\u4ef6\u3002\u8fd9\u6837\uff0c\u5f53\u5e94\u7528\u542f\u52a8\u548c\u5173\u95ed\u65f6\uff0c<\/p>\n","protected":false},"author":2,"featured_media":16401,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[1133,98,43],"topic":[],"class_list":["post-16420","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-servlet","tag-spring-boot","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SpringBoot\u7b2c\u4e09\u7ad9\uff084\uff09\uff1a\u914d\u7f6e\u5d4c\u5165\u5f0f\u670d\u52a1\u5668&amp;\u4f7f\u7528\u5916\u7f6e\u7684Servlet\u5bb9\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/16420.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SpringBoot\u7b2c\u4e09\u7ad9\uff084\uff09\uff1a\u914d\u7f6e\u5d4c\u5165\u5f0f\u670d\u52a1\u5668&amp;\u4f7f\u7528\u5916\u7f6e\u7684Servlet\u5bb9\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf13\u6b21\u3002Servlet \u76d1\u542c\u5668\u6709\u591a\u79cd\u7c7b\u578b\uff0c\u5e38\u89c1\u7684\u6709 ServletContextListener\uff08\u7528\u4e8e\u76d1\u542c ServletContext \u7684\u521b\u5efa\u548c\u9500\u6bc1\uff09\u3001HttpSessionListener\uff08\u7528\u4e8e\u76d1\u542c HttpSession \u7684\u521b\u5efa\u548c\u9500\u6bc1\uff09\u3001ServletRequestListener\uff08\u7528\u4e8e\u76d1\u542c ServletRequest \u7684\u521b\u5efa\u548c\u9500\u6bc1\uff09\u7b49\u3002\u7531\u4e8eSpringBoot\u9ed8\u8ba4\u662f\u4ee5jar\u5305\u7684\u65b9\u5f0f\u542f\u52a8\u5d4c\u5165\u5f0f\u7684Servlet\u5bb9\u5668\u6765\u542f\u52a8SpringBoot\u7684web\u5e94\u7528\uff0c\u6ca1\u6709web.xml\u6587\u4ef6\u3002\u8fd9\u6837\uff0c\u5f53\u5e94\u7528\u542f\u52a8\u548c\u5173\u95ed\u65f6\uff0c\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/16420.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T14:52:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145220-680267242b746.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/16420.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/16420.html\",\"name\":\"SpringBoot\u7b2c\u4e09\u7ad9\uff084\uff09\uff1a\u914d\u7f6e\u5d4c\u5165\u5f0f\u670d\u52a1\u5668&amp;\u4f7f\u7528\u5916\u7f6e\u7684Servlet\u5bb9\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T14:52:26+00:00\",\"dateModified\":\"2025-04-18T14:52:26+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/16420.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/16420.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/16420.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SpringBoot\u7b2c\u4e09\u7ad9\uff084\uff09\uff1a\u914d\u7f6e\u5d4c\u5165\u5f0f\u670d\u52a1\u5668&amp;\u4f7f\u7528\u5916\u7f6e\u7684Servlet\u5bb9\u5668\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SpringBoot\u7b2c\u4e09\u7ad9\uff084\uff09\uff1a\u914d\u7f6e\u5d4c\u5165\u5f0f\u670d\u52a1\u5668&amp;\u4f7f\u7528\u5916\u7f6e\u7684Servlet\u5bb9\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/16420.html","og_locale":"zh_CN","og_type":"article","og_title":"SpringBoot\u7b2c\u4e09\u7ad9\uff084\uff09\uff1a\u914d\u7f6e\u5d4c\u5165\u5f0f\u670d\u52a1\u5668&amp;\u4f7f\u7528\u5916\u7f6e\u7684Servlet\u5bb9\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf13\u6b21\u3002Servlet \u76d1\u542c\u5668\u6709\u591a\u79cd\u7c7b\u578b\uff0c\u5e38\u89c1\u7684\u6709 ServletContextListener\uff08\u7528\u4e8e\u76d1\u542c ServletContext \u7684\u521b\u5efa\u548c\u9500\u6bc1\uff09\u3001HttpSessionListener\uff08\u7528\u4e8e\u76d1\u542c HttpSession \u7684\u521b\u5efa\u548c\u9500\u6bc1\uff09\u3001ServletRequestListener\uff08\u7528\u4e8e\u76d1\u542c ServletRequest \u7684\u521b\u5efa\u548c\u9500\u6bc1\uff09\u7b49\u3002\u7531\u4e8eSpringBoot\u9ed8\u8ba4\u662f\u4ee5jar\u5305\u7684\u65b9\u5f0f\u542f\u52a8\u5d4c\u5165\u5f0f\u7684Servlet\u5bb9\u5668\u6765\u542f\u52a8SpringBoot\u7684web\u5e94\u7528\uff0c\u6ca1\u6709web.xml\u6587\u4ef6\u3002\u8fd9\u6837\uff0c\u5f53\u5e94\u7528\u542f\u52a8\u548c\u5173\u95ed\u65f6\uff0c","og_url":"https:\/\/www.wsisp.com\/helps\/16420.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T14:52:26+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418145220-680267242b746.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/16420.html","url":"https:\/\/www.wsisp.com\/helps\/16420.html","name":"SpringBoot\u7b2c\u4e09\u7ad9\uff084\uff09\uff1a\u914d\u7f6e\u5d4c\u5165\u5f0f\u670d\u52a1\u5668&amp;\u4f7f\u7528\u5916\u7f6e\u7684Servlet\u5bb9\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T14:52:26+00:00","dateModified":"2025-04-18T14:52:26+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/16420.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/16420.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/16420.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"SpringBoot\u7b2c\u4e09\u7ad9\uff084\uff09\uff1a\u914d\u7f6e\u5d4c\u5165\u5f0f\u670d\u52a1\u5668&amp;\u4f7f\u7528\u5916\u7f6e\u7684Servlet\u5bb9\u5668"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/16420","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=16420"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/16420\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/16401"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=16420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=16420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=16420"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=16420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}