{"id":26,"date":"2025-05-16T14:49:34","date_gmt":"2025-05-16T06:49:34","guid":{"rendered":"http:\/\/byname6.cn\/?p=26"},"modified":"2025-06-30T14:51:54","modified_gmt":"2025-06-30T06:51:54","slug":"tomcat%e6%ba%90%e7%a0%81%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"http:\/\/byname6.cn\/index.php\/2025\/05\/16\/tomcat%e6%ba%90%e7%a0%81%e5%88%86%e6%9e%90\/","title":{"rendered":"Tomcat\u6e90\u7801\u5206\u6790"},"content":{"rendered":"\n<p>\u8981\u5b8c\u6574\u8ddfTomcat\u53ef\u4ee5\u770b\u770b<a href=\"https:\/\/juejin.cn\/post\/7152446633194553374#heading-2\">https:\/\/juejin.cn\/post\/7152446633194553374#heading-2<\/a>\u7cfb\u5217\uff0c\u8fd9\u7bc7\u6587\u7ae0\u5c31\u7c97\u7565\u5730\u4ecb\u7ecd\u4e00\u4e0b\u3002<\/p>\n\n\n\n<p>IDEA\u642d\u5efa\u8c03\u8bd5tomcat web\u6d41\u7a0b\uff1a<\/p>\n\n\n\n<p><strong>1. <\/strong>\u5728<strong><code>Project Structure\/Modules<\/code><\/strong>\u4e2d\u589e\u52a0\u4e00\u4e2aIDEA\u7684\u81ea\u5e26web\u6a21\u5757\uff0c\u4f1a\u81ea\u52a8\u751f\u6210web\u5305\uff0c\u5e76\u9644\u5e26\u6807\u51c6\u7684\u6587\u4ef6\u7ed3\u6784\u3002<\/p>\n\n\n\n<p><strong>2. <\/strong>\u4e8eweb\u5305\u6839\u76ee\u5f55\u4e0b\u7f16\u5199index.jsp\u6216\u5b8c\u6210web\u4ee3\u7801\u540e\uff0c\u5728<strong><code>Project Structure\/Artifacts<\/code><\/strong>\u91ccAdd\u4e00\u4e2a<code>Web Application:Exploded<\/code>\u6216\u8005<code>Web Application: Archive<\/code>\uff0c\u5c06web\u5e94\u7528\u6253\u5305\u4ee5\u4fbf\u90e8\u7f72\u5728tomcat\u4e0a\uff0c\u4f1a\u5728out\u76ee\u5f55\u4e0b\u751f\u6210\u76f8\u5e94\u7684\u683c\u5f0f\uff1aExploded\u662f\u6587\u4ef6\u5939\u683c\u5f0f\uff0c\u65b9\u4fbf\u66f4\u6539\uff0c\u4e00\u822c\u8c03\u8bd5\u7684\u65f6\u5019\u9009\u7528\u8fd9\u4e2a\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728out\u76ee\u5f55\u4e2d\u66f4\u6539\u4ee3\u7801\uff1bArchive\u5c31\u662fwar\u5305\uff0cweb\u4ee3\u7801\u7f16\u5199\u8c03\u8bd5\u6210\u529f\u540e\u5c31\u6253\u6210war\u5305\u4fbf\u4e8e\u4f20\u8f93\u5230\u670d\u52a1\u5668\u4e0a\u8fd0\u884c\u3002<\/p>\n\n\n\n<p><strong>3. <\/strong>\u5728Run\/debug Configurations\u4e2dAdd\u4e00\u4e2aTomcat local(\u672c\u5730\u90e8\u7f72)\uff0c\u8f93\u5165URL\u7b49\u4fe1\u606f\uff0c\u518d\u5728Deployment\u4e2d\u9009\u62e9\u521a\u624d\u8f93\u51fa\u7684Artifact\uff0c\u542f\u52a8\u5373\u4f1a\u5728\u6307\u5b9aURL\u8fd0\u884cArtifact\u4e2d\u7684web\u670d\u52a1\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u67b6\u6784<\/h1>\n\n\n\n<p>tomcat\u662f\u4e00\u4e2aJava web\u5bb9\u5668\uff0c\u7c7b\u4f3c\u4e8e\u670d\u52a1\u5668\uff0c\u80fd\u63d0\u4f9b\u4e00\u4e2a\u6216\u591a\u4e2aweb\u7684\u8fd0\u884c\u73af\u5883\u3002\u8fd0\u884ctomcat web\u9700\u8981tomcat\u4e0a\u90e8\u7f72web\u670d\u52a1\u7684war\u5305\u3002<\/p>\n\n\n\n<p>Tomcat\u7ed3\u6784\u6700\u5168\u7684\u4e00\u5f20\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250525174350902.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250525174350902.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20250525174350902\"\/><\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Tomcat\u4e09\u5927\u903b\u8f91\u7ec4\u4ef6<\/h2>\n\n\n\n<p>\u5bf9\u4e8eTomcat Server\u63d0\u4f9bweb\u670d\u52a1\u7684\u6d41\u7a0b\uff0c\u53ef\u4ee5\u7b80\u5355\u62bd\u8c61\u4e3a\u4e09\u5927\u7ec4\u4ef6\uff1a<strong>Service\u3001Connector\u3001Container(\u56db\u79cd\u5b9e\u73b0\uff1aEngine\u3001Host\u3001Context\u3001Wrapper)<\/strong>\uff0c\u53ef\u4ee5\u7528\u4e00\u5f20\u56fe\u8868\u73b0\u5b83\u4eec\u4e4b\u95f4\u7684\u5173\u7cfb\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250525160222435.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250525160222435.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20250525160222435\"\/><\/div><\/figure>\n\n\n\n<p>Server\u662fTomcat\u9876\u5c42\u7ed3\u6784\uff0c\u53ef\u4ee5\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2aService\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Service<\/h3>\n\n\n\n<p><strong>Service\uff1a<\/strong>\u8868\u793a\u670d\u52a1\uff0cServer\u53ef\u4ee5\u8fd0\u884c\u591a\u4e2a\u670d\u52a1\u3002\u6bd4\u5982\u4e00\u4e2aTomcat\u91cc\u9762\u53ef\u8fd0\u884c\u8ba2\u5355\u670d\u52a1\u3001\u652f\u4ed8\u670d\u52a1\u3001\u7528\u6237\u670d\u52a1\u7b49\u7b49\uff1bServer\u7684\u5b9e\u73b0\u7c7bStandardServer\u53ef\u4ee5\u5305\u542b\u4e00\u4e2a\u5230\u591a\u4e2aServices, Service\u7684\u5b9e\u73b0\u7c7b\u4e3aStandardService\u8c03\u7528\u4e86Servlet Engine(\u5f15\u64ce)\uff0c\u800c\u4e14StandardService\u7c7b\u4e2d\u4e5f\u6307\u660e\u4e86\u8be5Service\u5f52\u5c5e\u7684Server\u3002<\/p>\n\n\n\n<p>\u5176\u4e2d\u9ed8\u8ba4Service\u5c31\u662f<strong>Catalina<\/strong>\uff0cCatalina\u5176\u5b9e\u5c31\u662fTomcat\u7684\u524d\u8eab\uff0c\u63d0\u4f9b\u57fa\u7840\u7684web\u670d\u52a1\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Container<\/h3>\n\n\n\n<p><strong>Engine\uff1a<\/strong>Container\u7684\u9876\u5c42\u7ed3\u6784\uff0c\u76f8\u5f53\u4e8e\u4e00\u4e2a\u63a5\u53e3\u80fd\u591f\u8c03\u63a7\u4ece\u5c5e\u7684Host\uff0c\u5e76\u751f\u6210\u65e5\u5fd7\u7b49\u3002\u4f1a\u914d\u7f6e\u4e00\u4e2a\u9ed8\u8ba4HOST\uff0c\u8bf7\u6c42\u7684host\u6ca1\u6709\u5339\u914d\u5230\u5408\u9002\u7684Host\u5c31\u4f1a\u4e22\u7ed9\u8fd9\u4e2a\u9ed8\u8ba4Host\u3002<\/p>\n\n\n\n<p><strong>Host\uff1a<\/strong>\u865a\u62df\u4e3b\u673a\u3002\u6bcf\u4e2aHost\u91cc\u53ef\u8fd0\u884c\u591a\u4e2aHost\u3002<\/p>\n\n\n\n<p><strong>Context\uff1a<\/strong>\u5728\u6e90\u7801\u7ed3\u6784\u91cc\u9762\u4f53\u73b0\u4e3a\u4e0d\u540c\u7684WebApps\u4e0b\u7684\u6bcf\u4e2a\u76ee\u5f55\u3002<\/p>\n\n\n\n<p><strong>Wrapper:<\/strong> \u5bf9Servlet\u7684\u5c01\u88c5\uff0c\u4e00\u4e2aWrapper\u5373\u5bf9\u5e94\u4e00\u4e2aServlet\u5b9e\u4f8b\uff0c\u8d1f\u8d23\u5bf9Servlet\u88c5\u8f7d\u3001\u521d\u59cb\u5316\u3001\u6267\u884c\u4ee5\u53ca\u8d44\u6e90\u56de\u6536\u3002<\/p>\n\n\n\n<p>\u5728Tomcat\u4e2d\u63d0\u4f9b\u7684\u4e00\u5207\u7684\u670d\u52a1\u90fd\u662f\u5bf9\u5e94\u7740\u4e0d\u540c\u7684Servlet\uff0cServlet\u53ef\u4ee5\u8bf4\u662f\u6700\u91cd\u8981\u7684\u4e00\u4e2a&#8221;\u7ec4\u4ef6&#8221;\uff0c\u90a3\u4e48Servlet\u7684\u5177\u4f53\u5b9e\u73b0\u662f\u600e\u6837\u7684\u5462\uff1f<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Connector<\/h3>\n\n\n\n<p>Connector\u662f\u7528\u6765\u5904\u7406\u8bf7\u6c42\uff0c\u76d1\u542c\u5728\u67d0\u4e2a\u7aef\u53e3\uff0c\u7b49\u5f85\u8bf7\u6c42\u63a5\u5165\uff0c\u5c01\u88c5Request\u5bf9\u8c61\uff0c\u5206\u53d1\u5230Container\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/1168971-20190805194836476-1284200425.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/1168971-20190805194836476-1284200425.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"img\"\/><\/div><\/figure>\n\n\n\n<p>Connector\u7684\u4e3b\u8981\u5b9e\u73b0\u662fProtocolHandler\u3002<\/p>\n\n\n\n<p><code>ProtocolHandler<\/code>\u5305\u542b\u4e09\u4e2a\u90e8\u4ef6\uff1a<code>Endpoint<\/code>\u3001<code>Processor<\/code>\u3001<code>Adapter<\/code>\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>Endpoint<\/code>\u7528\u6765\u5904\u7406\u5e95\u5c42Socket\u7684\u7f51\u7edc\u8fde\u63a5\uff0c<code>Processor<\/code>\u7528\u4e8e\u5c06<code>Endpoint<\/code>\u63a5\u6536\u5230\u7684Socket\u5c01\u88c5\u6210Request\uff0c<code>Adapter<\/code>\u7528\u4e8e\u5c06Request\u4ea4\u7ed9Container\u8fdb\u884c\u5177\u4f53\u7684\u5904\u7406\u3002<\/li>\n\n\n\n<li><code>Endpoint<\/code>\u7531\u4e8e\u662f\u5904\u7406\u5e95\u5c42\u7684Socket\u7f51\u7edc\u8fde\u63a5\uff0c\u56e0\u6b64<code>Endpoint<\/code>\u662f\u7528\u6765\u5b9e\u73b0<code>TCP\/IP\u534f\u8bae<\/code>\u7684\uff0c\u800c<code>Processor<\/code>\u7528\u6765\u5b9e\u73b0<code>HTTP\u534f\u8bae<\/code>\u7684\uff0c<code>Adapter<\/code>\u5c06\u8bf7\u6c42\u9002\u914d\u5230Servlet\u5bb9\u5668\u8fdb\u884c\u5177\u4f53\u7684\u5904\u7406\u3002<\/li>\n\n\n\n<li><code>Endpoint<\/code>\u7684\u62bd\u8c61\u5b9e\u73b0\u7c7bAbstractEndpoint\u91cc\u9762\u5b9a\u4e49\u4e86<code>Acceptor<\/code>\u548c<code>AsyncTimeout<\/code>\u4e24\u4e2a\u5185\u90e8\u7c7b\u548c\u4e00\u4e2a<code>Handler\u63a5\u53e3<\/code>\u3002<code>Acceptor<\/code>\u7528\u4e8e\u76d1\u542c\u8bf7\u6c42\uff0c<code>AsyncTimeout<\/code>\u7528\u4e8e\u68c0\u67e5\u5f02\u6b65Request\u7684\u8d85\u65f6\uff0c<code>Handler<\/code>\u7528\u4e8e\u5904\u7406\u63a5\u6536\u5230\u7684Socket\uff0c\u5728\u5185\u90e8\u8c03\u7528<code>Processor<\/code>\u8fdb\u884c\u5904\u7406\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u5177\u4f53\u7684\u5904\u7406\u6d41\u7a0b\u653e\u5728\u4e0b\u6587\u8bb2\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">JavaWeb\u4e09\u5927\u7ec4\u4ef6<\/h2>\n\n\n\n<p>\u4e09\u8005\u7684\u52a0\u8f7d\u987a\u5e8f\u4e3a<strong><code>Listener-&gt;Filter-&gt;Servlet<\/code><\/strong>\u3002<\/p>\n\n\n\n<p>\u5728<code>org.apache.catalina.core.StandardContext<\/code>\u7c7b\u7684<code>startInternal()<\/code>\u65b9\u6cd5\u4e2d\uff0c\u9996\u5148\u8c03\u7528\u4e86<code>listenerStart()<\/code>\uff0c\u63a5\u7740\u662f<code>filterStart()<\/code>\uff0c\u6700\u540e\u662f<code>loadOnStartup()<\/code>\u3002\u8fd9\u4e09\u5904\u8c03\u7528\u89e6\u53d1\u4e86Listener\u3001Filter\u3001Servlet\u7684\u6784\u9020\u52a0\u8f7d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Servlet<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Servlet<\/h4>\n\n\n\n<p>\u4e00\u5207\u7684\u6838\u5fc3\u903b\u8f91\u548c\u670d\u52a1\u5b9e\u73b0\u90fd\u662fServlet\u5b9e\u73b0\u7684\u3002<\/p>\n\n\n\n<p>Servlert\u7684\u6ce8\u518c\uff1a<\/p>\n\n\n\n<p>\u6ce8\u91ca\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">...<br>@WebServlet(\"\/hello\")<br>public class MyServlet extends HttpServlet {<br>...<br>}<\/pre>\n\n\n\n<p>web.xml:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp;&lt;servlet&gt;<br> &nbsp; &nbsp;&lt;servlet-name&gt;MyServlet&lt;\/servlet-name&gt;<br> &nbsp; &nbsp;&lt;servlet-class&gt;Servlet.MyServlet&lt;\/servlet-class&gt;<br> &nbsp; &nbsp;&lt;load-on-startup&gt;1&lt;\/load-on-startup&gt;<br> &nbsp;&lt;\/servlet&gt;<br> &nbsp;&lt;servlet-mapping&gt;<br> &nbsp; &nbsp;&lt;servlet-name&gt;MyServlet&lt;\/servlet-name&gt;<br> &nbsp; &nbsp;&lt;url-pattern&gt;\/hello&lt;\/url-pattern&gt;<br> &nbsp;&lt;\/servlet-mapping&gt;<\/pre>\n\n\n\n<p><strong>Servlet\u7684\u542f\u52a8\uff1a<\/strong>\u670d\u52a1\u5668\u542f\u52a8\u65f6(web.xml\u4e2d\u914d\u7f6eload-on-startup=1\uff0c\u9ed8\u8ba4\u4e3a0)\u6216\u8005\u7b2c\u4e00\u6b21\u8bf7\u6c42\u8be5servlet\u65f6\uff0c\u5c31\u4f1a\u521d\u59cb\u5316\u4e00\u4e2aServlet\u5bf9\u8c61\uff0c\u4e5f\u5c31\u662f\u4f1a\u6267\u884c\u521d\u59cb\u5316\u65b9\u6cd5init(ServletConfig conf)\u3002<\/p>\n\n\n\n<p>Servlet\u662f\u7528\u6765\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42\u7684\u52a8\u6001\u8d44\u6e90\uff0c\u5f53Tomcat\u63a5\u6536\u5230\u6765\u81ea\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u65f6\uff0c\u4f1a\u5c06\u5176\u89e3\u6790\u6210<code>RequestServlet<\/code>\u5bf9\u8c61\u5e76\u53d1\u9001\u5230\u5bf9\u5e94\u7684Servlet\u4e0a\u8fdb\u884c\u5904\u7406\u3002<\/p>\n\n\n\n<p>\u7f16\u5199\u4e00\u4e2aServlet\u7684\u5b9e\u73b0\u9700\u8981\u5b9e\u73b0Servlet\u63a5\u53e3\u7684\u4e94\u4e2a\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250526143040810.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250526143040810.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20250526143040810\"\/><\/div><\/figure>\n\n\n\n<p>\u5b9e\u73b0\u4e86\u670d\u52a1\u903b\u8f91\u7684\u65b9\u6cd5\u662f<strong>Servlet.service(\uff09<\/strong>\u3002<\/p>\n\n\n\n<p>Tomcat\u4e3a\u5f00\u53d1\u8005\u5c01\u88c5\u4e86\u4e24\u79cd\u6807\u51c6Servlet\uff0c\u4ee5\u4fbf\u4e8e\u5b9e\u73b0Servlet\u53ea\u9700\u8981\u91cd\u5199\u6838\u5fc3\u903b\u8f91\u800c\u4e0d\u7528\u7e41\u7410\u5730\u5b9e\u73b0\u591a\u79cd\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<p>GenericServlet\u3001HttpServlet\u3002<\/p>\n\n\n\n<p>GenericServlet\u62bd\u8c61\u7c7b\u662f\u4e00\u4e2a\u901a\u7528\u7684Servlet\u7c7b\uff0c\u5bf9\u5176\u4ed6\u56db\u79cd\u65b9\u6cd5\u8fdb\u884c\u4e86\u7b80\u5355\u5b9e\u73b0\u3001\u53ea\u9700\u8981\u5f00\u53d1\u8005\u91cd\u5199Service\u3002<\/p>\n\n\n\n<p>HttpServlet\u62bd\u8c61\u7c7b\u662fGenericServlet\u7684\u5b50\u7c7b\uff0c\u4e13\u95e8\u5904\u7406HTTP\u8bf7\u6c42\u3002\u5176Service\u65b9\u6cd5<strong><code>protected void service(HttpServletRequest req, HttpServletResponse resp)<\/code><\/strong>\u662f\u5bf9HTTP\u8bf7\u6c42\u7684\u6807\u51c6\u5904\u7406\uff0c\u5148\u89e3\u6790HTTP\u8bf7\u6c42\uff0c\u518d\u6839\u636e\u4e0d\u540c\u7684\u8bf7\u6c42\u65b9\u6cd5\u8c03\u7528\u5230doGet\u3001doPost\u3002\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\u53ea\u9700\u8981\u91cd\u5199doGet\u7b49\u65b9\u6cd5\u5373\u53ef\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>ServletConfig<\/strong><\/h4>\n\n\n\n<p>\u5c01\u88c5\u4e86Servlet\u7684\u7279\u6b8a\u4fe1\u606f\u5982\u7c7b\u540d\u7b49\u7b49\u3002\u5728Servler\u521d\u59cb\u5316\u7684\u65f6\u5019\u4e3a\u5176\u521b\u5efa\uff0c\u4e00\u4e2aServletConfig\u5bf9\u5e94\u4e00\u4e2aServlet\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>ServletContext<\/strong><\/h4>\n\n\n\n<p>\u5f53\u4e00\u4e2aContext(Webapps\u76ee\u5f55)\u4e0b\u7684\u4e00\u4e2aServlet\u88ab\u542f\u52a8\uff0c\u4f1a\u6ce8\u518c\u4e00\u4e2aServletContext\u4f5c\u4e3a\u5f53\u524dContext\u6240\u6709Servlet\u7684\u5171\u4eab\u4e0a\u4e0b\u6587\u3002ServletContext\u53ef\u4ee5\u83b7\u53d6Web\u5e94\u7528\u4e2d\u7684\u5171\u4eab\u7684\u8d44\u6e90\uff0c\u6bd4\u5982web.xml\u7684\u521d\u59cb\u5316\u53c2\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;web-app&gt;<br> &nbsp;&lt;display-name&gt;Archetype Created Web Application&lt;\/display-name&gt;<br>  \/\/\u914d\u7f6e\u4e0a\u4e0b\u6587\u521d\u59cb\u5316\u53c2\u6570name<br> &nbsp;&lt;context-param&gt;<br> &nbsp; &nbsp;&lt;param-name&gt;name&lt;\/param-name&gt;<br> &nbsp; &nbsp;&lt;param-value&gt;Feng&lt;\/param-value&gt;<br> &nbsp;&lt;\/context-param&gt;<br> &nbsp;&lt;context-param&gt;<br> &nbsp; &nbsp;&lt;param-name&gt;age&lt;\/param-name&gt;<br> &nbsp; &nbsp;&lt;param-value&gt;18&lt;\/param-value&gt;<br> &nbsp;&lt;\/context-param&gt;<br>&lt;\/web-app&gt;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"> protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {<br> &nbsp; &nbsp; &nbsp; &nbsp;resp.setContentType(\"text\/html; charset=UTF-8\");<br> &nbsp; &nbsp; &nbsp; &nbsp;PrintWriter writer = resp.getWriter();<br> <br> &nbsp; &nbsp; &nbsp; &nbsp;ServletContext servletContext = getServletContext();<br> <br> &nbsp; &nbsp; &nbsp; &nbsp;Enumeration&lt;String&gt; initParamerNames = servletContext.getInitParameterNames();<br> &nbsp; &nbsp; &nbsp; &nbsp;while(initParamerNames.hasMoreElements()){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String ParamerName = initParamerNames.nextElement();<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String Paramer = servletContext.getInitParameter(ParamerName);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;writer.write(ParamerName+\"\u7684\u503c\u4e3a\uff1a\"+Paramer+\"&lt;br\/&gt;\");<br> &nbsp; &nbsp; &nbsp;  }<br> <br> &nbsp; &nbsp; &nbsp; &nbsp;writer.close();<br> &nbsp;  }<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Filter<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Filter<\/h4>\n\n\n\n<p>\u5de5\u4f5c\u6d41\u7a0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/1504236208-0.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/1504236208-0.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"Filter \u6d41\u7a0b\u56fe\"\/><\/div><\/figure>\n\n\n\n<p>\u4f1a\u628a\u6240\u6709\u7684Filter\u6ce8\u518c\u5230\u4e00\u4e2aFilterChain\u4e2d\uff0c\u7136\u540e\u5728\u8bf7\u6c42\u548c\u8fd4\u56de\u54cd\u5e94\u65f6\u94fe\u5f0f\u8c03\u7528Filter.doFilter()\u3002\u5176\u89e6\u53d1\u987a\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">- \u57fa\u4e8e\u6ce8\u89e3\u914d\u7f6e\uff1a\u6309\u7167\u7c7b\u540d\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83\u89c4\u5219\u6bd4\u8f83\uff0c\u503c\u5c0f\u7684\u5148\u6267\u884c<br>- \u4f7f\u7528web.xml\u914d\u7f6e\uff1a\u6839\u636e\u5bf9\u5e94\u7684Mapping\u7684\u987a\u5e8f\u7ec4\u7ec7\uff0c\u8c01\u5b9a\u4e49\u5728\u4e0a\u8fb9\u8c01\u5c31\u5728\u524d<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>FilterConfig<\/strong><\/h4>\n\n\n\n<p>\u548cServlet\u7c7b\u4f3c\uff0c\u7531\u4e8eFilter\u4e5f\u6709\u53ef\u80fd\u8bbf\u95eeServlet\uff0c\u6240\u4ee5Servlet \u89c4\u8303\u5c06\u4ee3\u8868 ServletContext \u5bf9\u8c61\u548c Filter \u7684\u914d\u7f6e\u53c2\u6570\u4fe1\u606f\u90fd\u5c01\u88c5\u5230\u4e00\u4e2a\u79f0\u4e3a FilterConfig \u7684\u5bf9\u8c61\u4e2d\u3002<\/p>\n\n\n\n<p>FilterConfig\u63a5\u53e3\u5219\u7528\u4e8e\u5b9a\u4e49FilterConfig\u5bf9\u8c61\u5e94\u8be5\u5bf9\u5916\u63d0\u4f9b\u7684\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u5728 Filter\u7684doFilter()\u65b9\u6cd5\u4e2d\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u83b7\u53d6 ServletContext \u5bf9\u8c61\uff0c\u4ee5\u53ca\u83b7\u53d6\u5728 web.xml \u6587\u4ef6\u4e2d\u7684\u4e00\u4e9b\u521d\u59cb\u5316\u53c2\u6570\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250526153349442.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250526153349442.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20250526153349442\"\/><\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Listener<\/h3>\n\n\n\n<p>Listener\u662f\u4e00\u4e2a\u5b9e\u73b0\u4e86\u7279\u5b9a\u63a5\u53e3\u7684Java\u7a0b\u5e8f\uff0c\u7528\u4e8e\u76d1\u542c\u4e00\u4e2a\u65b9\u6cd5\u6216\u8005\u5c5e\u6027\uff0c\u5f53\u88ab\u76d1\u542c\u7684\u65b9\u6cd5\u88ab\u8c03\u7528\u6216\u8005\u5c5e\u6027\u6539\u53d8\u65f6\uff0c\u5c31\u4f1a\u81ea\u52a8\u6267\u884c\u67d0\u4e2a\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u6709\u51e0\u4e2a\u4e0eListener\u76f8\u5173\u7684\u6982\u5ff5<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e8b\u4ef6\uff1a\u67d0\u4e2a\u65b9\u6cd5\u88ab\u8c03\u7528\uff0c\u6216\u8005\u5c5e\u6027\u7684\u6539\u53d8<\/li>\n\n\n\n<li>\u4e8b\u4ef6\u6e90\uff1a\u88ab\u76d1\u542c\u7684\u5bf9\u8c61\uff08\u5982ServletContext\u3001requset\u3001\u65b9\u6cd5\u7b49\uff09<\/li>\n\n\n\n<li>\u76d1\u542c\u5668\uff1a\u7528\u4e8e\u76d1\u542c\u4e8b\u4ef6\u6e90\uff0c\u5f53\u53d1\u751f\u4e8b\u4ef6\u65f6\u4f1a\u89e6\u53d1\u76d1\u542c\u5668<\/li>\n<\/ul>\n\n\n\n<p>\u76d1\u542c\u5668\u6709\u7c7b\u4f3c\u51e0\u79cd\u63a5\u53e3\uff1a\u90fd\u7ee7\u627f\u81ea<strong>EventListener<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u4e8b\u4ef6\u6e90<\/th><th>\u76d1\u542c\u5668<\/th><th>\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td>ServletContext<\/td><td>ServletContextListener<\/td><td>\u7528\u4e8e\u76d1\u542c ServletContext \u5bf9\u8c61\u7684\u521b\u5efa\u4e0e\u9500\u6bc1<\/td><\/tr><tr><td>HttpSession<\/td><td>HttpSessionListener<\/td><td>\u7528\u4e8e\u76d1\u542c HttpSession \u5bf9\u8c61\u7684\u521b\u5efa\u548c\u9500\u6bc1<\/td><\/tr><tr><td>ServletRequest<\/td><td>ServletRequestListener<\/td><td>\u7528\u4e8e\u76d1\u542c ServletRequest \u5bf9\u8c61\u7684\u521b\u5efa\u548c\u9500\u6bc1<\/td><\/tr><tr><td>Livecycle<\/td><td>LivecycleListener<\/td><td>\u7528\u4e8e\u76d1\u542c\u751f\u547d\u5468\u671f\u4e8b\u4ef6\u7684\u53d1\u751f<\/td><\/tr><tr><td>ServletContext<\/td><td>ServletContextAttributeListener<\/td><td>\u7528\u4e8e\u76d1\u542c ServletContext \u5bf9\u8c61\u7684\u5c5e\u6027\u65b0\u589e\u3001\u79fb\u9664\u548c\u66ff\u6362<\/td><\/tr><tr><td>HttpSession<\/td><td>HttpSessionAttributeListener<\/td><td>\u7528\u4e8e\u76d1\u542c HttpSession \u5bf9\u8c61\u7684\u5c5e\u6027\u65b0\u589e\u3001\u79fb\u9664\u548c\u66ff\u6362<\/td><\/tr><tr><td>ServletRequest<\/td><td>ServletRequestAttributeListener<\/td><td>\u7528\u4e8e\u76d1\u542c HttpServletRequest \u5bf9\u8c61\u7684\u5c5e\u6027\u65b0\u589e\u3001\u79fb\u9664\u548c\u66ff\u6362<\/td><\/tr><tr><td>HttpSession<\/td><td>HttpSessionBindingListener<\/td><td>\u7528\u4e8e\u76d1\u542c JavaBean \u5bf9\u8c61\u7ed1\u5b9a\u5230 HttpSession \u5bf9\u8c61\u548c\u4ece HttpSession \u5bf9\u8c61\u89e3\u7ed1\u7684\u4e8b\u4ef6<\/td><\/tr><tr><td>HttpSession<\/td><td>HttpSessionActivationListener<\/td><td>\u7528\u4e8e\u76d1\u542c HttpSession \u4e2d\u5bf9\u8c61\u6d3b\u5316\u548c\u949d\u5316\u7684\u8fc7\u7a0b<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>ServletContextListener\u4f7f\u7528\u793a\u4f8b<\/strong><\/p>\n\n\n\n<p>\u4ee5ServletContextListener\u4e3a\u4f8b\uff0c\u521b\u5efa\u4e00\u4e2a\u7528\u4e8e\u76d1\u542cServletContext\u5bf9\u8c61\u7684Listener\uff0c\u4f7f\u7528\u6ce8\u89e3\u914d\u7f6e<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">package Listener;<br> <br>import javax.servlet.ServletContextEvent;<br>import javax.servlet.ServletContextListener;<br>import javax.servlet.annotation.WebListener;<br> <br>@WebListener<br>public class Hello_Listener implements ServletContextListener {<br> <br> &nbsp; &nbsp;@Override<br> &nbsp; &nbsp;public void contextInitialized(ServletContextEvent sce) {<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"ServletContext\u5bf9\u8c61\u521b\u5efa\u4e86\uff01\");<br> &nbsp;  }<br> <br> &nbsp; &nbsp;@Override<br> &nbsp; &nbsp;public void contextDestroyed(ServletContextEvent sce) {<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"ServletContext\u5bf9\u8c61\u9500\u6bc1\u4e86\uff01\");<br> &nbsp;  } <br>}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Pipeline-value<\/h2>\n\n\n\n<p>tomcat\u5b9a\u4e49\u4e86Pipeline\uff08\u7ba1\u9053\uff09\u548cValue\uff08\u9600\uff09\u3002<\/p>\n\n\n\n<p>Pipeline\u7684\u4e3b\u8981\u529f\u80fd\u662f\u7ef4\u62a4\u4e00\u4e2aValue\u94fe\u8868\uff0c\u94fe\u5f0f\u8c03\u7528\u8fd9\u4e2aValue\uff0cPipeline\u7684\u552f\u4e00\u5b9e\u73b0\u7c7b\u662fStandardPipeline\u3002\u5728ContainerBase\u4e2d\u4fbf\u5b9e\u4f8b\u5316\u4e86\u4e00\u4e2aStandardPipeline\uff0c\u800cTomcat\u7684\u51e0\u4e2a\u5bb9\u5668\u90fd\u662f\u7ee7\u627f\u4e86ContainerBase\uff0c\u5b9e\u9645\u4e0a\u5b83\u4eec\u4e5f\u90fd\u662f\u4f9d\u9760Pipeline\u7ba1\u9053\u6765\u5f00\u5c55\u8c03\u7528\u6267\u884c\u3002<\/p>\n\n\n\n<p>Value\u7684\u6838\u5fc3\u903b\u8f91\u5b9e\u73b0\u5728invoke\u65b9\u6cd5\u4e2d\u3002ValueBase\u5b9e\u73b0\u4e86Container\u548cValue\u63a5\u53e3\uff0c\u662f\u4e00\u4e2a\u57fa\u7840\u62bd\u8c61\u7c7b\uff0c\u5176\u56db\u4e2a\u5b50\u7c7b\u5206\u522b\u662f\u56db\u79cd\u5bb9\u5668\u5bf9\u5e94\u7684\u72ec\u7279\u6807\u51c6Value\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250528110310173.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250528110310173.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20250528110310173\"\/><\/div><\/figure>\n\n\n\n<p>\u4e0a\u9762\u8bf4\u4e86Pipeline\u7684\u5b9e\u4f8b\u5316\u90fd\u662f\u5728ContainerBase\u4e2d\u5b9e\u73b0\u7684\uff0c\u90a3\u4e48\u6bcf\u4e2a\u5bb9\u5668\u4e2d\u7684Value\u5462\uff1f<\/p>\n\n\n\n<p>\u90fd\u662f\u5728\u5bb9\u5668\u65b9\u6cd5\u4e2d\u5b9e\u4f8b\u5316\u7684\uff0c\u5982StandardHost#startInternal\uff1b\u5982\u679c\u60f3\u81ea\u5b9a\u4e49\u4f7f\u7528\u4e5f\u53ef\u4ee5\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u8fd9\u662f\u89e3\u6790\u914d\u7f6e\u6587\u4ef6\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">digester.addObjectCreate(prefix + \"Context\/Valve\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null, \/\/ MUST be specified in the element<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"className\");<br>digester.addSetProperties(prefix + \"Context\/Valve\");<br>digester.addSetNext(prefix + \"Context\/Valve\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"addValve\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"org.apache.catalina.Valve\");<\/pre>\n\n\n\n<p>\u5728\u6bcf\u4e2a\u5185\u7f6e\u7684Value\u5b9e\u73b0\u7c7b\u7684invoke\u65b9\u6cd5\u90fd\u4f1a\u94fe\u5f0f\u8c03\u7528\u4e0b\u4e00\u5c42value#invoke\uff0c\u5982\uff1a<\/p>\n\n\n\n<p>StandardEngineValue#invoke:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">host.getPipeline().getFirst().invoke(request, response);<\/pre>\n\n\n\n<p>StandardHostValue#invoke:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">context.getPipeline().getFirst().invoke(request, response);<\/pre>\n\n\n\n<p>&#8230;&#8230;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250528124823340.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250528124823340.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20250528124823340\"\/><\/div><\/figure>\n\n\n\n<p>\u8bf7\u6c42\u5305\u6700\u540e\u8d70\u5230<strong><code>StandardWrapperValve#invoke<\/code>\uff1a<\/strong><\/p>\n\n\n\n<p>\u901a\u8fc7wrapper.allocate()\u65b9\u6cd5\uff0c\u521d\u59cb\u5316servlet\u3002<\/p>\n\n\n\n<p>\u521b\u5efaApplicationFilterChain\uff0c\u8c03\u7528FilterChain\u7684doFilter\u65b9\u6cd5\uff0c\u5728\u6700\u540e\u4e00\u4e2aFilter\u65b9\u6cd5\u6267\u884c\u7ed3\u675f\u540e\uff0c\u4f1a\u8c03\u7528Servlet\u7684Service\u65b9\u6cd5\uff0c\u4ece\u800c\u5b9e\u73b0\u5bf9Servlet\u7684\u8c03\u7528\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">LifeStyle<\/h2>\n\n\n\n<p>\u5728 Tomcat \u4e2d\uff0cServer\u3001Service\u3001\u5bb9\u5668\u53ca\u7ba1\u9053\u7b49\u7ec4\u4ef6\u90fd\u5177\u6709\u7edf\u4e00\u7684\u751f\u547d\u5468\u671f\uff1a<strong>\u521d\u59cb\u5316\uff08init\uff09\u3001\u542f\u52a8\uff08start\uff09\u3001\u505c\u6b62\uff08stop\uff09\u3001\u9500\u6bc1\uff08destroy\uff09<\/strong>\u3002<\/p>\n\n\n\n<p>LifeCycle \u63a5\u53e3\u5c31\u662f\u5bf9\u8fd9\u4e9b\u751f\u547d\u5468\u671f\u64cd\u4f5c\u8fdb\u884c\u4e86\u62bd\u8c61\uff0c\u4fbf\u4e8e\u7ec4\u4ef6\u7684\u7edf\u4e00\u7f16\u5199\u548c\u8c03\u7528\u3002<\/p>\n\n\n\n<p>\u6709\u4e86\u8fd9\u4e2a\u63a5\u53e3\uff0cTomcat \u7684\u751f\u547d\u5468\u671f\u8c03\u7528\u8def\u5f84\u4e5f\u66f4\u6e05\u6670\u3002\u4f8b\u5982\uff0c\u521d\u59cb\u5316\u8fc7\u7a0b\u4ece\u9876\u5c42 Server \u7684 <code>init()<\/code> \u65b9\u6cd5\u5f00\u59cb\uff0c\u4f9d\u6b21\u94fe\u5f0f\u8c03\u7528\u5404\u5b50\u7ec4\u4ef6\u7684 <code>init()<\/code>\u3002\u56e0\u6b64\uff0c\u53ea\u9700\u64cd\u4f5c\u9876\u5c42 Server\uff0c\u5373\u53ef\u5b8c\u6210\u6240\u6709\u7ec4\u4ef6\u7684\u751f\u547d\u5468\u671f\u7ba1\u7406\u3002<\/p>\n\n\n\n<p><strong><code>LifecycleBase<\/code><\/strong>\u662fLifecycle\u7684\u62bd\u8c61\u7c7b\uff0c\u4e5f\u662f\u88abContainer\u7ee7\u627f\u7684\u7c7b\uff1b\u5176init\u3001start\u7b49\u65b9\u6cd5\u5b9e\u9645\u4e0a\u662f\u53bb\u8c03\u7528\u5b9e\u73b0\u4e86\u7684<strong>xxxInternal<\/strong>\uff0c\u5982initInternal\u3001startInternal\u7b49\u3002<\/p>\n\n\n\n<p>\u5728\u8fd9\u79cd\u64cd\u4f5c\u6a21\u5f0f\u4e0b\uff0c\u4e5f\u4f1a\u5f15\u5165\u4e00\u4e9b\u65b0\u7684\u60c5\u666f\uff1a\u5982\u5e76\u975e\u6240\u6709\u7ec4\u4ef6\u90fd\u5728\u521d\u59cb\u5316\u9636\u6bb5\u7acb\u5373\u52a0\u8f7d\uff0c\u50cf\u662fServlet\uff0c\u53ef\u80fd\u5728\u9996\u6b21\u8bf7\u6c42\u65f6\u624d\u521d\u59cb\u5316\u3002\u5728\u5904\u7406\u8bf7\u6c42\u7684\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u4e4b\u524d\u521d\u59cb\u5316\u7684\u7ec4\u4ef6\u4e0d\u518d\u9700\u8981\u518d\u521d\u59cb\u5316\u4e86\u3002\u4e3a\u4e86\u652f\u6301\u6309\u9700\u52a0\u8f7d\uff0cTomcat \u5f15\u5165\u4e86<strong>\u72b6\u6001\u673a\u6a21\u5f0f<\/strong>\u6765\u7cbe\u7ec6\u7ba1\u7406\u7ec4\u4ef6\u72b6\u6001\u3002<\/p>\n\n\n\n<p>\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u5f53\u7ec4\u4ef6\u5728STARTING_PREP\u3001STARTING\u6216STARTED\u65f6\uff0c\u8c03\u7528start()\u65b9\u6cd5\u6ca1\u6709\u4efb\u4f55\u6548\u679c<br>\u5f53\u7ec4\u4ef6\u5728NEW\u72b6\u6001\u65f6\uff0c\u8c03\u7528start()\u65b9\u6cd5\u4f1a\u5bfc\u81f4init()\u65b9\u6cd5\u88ab\u7acb\u523b\u6267\u884c\uff0c\u968f\u540estart()\u65b9\u6cd5\u88ab\u6267\u884c<br>\u5f53\u7ec4\u4ef6\u5728STOPPING_PREP\u3001STOPPING\u6216STOPPED\u65f6\uff0c\u8c03\u7528stop()\u65b9\u6cd5\u6ca1\u6709\u4efb\u4f55\u6548\u679c<br>\u5f53\u4e00\u4e2a\u7ec4\u4ef6\u5728NEW\u72b6\u6001\u65f6\uff0c\u8c03\u7528stop()\u65b9\u6cd5\u4f1a\u5c06\u7ec4\u4ef6\u72b6\u6001\u53d8\u66f4\u4e3aSTOPPED\uff0c\u6bd4\u8f83\u5178\u578b\u7684\u573a\u666f\u5c31\u662f\u7ec4\u4ef6\u542f\u52a8\u5931\u8d25\uff0c\u5176\u5b50\u7ec4\u4ef6\u8fd8\u6ca1\u6709\u542f\u52a8\u3002\u5f53\u4e00\u4e2a\u7ec4\u4ef6\u505c\u6b62\u7684\u65f6\u5019\uff0c\u5b83\u5c06\u5c1d\u8bd5\u505c\u6b62\u5b83\u4e0b\u9762\u7684\u6240\u6709\u5b50\u7ec4\u4ef6\uff0c\u5373\u4f7f\u5b50\u7ec4\u4ef6\u8fd8\u6ca1\u6709\u542f\u52a8<\/pre>\n\n\n\n<p>\u5bf9\u4e8e\u6bcf\u4e2a\u4e8b\u4ef6\u4e5f\u5f15\u5165\u4e86\u76d1\u542c\u673a\u5236\uff0c\u4f1a\u90e8\u7f72Listener\u76d1\u542cLifeStyle\uff0c\u5728\u4ee3\u7801\u4e2d\u4f53\u73b0\u4e3a\u5982<code>setState(LifecycleState.STARTING)<\/code>\uff1b\u6709\u52a9\u4e8e\u5bf9\u529f\u80fd\u7684\u4fee\u6539\u548c\u589e\u52a0\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Tomcat\u67b6\u6784\u7684\u7c7b\u4f9d\u8d56<\/h2>\n\n\n\n<p>Tomcat\u6574\u4e2a\u67b6\u6784\u7684\u7c7b\u3001\u4f9d\u8d56\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250526220432076.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250526220432076.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20250526220432076\"\/><\/div><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">\u4ee3\u7801\u89e3\u6790<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">Tomcat\u670d\u52a1\u521d\u59cb\u5316\u548c\u542f\u52a8<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">LifeStyle\u7ec4\u4ef6\u521d\u59cb\u5316\u3001\u542f\u52a8<\/h3>\n\n\n\n<p>\u4e0a\u9762\u77e5\u9053\uff0c\u6240\u6709\u7684Service\uff0cContainer\uff0cConnector\u7ec4\u4ef6\u90fd\u662fLifeStyle\u63a5\u53e3\u5b9e\u73b0\uff0c\u6240\u4ee5\u5176\u521d\u59cb\u5316\u3001\u542f\u52a8\u90fd\u662f\u94fe\u5f0f\u8c03\u7528xxx.init()\uff0cxxx.start()\u3002<\/p>\n\n\n\n<p>startup.sh\u8c03\u7528\u4e86<strong><code>Bootstra#main<\/code><\/strong>\u3002<strong><code>Bootstra#main<\/code><\/strong>\u4e2d\u5148\u8c03\u7528init\u65b9\u6cd5\uff0c\u521b\u5efa\u4e00\u4e9b\u7c7b\u52a0\u8f7d\u5668\uff0c\u4ee5\u53ca\u5b9e\u4f8b\u5316<strong><code>org.apache.catalina.startup.Catalina<\/code><\/strong>\u5230deamon\u6210\u5458\u53d8\u91cf\u4e2d\uff1b\u968f\u540e\u63a5\u53d7\u5230.sh\u4e2d\u7684start\u53c2\u6570\uff0c\u53cd\u5c04\u8c03\u7528<strong>Catalina\u7684load\u548cstart\u65b9\u6cd5<\/strong>\u542f\u52a8\u3002<\/p>\n\n\n\n<p><strong><code>Catalina#load<\/code><\/strong>\u5148\u901a\u8fc7<code>parseServerXml<\/code>\u89e3\u6790server.xml\uff0c\u5b9e\u4f8b\u5316\u5176\u4e2d\u7684\u7ec4\u4ef6\u5e76\u6ce8\u518c\u5230Catalina\u4e2d\uff0c\u5e76\u8c03\u7528<code>getServer().init()<\/code>\uff0c\u5373\u5982\u4e0a\u6587\u6240\u8bf4\u4eceCatalina#init\u521d\u59cb\u5316Catalina\u53ca\u5176\u5b50\u90e8\u4ef6\u3002<strong><code>Catalina#start<\/code><\/strong>\u4e5f\u662f\u8c03\u7528<code>getServer().start()<\/code>\uff0c\u542f\u52a8\u5bf9\u6240\u6709\u7ec4\u4ef6\u7684\u521d\u59cb\u5316\u3002\u5176\u4e2d<strong>Host\u53ca\u5176\u4e0b\u7ea7\u7ec4\u4ef6\u7684\u521d\u59cb\u5316\u3001\u5b9e\u4f8b\u5316\u548c\u542f\u52a8\u6bd4\u8f83\u7279\u6b8a\uff0c\u5e76\u6ca1\u6709\u5728Engine\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u4e2d\u5b8c\u6210Host\u53ca\u4e0b\u7ea7\u7ec4\u4ef6\u7684\u521d\u59cb\u5316\u3002<\/strong>\u5177\u4f53\u7684\u5b9e\u73b0\u4e5f\u662fTomcat\u652f\u6301\u70ed\u90e8\u7f72Webapp\u7684\u539f\u56e0\uff0c\u7a0d\u540e\u5373\u8ba8\u8bba\u5230\u3002<\/p>\n\n\n\n<p><code>StandardServer#initInternal<\/code> -&gt; <code>StandardService#initInternal<\/code> -&gt; <code>StandardEngine#initInternal<\/code> -&gt; <strong><code>super.initInternal()<\/code>(ContainerBase#initInternal)<\/strong><\/p>\n\n\n\n<p><code>StandardServer#startInternal<\/code> -&gt; <code>StandardService#startInternal<\/code> -&gt; <code>StandardEngine#startInternal<\/code> -&gt; <strong><code>super.initStart()<\/code>(ContainerBase#startInternal)<\/strong><\/p>\n\n\n\n<p>\u5728Engine\u4ee5\u4e0a\u7684\u4e0a\u5c42\u7ec4\u4ef6\u4e2d\uff0c\u521d\u59cb\u5316\u6216\u8005\u542f\u52a8\u65f6\u90fd\u4e3a\u4e0b\u5c42\u7ec4\u4ef6\u8fdb\u884c\u4e86\u521d\u59cb\u5316\u64cd\u4f5c<\/p>\n\n\n\n<p>\u5982<code>StandardServer#initInternal<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp; &nbsp;for (Service service : services) {<br> &nbsp; &nbsp; &nbsp; &nbsp;service.init();<br> &nbsp;  }<\/pre>\n\n\n\n<p>\u800cStandardEngine\u5374\u76f4\u63a5\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\u4e86\uff0c\u800c\u8fd9\u4e00\u6b65\u5176\u5b9e\u4ec0\u4e48\u4e5f\u6ca1\u5e72\u3002\u5b9e\u9645\u4e0aHOST\u662f\u5728StandardHost#start\u4e2d\u5148\u521d\u59cb\u5316\u518d\u542f\u52a8\u7684\u3002<strong>\u56e0\u4e3a\u9700\u8981\u8003\u8651\u6548\u7387\u95ee\u9898\uff0c\u4e00\u4e2aEngine\u53ef\u80fd\u5bf9\u5e94\u7740\u591a\u4e2a\u4e0d\u540cHOST\u57df\u540d\u4e0b\u7684\u670d\u52a1\uff0c\u5982\u679c\u4e00\u952e\u521d\u59cb\u5316\u4f1a\u8017\u8d39\u5f88\u957f\u65f6\u95f4\uff0c\u6240\u4ee5\u9700\u8981\u5c06\u521d\u59cb\u5316\u7559\u5728\u5404\u4e2aHost\u4e2d\u81ea\u5df1\u6267\u884c\u3002<\/strong>\u540c\u6837\uff0cStandardHost\u548cStandardEngine\u4e00\u6837\u4e5f\u6ca1\u6709\u521d\u59cb\u5316Context\uff0c\u521d\u59cb\u5316\u540c\u6837\u653e\u5728\u4e86Context#start\u4e2d\u3002<\/p>\n\n\n\n<p><strong><code>ContainerBase#startInternal<\/code><\/strong> \u4f1a\u8c03\u7528\u5b50\u5bb9\u5668\u7684start\u65b9\u6cd5\uff0c\u8fd9\u91cc\u4fbf\u662f<strong><code>StandardHost#start<\/code><\/strong>\u4e86\uff0c<strong>\u4e0d\u4f1a\u8c03\u7528<code>StandardHost#init<\/code><\/strong>\u4e86<\/p>\n\n\n\n<p>\u540c\u6837\u5730\uff0cStandardHost#start\u53c8\u4f1a\u53bb\u8c03\u7528super.initStart()\u4e5f\u5c31\u662f<code>ContainerBase#startInternal<\/code>\uff0c\u8c03\u7528\u5b50\u5bb9\u5668Context\u7684start\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u4fbf\u6709\u4e00\u4e2a\u95ee\u9898\uff1a\u5728Server.xml\u4e2d\u662f\u4e0d\u4f1a\u5199Context\u7684\u914d\u7f6e\u7684\uff0c\u8fd9\u4e2aContext\u662f\u54ea\u91cc\u5b9e\u4f8b\u5316\u7684\uff1f<\/p>\n\n\n\n<p>\u5176\u5b9e\uff0c\u5728ParseServerXml\u4e2d\u9664\u4e86\u89e3\u6790\u914d\u7f6e\u6587\u4ef6\uff0c\u8fd8\u4f1a\u4e3aEngine\u3001Host\u914d\u7f6e\u751f\u547d\u5468\u671f\u76d1\u542c\u5668EngineConfig\u548c<strong>HostConfig<\/strong>\u3002<\/p>\n\n\n\n<p>\u5e76\u4e0d\u9700\u8981\u5728server.xml\u4e2d\u914d\u7f6eContext\uff0c\u800c\u662f\u5728ParseServerXml\u4e2d\u7531HostConfig\u81ea\u52a8\u626b\u63cf\u90e8\u7f72\u76ee\u5f55\uff0c\u4ee5context.xml\u6587\u4ef6\u4e3a\u57fa\u7840\u8fdb\u884c\u89e3\u6790\u521b\u5efa\uff08\u5982\u679c\u901a\u8fc7IDE\u542f\u52a8Tomcat\u5e76\u90e8\u7f72\u5e94\u7528\uff0c\u5176Context\u914d\u7f6e\u5c06\u4f1a\u88ab\u52a8\u6001\u66f4\u65b0\u5230server.xml\u4e2d\uff09\u3002<\/p>\n\n\n\n<p>\u540e\u7eed\u7684start\/init\u6d41\u7a0b\u7740\u91cd\u8bb2\u4e00\u4e0b\u5173\u4e8eServlet\/Linster\u7684\u6ce8\u518c\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Servlet\/Linster\u7684\u6ce8\u518c<\/h3>\n\n\n\n<p><strong><code>StandardContext#startInternal()<\/code><\/strong>\u4e2d\uff0c\u89e3\u6790xml\u540e\u4f9d\u6b21\u5bf9Listener\u3001Filter\u3001Servler\u52a0\u8f7d\uff0c\u5176\u4e2d\u521b\u5efaWrapper\u7684\u5173\u952e\u65b9\u6cd5\u662f<strong>configureContext(webXml)<\/strong>\uff0c<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> private void configureContext(WebXml webxml) {<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ As far as possible, process in alphabetical order so it is easy to<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ check everything is present<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Some validation depends on correct public ID<br> &nbsp; &nbsp; &nbsp; &nbsp;context.setPublicId(webxml.getPublicId());<br>... &nbsp; \/\/\u8bbe\u7f6eStandardContext\u53c2\u6570 &nbsp; &nbsp; &nbsp; <br> &nbsp; &nbsp; &nbsp; &nbsp;for (ServletDef servlet : webxml.getServlets().values()) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u521b\u5efaStandardWrapper\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Wrapper wrapper = context.createWrapper();<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (servlet.getLoadOnStartup() != null) {<br> <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u8bbe\u7f6eLoadOnStartup\u5c5e\u6027<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wrapper.setLoadOnStartup(servlet.getLoadOnStartup().intValue());<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (servlet.getEnabled() != null) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wrapper.setEnabled(servlet.getEnabled().booleanValue());<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u8bbe\u7f6eServletName\u5c5e\u6027<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wrapper.setName(servlet.getServletName());<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Map&lt;String,String&gt; params = servlet.getParameterMap();<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (Entry&lt;String, String&gt; entry : params.entrySet()) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wrapper.addInitParameter(entry.getKey(), entry.getValue());<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wrapper.setRunAs(servlet.getRunAs());<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Set&lt;SecurityRoleRef&gt; roleRefs = servlet.getSecurityRoleRefs();<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (SecurityRoleRef roleRef : roleRefs) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wrapper.addSecurityReference(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;roleRef.getName(), roleRef.getLink());<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u8bbe\u7f6eServletClass\u5c5e\u6027<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wrapper.setServletClass(servlet.getServletClass());<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  ...<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wrapper.setOverridable(servlet.isOverridable());<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u5c06\u5305\u88c5\u597d\u7684StandWrapper\u6dfb\u52a0\u8fdbContainerBase\u7684children\u5c5e\u6027\u4e2d<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;context.addChild(wrapper);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Entry&lt;String, String&gt; entry :<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;webxml.getServletMappings().entrySet()) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u6dfb\u52a0\u8def\u5f84\u6620\u5c04<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;context.addServletMappingDecoded(entry.getKey(), entry.getValue());<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp;  ...<br> &nbsp;  }<\/pre>\n\n\n\n<p>\u8c03\u7528StandradContext.createWrapper\u521b\u5efa\u4e00\u4e2aWrapper\uff0c\u7136\u540e\u5c06LoadOnStartup\u3001ServletName\u3001ServletClass\u5c5e\u6027\u8bbe\u7f6e\u8fdb\u53bb\uff0c\u6700\u540e\u6dfb\u52a0\u8fdbContainerBase\u7684children\u5c5e\u6027\u3002<\/p>\n\n\n\n<p>\u63a5\u7740\u5728<code>StandardContext#startInternal<\/code>\u65b9\u6cd5\u901a\u8fc7<code>findChildren()<\/code>\u83b7\u53d6<code>StandardWrapper<\/code>\u7c7b<\/p>\n\n\n\n<p>\u6700\u540e\u4f9d\u6b21\u52a0\u8f7d\u5b8cListener\u3001Filter\u540e\uff0c\u5c31\u901a\u8fc7<code>loadOnStartUp()<\/code>\u65b9\u6cd5\u52a0\u8f7dwrapper\uff0c\u4f1a\u5bf9Wrapper\u5bf9\u8c61\u4e2d<code>loadOnStartup<\/code>\u5c5e\u6027\u7684\u503c\u8fdb\u884c\u5224\u65ad\uff0c\u53ea\u6709\u5927\u4e8e0\u7684\u624d\u4f1a\u88ab\u653e\u5165list\u8fdb\u884c\u540e\u7eed\u7684<code>wrapper.load()<\/code>\u52a0\u8f7d\u8c03\u7528\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u5bf9\u5e94\u7684\u5b9e\u9645\u4e0a\u5c31\u662fTomcat Servlet\u7684\u61d2\u52a0\u8f7d\u673a\u5236\uff0c\u53ef\u4ee5\u901a\u8fc7<code>loadOnStartup<\/code>\u5c5e\u6027\u503c\u6765\u8bbe\u7f6e\u6bcf\u4e2aServlet\u7684\u542f\u52a8\u987a\u5e8f\u3002\u9ed8\u8ba4\u503c\u4e3a-1\uff0c\u53ea\u6709\u5f53Servlet\u88ab\u8c03\u7528\u65f6\u624d\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Connector\u7684\u542f\u52a8\u3001\u521d\u59cb\u5316<\/h3>\n\n\n\n<p>\u8fd9\u91cc\u8fd8\u503c\u5f97\u4e00\u63d0\u7684\u662fConnector\u7ec4\u4ef6\u7684\u542f\u52a8\u548c\u59cb\u5316\uff0c\u8fd9\u91ccConnector\u662f\u4f5c\u4e3aService\u7684\u4e00\u4e2a\u7ec4\u4ef6\uff0c\u548c\u5176\u5b83\u4e00\u5e76\u8fdb\u884c\u521d\u59cb\u5316\u548c\u542f\u52a8\u7684\uff1a<\/p>\n\n\n\n<p><strong>Service.init()<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">connector.init();<\/pre>\n\n\n\n<p><strong>Service.start()<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">connector.start();<\/pre>\n\n\n\n<p>\u8fd9\u5176\u4e2d\u5177\u4f53\u7684\u4ee3\u7801\u5b9e\u73b0\u5c31\u4e0d\u5177\u4f53\u8bf4\u660e\u4e86\uff0c\u6700\u7ec8\u4f1a\u6267\u884c<strong><code>serverSock.socket().bind(addr,getAcceptCount());<\/code><\/strong>\u8fdb\u884c\u7ed1\u5b9a\u7aef\u53e3\u548c\u76d1\u542c\uff0c\u5e76\u5f00\u542f\u4e00\u4e2aPoller\u7684\u5957\u63a5\u5b57\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Tomcat\u5904\u7406\u8bf7\u6c42<\/h2>\n\n\n\n<p>\u8bf7\u6c42\u63a5\u5165\u5230Connecot\u5f00\u542f\u7684Poller\u7ebf\u7a0b\u540e\uff0c\u6700\u540e\u8d70\u5165<strong><code>Http11Processor#service<\/code><\/strong>\uff08http\u534f\u8bae\u662fHttp11Processor\u7c7b\uff0c\u5176\u5b83\u534f\u8bae\u6839\u636e\u914d\u7f6e\u5b9e\u73b0\uff09\u3002<\/p>\n\n\n\n<p>\u5176\u4e2d\u4f1a\u6267\u884c\u4e00\u4e2a\u5173\u952e\u7684\u65b9\u6cd5\u662f<strong>prepareRequest()<\/strong>\uff0c\u5bf9\u8bf7\u6c42url\u8fdb\u884c\u5904\u7406\uff0c\u6700\u7ec8\u5bf9Request\u5bf9\u8c61\u5b8c\u6210\u6700\u540e\u7684\u5305\u88c5\uff0c\u8fd9\u4e2a\u5bf9\u4e8e\u6211\u4eec\u5b66\u5b89\u5168\u7684\u4f1a\u91cd\u8981\u4e00\u70b9\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Http11Processor#prepareRequest<\/h3>\n\n\n\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u5e72\u4e86\u4e24\u4ef6\u4e8b\uff1a1. \u5224\u65ad\u8bf7\u6c42\u884curl\u8def\u5f84\uff0c\u5982\u679c\u4e3a\u7edd\u5bf9\u8def\u5f84\u8f6c\u5316\u4e3a\u76f8\u5bf9\u8def\u5f84 2.\u5224\u65ad\u662f\u5426\u5b58\u5728&#8221;\u8fdd\u89c4\u5b57\u7b26&#8221;<\/p>\n\n\n\n<p>\u7b2c\u4e00\u4ef6\u4e8b\u662f\u4ec0\u4e48\u610f\u601d\u5462\uff1f\u6709\u4e9b\u8bf7\u6c42\u5305\u53ef\u80fd\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">GET http:\/\/localhost\/aaa\/test HTTP\/1.1<br>Host: localhost<br>User-Agent: curl\/8.2.1<br>Accept: *\/*<br>Connection: close<\/pre>\n\n\n\n<p>\u8fd9\u79cd\u60c5\u51b5\u5c31\u8981\u8f6c\u5316\u4e3a\/aaa\/test\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e8c\u4ef6\u4e8b\u5bf9\u4e8e\u5b89\u5168\u6765\u8bf4\u5c31\u76f8\u5bf9\u91cd\u8981\u4e86\uff1a\u4f1a\u5bf9\u5c01\u88c5\u597d\u7684\u76f8\u5bf9\u8def\u5f84uriBC\u8c03\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp;for (int i = uriBC.getStart(); i &lt; uriBC.getEnd(); i++) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (!httpParser.isAbsolutePathRelaxed(uriB[i])) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;badRequest(\"http11processor.request.invalidUri\");<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp;  }<\/pre>\n\n\n\n<p><code>HttpParser#isAbsolutePathRelaxed<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp;public boolean isAbsolutePathRelaxed(int c) {<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Fast for valid user info characters, slower for some incorrect<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ ones<br> &nbsp; &nbsp; &nbsp; &nbsp;try {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return IS_ABSOLUTEPATH_RELAXED[c];<br> &nbsp; &nbsp; &nbsp;  } catch (ArrayIndexOutOfBoundsException ex) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return false;<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<\/pre>\n\n\n\n<p>\u8fd9\u4e2aIS_ABSOLUTEPATH_RELAXED\u662f\u4e00\u4e2a\u5e03\u5c14\u6570\u7ec4\uff0c\u5bf9\u6bcf\u4e2aascii\u5b57\u7b26\u5c01\u88c5\u4e86\u76f8\u5e94\u7684true false\uff0c\u4ece\u6574\u4f53\u903b\u8f91\u4e0a\u6765\u770b\uff0c\u5982\u679c\u4e3afalse\u5219\u4f1a\u8fd4\u56dehttp 400\u62a5\u9519\u3002\u8fd9\u91cc\u603b\u7ed3\u4e86\u4e00\u4e0b<strong>\u5728tomact URL\u4e2d\u5141\u8bb8\u6216\u8005\u4e0d\u5141\u8bb8\u7684\u5b57\u7b26<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0\u201331\u3001127&nbsp;\u662f\u6240\u6709 C0 \u63a7\u5236\u5b57\u7b26\uff0c false<br>\u200b<br>! \" # $ &lt; &gt; &#91; \\ ] ^ \\ { | } ~ \u3002false<br>\u200b<br>\u6240\u6709\u5b57\u6bcd\u3001\u6570\u5b57\uff0c\u4ee5\u53ca - . \/ : = ? @ _ \u5b57\u7b26\u90fd\u4e3a&nbsp;true<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Adapter#service<\/h3>\n\n\n\n<p>\u5bf9\u8bf7\u6c42\u8fdb\u884c\u9884\u5904\u7406\u83b7\u5f97\u4e00\u4e2a\u5b8c\u6574\u7684Request\u5bf9\u8c61\uff0c\u63a5\u7740\u4f1a\u8c03\u7528<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">this.getAdapter().service<\/pre>\n\n\n\n<p>-&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">this.connector.getService().getContainer().getPipeline().getFirst().invoke(request, response);<\/pre>\n\n\n\n<p>\u7531\u6b64\u5f00\u59cb\u4e86Pipline-value\u94fe\u7684\u6267\u884c\u3002<\/p>\n\n\n\n<p><code>StandardEngineValue#invoke<\/code> -&gt; <code>StandardHostValue#invoke<\/code> -&gt; <code>StandardContextValue#invoke<\/code> -&gt; <code>StandardWrapperValue#invoke<\/code><\/p>\n\n\n\n<p>\u4ee5<a href=\"http:\/\/localhost\/admin\/login\">http:\/\/localhost\/admin\/login<\/a>\u4e3a\u4f8b<\/p>\n\n\n\n<p><code>StandardEngineValue#invoke<\/code> \uff1a\u4f1a\u4eceRequest\u5bf9\u8c61\u4e2d\u53d6\u51fa\u8bbf\u95ee\u7684host\uff08localhost\uff09\uff0c\u5982\u679c\u5224\u65ad\u4e0d\u5b58\u5728\uff0c\u8bbe\u7f6e404\u8fd4\u56de\u5305\u3002<\/p>\n\n\n\n<p><code>StandardHostValue#invoke<\/code>\uff1a\u4f1a\u4eceRequest\u5bf9\u8c61\u4e2d\u53d6\u51fa\u8bbf\u95ee\u7684Context (admin)\uff0c\u5982\u679c\u5224\u65ad\u4e0d\u5b58\u5728\uff0c\u8fd4\u56de404\u8fd4\u56de\u5305\u3002<\/p>\n\n\n\n<p><code>StandardContextValue#invoke<\/code>\uff1a\u5148\u5224\u65adRequest\u7684\u8def\u5f84\uff08\/admin\/login\uff09\uff0c\u662f\u5426\u5305\u542b<strong><code>\/META-INF\/<\/code><\/strong>\uff0c\u5982\u679c\u5305\u542b\u76f4\u63a5\u8fd4\u56de404\u8fd4\u56de\u5305\uff1b\u540c\u6837\u4e5f\u4f1a\u53d6\u51faWrapper\uff08login\uff09\uff0c\u5e76\u8fdb\u884c\u7b80\u5355\u7684\u6821\u9a8c\u3002<\/p>\n\n\n\n<p><code>StandardWrapperValue#invoke<\/code>\uff1a\u5bf9Wrapper\u8fdb\u884c\u4e00\u7cfb\u5217\u7684\u5c01\u88c5\uff0c\u8bfb\u53d6\u4e00\u7cfb\u5217\u7684\u914d\u7f6e\uff0c\u83b7\u53d6\u4e86\u5bf9\u5e94\u8def\u5f84\u7684Servlet\uff08\u5982\u679c\u8def\u5f84\u6307\u5411JSP\u6587\u4ef6\uff0c\u4f1a\u8fd4\u56deJspServlet\uff09\uff0c\u6700\u7ec8\u628aServlet\u4ee5\u53ca\u4e00\u4f17\u914d\u7f6e\u5305\u88c5\u8fdb<strong>ApplicationFilterChain<\/strong>\uff0c\u5e76\u4e14\u8c03\u7528<strong><code>filterChain.doFilter(request.getRequest(), response.getResponse());<\/code><\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ApplicationFilterChain#doFilter<\/h3>\n\n\n\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u4ec5\u6267\u884c\u4e86Filter\u94fe\uff0c\u5728\u6700\u540e\uff0c\u8fd8\u4f1a\u6267\u884cservlet.service(request, response); \u4e5f\u5c31\u662f\u8d70\u5165\u76f8\u5e94\u7684Servlet\uff0c\u6267\u884c\u4ee3\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8981\u5b8c\u6574\u8ddfTomcat\u53ef\u4ee5\u770b\u770bhttps:\/\/juejin.cn\/post\/715244663319455337 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/byname6.cn\/index.php\/wp-json\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/byname6.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/byname6.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/byname6.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/byname6.cn\/index.php\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":1,"href":"http:\/\/byname6.cn\/index.php\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":27,"href":"http:\/\/byname6.cn\/index.php\/wp-json\/wp\/v2\/posts\/26\/revisions\/27"}],"wp:attachment":[{"href":"http:\/\/byname6.cn\/index.php\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/byname6.cn\/index.php\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/byname6.cn\/index.php\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}