{"id":24202,"date":"2025-04-19T09:42:04","date_gmt":"2025-04-19T01:42:04","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/24202.html"},"modified":"2025-04-19T09:42:04","modified_gmt":"2025-04-19T01:42:04","slug":"%e7%ac%ac%e4%b8%89%e7%ab%a0%ef%bc%9aminitomcat-%e8%bf%9e%e6%8e%a5%e5%99%a8%ef%bc%88connector%ef%bc%89%e7%bb%84%e4%bb%b6%e5%a4%a7%e6%8f%ad%e7%a7%98%ef%bc%9a%e6%9e%84%e5%bb%ba%e5%bc%ba%e5%a4%a7%e6%9c%8d","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/24202.html","title":{"rendered":"\u7b2c\u4e09\u7ae0\uff1aMiniTomcat-\u8fde\u63a5\u5668\uff08Connector\uff09\u7ec4\u4ef6\u5927\u63ed\u79d8\uff1a\u6784\u5efa\u5f3a\u5927\u670d\u52a1\u5668\u7684\u5173\u952e\u4e00\u6b65\uff01"},"content":{"rendered":"<p>\u7b2c\u4e09\u7ae0&#xff1a;MiniTomcat \u7cfb\u5217 \u2014\u2014 \u5b9e\u73b0\u8fde\u63a5\u5668&#xff08;Connector&#xff09;\u7ec4\u4ef6<\/p>\n<p>\u4e0a\u4e00\u7ae0\u5185\u5bb9&#xff1a;\u7b2c\u4e8c\u7ae0&#xff1a;\u89e3\u6790 HTTP \u8bf7\u6c42&#xff0c;\u652f\u6301\u9759\u6001\u6587\u4ef6-MiniTomcat<\/p>\n<p>\u5404\u4f4d\u6280\u672f\u5927\u795e\u548c\u7231\u597d\u8005\u4eec&#xff0c;\u4eca\u5929\u6211\u4eec\u8981\u6df1\u5165\u63a2\u7d22 MiniTomcat \u4e2d\u4e00\u4e2a\u8d85\u7ea7\u91cd\u8981\u7684\u7ec4\u4ef6\u2014\u2014\u8fde\u63a5\u5668&#xff08;Connector&#xff09;\u3002\u5b83\u5c31\u50cf\u4e00\u5ea7\u6865\u6881&#xff0c;\u8fde\u63a5\u7740\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668&#xff0c;\u8ba9\u4fe1\u606f\u5728\u4e24\u8005\u4e4b\u95f4\u987a\u7545\u6d41\u901a\u3002\u51c6\u5907\u597d\u8ddf\u6211\u4e00\u8d77\u63ed\u5f00\u5b83\u7684\u795e\u79d8\u9762\u7eb1&#xff0c;\u770b\u770b\u5982\u4f55\u901a\u8fc7\u4ee3\u7801\u6784\u5efa\u8fd9\u4e2a\u795e\u5947\u7684\u7ec4\u4ef6&#xff0c;\u8ba9\u6211\u4eec\u7684\u670d\u52a1\u5668\u53d8\u5f97\u66f4\u52a0\u5f3a\u5927\u5427&#xff01;&#x1f4aa;<\/p>\n<h3 id=\"\u4e00\u3001\u8fde\u63a5\u5668&#xff08;connector&#xff09;\u7ec4\u4ef6&#xff1a;minitomcat-\u7684\u901a\u4fe1\u67a2\u7ebd&#x1f9d0;\">\u4e00\u3001\u8fde\u63a5\u5668&#xff08;Connector&#xff09;\u7ec4\u4ef6&#xff1a;MiniTomcat \u7684\u201c\u901a\u4fe1\u67a2\u7ebd\u201d&#x1f9d0;<\/h3>\n<h4 id=\"&#xff08;\u4e00&#xff09;\u8fde\u63a5\u5668\u7684\u4f7f\u547d\">&#xff08;\u4e00&#xff09;\u8fde\u63a5\u5668\u7684\u4f7f\u547d<\/h4>\n<p>\u5728 MiniTomcat \u8fd9\u4e2a\u5c0f\u800c\u5f3a\u5927\u7684\u4e16\u754c\u91cc&#xff0c;\u8fde\u63a5\u5668\u7ec4\u4ef6\u626e\u6f14\u7740\u81f3\u5173\u91cd\u8981\u7684\u89d2\u8272\u3002\u5b83\u5c31\u50cf\u662f\u4e00\u4e2a\u8d85\u7ea7\u667a\u80fd\u7684\u201c\u901a\u4fe1\u67a2\u7ebd\u201d&#xff0c;\u8d1f\u8d23\u7ba1\u7406 HTTP \u8fde\u63a5&#xff0c;\u4e0e\u5ba2\u6237\u7aef\u5efa\u7acb\u7d27\u5bc6\u7684\u8054\u7cfb&#xff0c;\u65f6\u523b\u76d1\u542c\u5e76\u8bfb\u53d6\u4f20\u5165\u7684\u6570\u636e\u5305\u3002\u540c\u65f6&#xff0c;\u5b83\u8fd8\u5177\u5907\u4e00\u9879\u8d85\u5389\u5bb3\u7684\u6280\u80fd\u2014\u2014\u89e3\u8026\u7f51\u7edc\u4f20\u8f93\u548c\u8bf7\u6c42\u5904\u7406\u7684\u903b\u8f91\u3002\u8fd9\u610f\u5473\u7740\u4ec0\u4e48\u5462&#xff1f;\u7b80\u5355\u6765\u8bf4&#xff0c;\u5b83\u8ba9\u7f51\u7edc\u4f20\u8f93\u90e8\u5206\u548c\u5904\u7406\u8bf7\u6c42\u7684\u90e8\u5206\u80fd\u591f\u5404\u81ea\u72ec\u7acb\u53d1\u5c55&#xff0c;\u4e92\u4e0d\u5e72\u6270&#xff0c;\u5c31\u50cf\u4e24\u4e2a\u5e76\u884c\u7684\u8f68\u9053&#xff0c;\u5927\u5927\u63d0\u5347\u4e86\u4ee3\u7801\u7684\u6e05\u6670\u5ea6\u548c\u5bb9\u9519\u6027\u3002\u8fd9\u6837\u4e00\u6765&#xff0c;\u5f53\u6211\u4eec\u9700\u8981\u5bf9\u5176\u4e2d\u4e00\u90e8\u5206\u8fdb\u884c\u4fee\u6539\u6216\u4f18\u5316\u65f6&#xff0c;\u4e0d\u4f1a\u5f71\u54cd\u5230\u53e6\u4e00\u90e8\u5206&#xff0c;\u662f\u4e0d\u662f\u5f88\u795e\u5947\u5462&#xff1f;&#x1f60e;<\/p>\n<h4 id=\"&#xff08;\u4e8c&#xff09;minitomcat-\u9879\u76ee\u7ed3\u6784\u6982\u89c8\">&#xff08;\u4e8c&#xff09;MiniTomcat \u9879\u76ee\u7ed3\u6784\u6982\u89c8<\/h4>\n<p>\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e2a\u5f3a\u5927\u7684\u8fde\u63a5\u5668\u7ec4\u4ef6&#xff0c;\u6211\u4eec\u5728 MiniTomcat \u9879\u76ee\u4e2d\u6784\u5efa\u4e86\u4e00\u7cfb\u5217\u76f8\u5173\u7684\u7c7b\u3002\u4e0b\u9762\u662f\u9879\u76ee\u7684\u57fa\u672c\u4ee3\u7801\u7ed3\u6784&#xff0c;\u5c31\u50cf\u4e00\u5ea7\u5927\u53a6\u7684\u84dd\u56fe&#xff0c;\u6bcf\u4e2a\u7c7b\u90fd\u5728\u81ea\u5df1\u7684\u4f4d\u7f6e\u4e0a\u53d1\u6325\u7740\u91cd\u8981\u4f5c\u7528\u3002&#x1f9d0;<\/p>\n<p>MiniTomcat<br \/>\n\u251c\u2500 src<br \/>\n\u2502  \u251c\u2500 main<br \/>\n\u2502  \u2502  \u251c\u2500 java<br \/>\n\u2502  \u2502  \u2502  \u251c\u2500 com.daicy.minitomcat<br \/>\n\u2502  \u2502  \u2502  \u2502  \u251c\u2500 HttpConnector.java          \/\/ \u8fde\u63a5\u5668\u7c7b&#xff0c;\u8d1f\u8d23\u5efa\u7acb\u8fde\u63a5\u548c\u76d1\u542c<br \/>\n\u2502  \u2502  \u2502  \u2502  \u251c\u2500 HttpProcessor.java          \/\/ \u8bf7\u6c42\u5904\u7406\u5668&#xff0c;\u89e3\u6790\u8bf7\u6c42\u5e76\u6784\u5efa\u54cd\u5e94<br \/>\n\u2502  \u2502  \u2502  \u2502  \u251c\u2500 HttpServer.java             \/\/ \u4e3b\u670d\u52a1\u5668\u7c7b&#xff0c;\u542f\u52a8\u8fde\u63a5\u5668\u5e76\u63a5\u53d7\u8bf7\u6c42<br \/>\n\u2502  \u2502  \u2502  \u2502  \u251c\u2500 Request.java                \/\/ \u8bf7\u6c42\u5c01\u88c5\u7c7b&#xff0c;\u627f\u8f7d\u5ba2\u6237\u7aef\u8bf7\u6c42\u6570\u636e<br \/>\n\u2502  \u2502  \u2502  \u2502  \u251c\u2500 Response.java               \/\/ \u54cd\u5e94\u5c01\u88c5\u7c7b&#xff0c;\u7528\u4e8e\u751f\u6210\u548c\u53d1\u9001\u670d\u52a1\u5668\u54cd\u5e94<br \/>\n\u2502  \u2502  \u2502  \u2502  \u251c\u2500 StaticResourceProcessor.java \/\/ \u9759\u6001\u8d44\u6e90\u5904\u7406\u5668&#xff0c;\u5904\u7406\u9759\u6001\u6587\u4ef6\u8bf7\u6c42<br \/>\n\u2502  \u2502  \u251c\u2500 resources<br \/>\n\u2502  \u251c\u2500 test<br \/>\n\u2502  \u251c\u2500 webroot<br \/>\n\u2502  \u2502  \u251c\u2500 index.html<br \/>\n\u251c\u2500 pom.xml <\/p>\n<h4 id=\"&#xff08;\u4e09&#xff09;\u4ee3\u7801\u5b9e\u73b0\u5927\u63ed\u79d8\">&#xff08;\u4e09&#xff09;\u4ee3\u7801\u5b9e\u73b0\u5927\u63ed\u79d8<\/h4>\n<h5 id=\"1-httpconnector-\u7c7b&#xff1a;\u8fde\u63a5\u7684\u5b88\u62a4\u8005&#x1f46e;\u2642\ufe0f\">1. HttpConnector \u7c7b&#xff1a;\u8fde\u63a5\u7684\u201c\u5b88\u62a4\u8005\u201d&#x1f46e;\u200d\u2642\ufe0f<\/h5>\n<p>HttpConnector \u53ef\u662f\u670d\u52a1\u5668\u7684\u8fde\u63a5\u5927\u95e8&#xff0c;\u5b83\u9759\u9759\u5730\u76d1\u542c\u5728\u6307\u5b9a\u7aef\u53e3&#xff08;\u8fd9\u91cc\u662f 8080 \u7aef\u53e3\u54e6&#xff0c;\u5c31\u50cf\u4e00\u4e2a\u5fe0\u8bda\u7684\u95e8\u536b\u5728\u7b49\u5f85\u5ba2\u4eba\u6765\u8bbf&#xff09;\u3002\u5f53\u5ba2\u6237\u7aef\u8bd5\u56fe\u8fde\u63a5\u65f6&#xff0c;\u5b83\u5c31\u50cf\u70ed\u60c5\u7684\u4e3b\u4eba\u4e00\u6837\u63a5\u53d7\u8fde\u63a5&#xff0c;\u5e76\u8fc5\u901f\u5c06\u8fd9\u4e2a\u8fde\u63a5\u4efb\u52a1\u4ea4\u7ed9 HttpProcessor \u53bb\u5904\u7406\u3002\u4e0b\u9762\u662f\u5b83\u7684\u4ee3\u7801\u5b9e\u73b0&#xff0c;\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002&#x1f4bc;<\/p>\n<p>package com.daicy.minitomcat;<\/p>\n<p>import java.io.IOException;<br \/>\nimport java.net.ServerSocket;<br \/>\nimport java.net.Socket;<\/p>\n<p>public class HttpConnector implements Runnable {<br \/>\n    \/\/ \u5b9a\u4e49\u670d\u52a1\u5668\u76d1\u542c\u7684\u7aef\u53e3\u53f7<br \/>\n    private static final int PORT &#061; 8080;<\/p>\n<p>    \/\/ \u542f\u52a8\u8fde\u63a5\u5668&#xff0c;\u521b\u5efa\u4e00\u4e2a\u65b0\u7ebf\u7a0b\u6765\u8fd0\u884c\u8fde\u63a5\u5668<br \/>\n    public void start() {<br \/>\n        Thread thread &#061; new Thread(this);<br \/>\n        thread.start();<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public void run() {<br \/>\n        try (ServerSocket serverSocket &#061; new ServerSocket(PORT)) {<br \/>\n            System.out.println(&#034;HTTP Connector is running on port &#034; &#043; PORT);<\/p>\n<p>            \/\/ \u8fdb\u5165\u4e00\u4e2a\u65e0\u9650\u5faa\u73af&#xff0c;\u6301\u7eed\u76d1\u542c\u5ba2\u6237\u7aef\u8fde\u63a5<br \/>\n            while (true) {<br \/>\n                \/\/ \u63a5\u53d7\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u8fd4\u56de\u4e00\u4e2a\u4ee3\u8868\u5ba2\u6237\u7aef\u8fde\u63a5\u7684 Socket \u5bf9\u8c61<br \/>\n                Socket clientSocket &#061; serverSocket.accept();<br \/>\n                System.out.println(&#034;Accepted connection from &#034; &#043; clientSocket.getInetAddress());<\/p>\n<p>                \/\/ \u521b\u5efa HttpProcessor \u5b9e\u4f8b\u6765\u5904\u7406\u8be5\u8fde\u63a5<br \/>\n                HttpProcessor processor &#061; new HttpProcessor(clientSocket);<br \/>\n                processor.process();<br \/>\n            }<br \/>\n        } catch (IOException e) {<br \/>\n            e.printStackTrace();<br \/>\n        }<br \/>\n    }<br \/>\n} <\/p>\n<p>[\u8fd9\u91cc\u53ef\u4ee5\u753b\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u610f\u56fe&#xff0c;\u5c55\u793a HttpConnector \u5982\u4f55\u76d1\u542c\u7aef\u53e3\u3001\u63a5\u53d7\u8fde\u63a5\u5e76\u5c06\u4efb\u52a1\u4ea4\u7ed9 HttpProcessor&#xff0c;\u6bd4\u5982\u753b\u4e00\u4e2a\u5e26\u6709\u7aef\u53e3\u53f7\u7684\u670d\u52a1\u5668\u56fe\u6807&#xff0c;\u8fde\u63a5\u7740\u591a\u4e2a\u5ba2\u6237\u7aef\u56fe\u6807&#xff0c;\u65c1\u8fb9\u6709\u7bad\u5934\u6307\u5411 HttpProcessor]<\/p>\n<h5 id=\"2-httpprocessor-\u7c7b&#xff1a;\u8bf7\u6c42\u7684\u89e3\u6790\u5927\u5e08&#x1f9d0;\">2. HttpProcessor \u7c7b&#xff1a;\u8bf7\u6c42\u7684\u201c\u89e3\u6790\u5927\u5e08\u201d&#x1f9d0;<\/h5>\n<p>HttpProcessor \u7c7b\u5c31\u50cf\u662f\u4e00\u4e2a\u8d85\u7ea7\u806a\u660e\u7684\u201c\u89e3\u6790\u5927\u5e08\u201d&#xff0c;\u5b83\u7684\u4efb\u52a1\u662f\u5904\u7406\u4f20\u5165\u7684 HTTP \u8bf7\u6c42\u3002\u5b83\u4f1a\u4ece\u5ba2\u6237\u7aef\u8fde\u63a5\u7684\u8f93\u5165\u6d41\u4e2d\u8bfb\u53d6\u8bf7\u6c42\u6570\u636e&#xff0c;\u7136\u540e\u50cf\u4e00\u4e2a\u7ec6\u81f4\u7684\u5de5\u5320\u4e00\u6837&#xff0c;\u5c06\u8fd9\u4e9b\u6570\u636e\u7cbe\u5fc3\u89e3\u6790\u4e3a Request \u5bf9\u8c61&#xff0c;\u8fd9\u4e2a\u5bf9\u8c61\u5c31\u50cf\u662f\u4e00\u4e2a\u88c5\u6ee1\u8bf7\u6c42\u4fe1\u606f\u7684\u76d2\u5b50\u3002\u4e4b\u540e&#xff0c;\u5b83\u4f1a\u6839\u636e\u8bf7\u6c42\u7684\u5185\u5bb9\u6784\u5efa\u76f8\u5e94\u7684\u54cd\u5e94&#xff0c;\u51c6\u5907\u597d\u9001\u7ed9\u5ba2\u6237\u7aef\u7684\u201c\u793c\u7269\u201d\u3002&#x1f381;<\/p>\n<p>package com.daicy.minitomcat;<\/p>\n<p>import java.io.BufferedReader;<br \/>\nimport java.io.IOException;<br \/>\nimport java.io.InputStream;<br \/>\nimport java.io.InputStreamReader;<br \/>\nimport java.io.OutputStream;<br \/>\nimport java.net.Socket;<\/p>\n<p>public class HttpProcessor {<br \/>\n    private Socket socket;<br \/>\n    \/\/ \u521b\u5efa\u9759\u6001\u8d44\u6e90\u5904\u7406\u5668\u5b9e\u4f8b<br \/>\n    private StaticResourceProcessor staticProcessor &#061; new StaticResourceProcessor();<\/p>\n<p>    public HttpProcessor(Socket socket) {<br \/>\n        this.socket &#061; socket;<br \/>\n    }<\/p>\n<p>    public void process() {<br \/>\n        try (InputStream inputStream &#061; socket.getInputStream();<br \/>\n             OutputStream outputStream &#061; socket.getOutputStream()) {<br \/>\n            \/\/ \u89e3\u6790\u8bf7\u6c42&#xff0c;\u5c06\u8f93\u5165\u6d41\u8f6c\u6362\u4e3a Request \u5bf9\u8c61<br \/>\n            Request request &#061; parseRequest(inputStream);<br \/>\n            \/\/ \u521b\u5efa\u54cd\u5e94\u5bf9\u8c61&#xff0c;\u5173\u8054\u8f93\u51fa\u6d41<br \/>\n            Response response &#061; new Response(outputStream);<br \/>\n            if (null &#061;&#061; request) {<br \/>\n                return;<br \/>\n            }<br \/>\n            String uri &#061; request.getUri();<br \/>\n            \/\/ \u5982\u679c\u8bf7\u6c42\u7684 URI \u662f HTML\u3001CSS \u6216 JS \u6587\u4ef6&#xff0c;\u4f7f\u7528\u9759\u6001\u8d44\u6e90\u5904\u7406\u5668\u5904\u7406<br \/>\n            if (uri.endsWith(&#034;.html&#034;) || uri.endsWith(&#034;.css&#034;) || uri.endsWith(&#034;.js&#034;)) {<br \/>\n                staticProcessor.process(request, response);<br \/>\n            } else {<br \/>\n                staticProcessor.process(request, response);<br \/>\n            }<br \/>\n        } catch (IOException e) {<br \/>\n            e.printStackTrace();<br \/>\n        } finally {<br \/>\n            try {<br \/>\n                \/\/ \u5173\u95ed\u5ba2\u6237\u7aef\u8fde\u63a5<br \/>\n                socket.close();<br \/>\n            } catch (IOException e) {<br \/>\n                e.printStackTrace();<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u4ece\u8f93\u5165\u6d41\u4e2d\u89e3\u6790\u8bf7\u6c42\u884c&#xff0c;\u6784\u5efa Request \u5bf9\u8c61<br \/>\n    private Request parseRequest(InputStream inputStream) throws IOException {<br \/>\n        BufferedReader reader &#061; new BufferedReader(new InputStreamReader(inputStream));<br \/>\n        String requestLine &#061; reader.readLine();<br \/>\n        if (requestLine &#061;&#061; null || requestLine.isEmpty()) {<br \/>\n            return null;<br \/>\n        }<br \/>\n        System.out.println(&#034;Request Line: &#034; &#043; requestLine);<br \/>\n        String[] parts &#061; requestLine.split(&#034; &#034;);<br \/>\n        String method &#061; parts[0];<br \/>\n        String path &#061; parts[1];<br \/>\n        return new Request(method, path);<br \/>\n    }<br \/>\n} <\/p>\n<p>[\u53ef\u4ee5\u753b\u4e00\u4e2a\u56fe&#xff0c;\u5c55\u793a HttpProcessor \u5982\u4f55\u4ece\u8f93\u5165\u6d41\u8bfb\u53d6\u6570\u636e\u3001\u89e3\u6790\u8bf7\u6c42\u5e76\u6784\u5efa\u54cd\u5e94&#xff0c;\u6bd4\u5982\u7528\u7bad\u5934\u8868\u793a\u6570\u636e\u6d41\u5411&#xff0c;\u4ece\u5ba2\u6237\u7aef\u8fde\u63a5\u5230\u8f93\u5165\u6d41&#xff0c;\u518d\u5230\u89e3\u6790\u548c\u6784\u5efa\u54cd\u5e94&#xff0c;\u6700\u540e\u901a\u8fc7\u8f93\u51fa\u6d41\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef]<\/p>\n<h5 id=\"3-staticresourceprocessor-\u7c7b&#xff1a;\u9759\u6001\u8d44\u6e90\u7684\u642c\u8fd0\u5de5&#x1f69a;\">3. StaticResourceProcessor \u7c7b&#xff1a;\u9759\u6001\u8d44\u6e90\u7684\u201c\u642c\u8fd0\u5de5\u201d&#x1f69a;<\/h5>\n<p>StaticResourceProcessor \u7c7b\u5c31\u50cf\u662f\u4e00\u4e2a\u52e4\u52b3\u7684\u201c\u642c\u8fd0\u5de5\u201d&#xff0c;\u4e13\u95e8\u8d1f\u8d23\u67e5\u627e\u548c\u8bfb\u53d6\u670d\u52a1\u5668\u4e0a\u7684\u9759\u6001\u6587\u4ef6&#xff08;\u6bd4\u5982 HTML\u3001CSS\u3001JS \u6587\u4ef6\u7b49&#xff09;&#xff0c;\u5e76\u6784\u5efa\u54cd\u5e94\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002\u5b83\u4f1a\u6839\u636e\u8bf7\u6c42\u7684 URI \u627e\u5230\u5bf9\u5e94\u7684\u6587\u4ef6&#xff0c;\u5982\u679c\u6587\u4ef6\u5b58\u5728&#xff0c;\u5c31\u628a\u6587\u4ef6\u5185\u5bb9\u6253\u5305\u6210\u54cd\u5e94\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef&#xff1b;\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728&#xff0c;\u5c31\u53d1\u9001\u4e00\u4e2a 404 \u9519\u8bef\u54cd\u5e94&#xff0c;\u544a\u8bc9\u5ba2\u6237\u7aef\u201c\u5bf9\u4e0d\u8d77&#xff0c;\u4f60\u8981\u7684\u8d44\u6e90\u6ca1\u627e\u5230\u201d\u3002&#x1f605;<\/p>\n<p>package com.daicy.minitomcat;<\/p>\n<p>import java.io.File;<br \/>\nimport java.io.FileInputStream;<br \/>\nimport java.io.IOException;<br \/>\nimport java.io.OutputStream;<br \/>\nimport java.net.URL;<\/p>\n<p>import static com.daicy.minitomcat.HttpServer.WEB_ROOT;<\/p>\n<p>public class StaticResourceProcessor {<br \/>\n    public void process(Request request, Response response) {<br \/>\n        try {<br \/>\n            OutputStream outputStream &#061; response.getOutputStream();<br \/>\n            \/\/ \u83b7\u53d6\u8bf7\u6c42\u7684\u9759\u6001\u6587\u4ef6\u8def\u5f84<br \/>\n            String path &#061; request.getUri();<br \/>\n            URL url &#061; HttpServer.class.getClassLoader().getResource(WEB_ROOT &#043; path);<br \/>\n            if (null &#061;&#061; url) {<br \/>\n                sendResponse(outputStream, 404, &#034;Not Found&#034;, &#034;The requested resource was not found.&#034;);<br \/>\n                return;<br \/>\n            }<br \/>\n            File file &#061; new File(url.getPath());<br \/>\n            if (file.exists() &amp;&amp;!file.isDirectory()) {<br \/>\n                sendFileResponse(outputStream, file);<br \/>\n            } else {<br \/>\n                sendResponse(outputStream, 404, &#034;Not Found&#034;, &#034;The requested resource was not found.&#034;);<br \/>\n            }<br \/>\n        } catch (IOException e) {<br \/>\n            e.printStackTrace();<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u53d1\u9001\u666e\u901a\u6587\u672c\u54cd\u5e94&#xff0c;\u5982\u9519\u8bef\u4fe1\u606f\u7b49<br \/>\n    private static void sendResponse(OutputStream outputStream, int statusCode, String statusText, String message) throws IOException {<br \/>\n        PrintWriter writer &#061; new PrintWriter(outputStream, true);<br \/>\n        writer.println(&#034;HTTP\/1.1 &#034; &#043; statusCode &#043; &#034; &#034; &#043; statusText);<br \/>\n        writer.println(&#034;Content-Type: text\/html; charset&#061;UTF-8&#034;);<br \/>\n        writer.println();<br \/>\n        writer.println(&#034;&lt;html&gt;&lt;body&gt;&lt;h1&gt;&#034; &#043; statusCode &#043; &#034; &#034; &#043; statusText &#043; &#034;&lt;\/h1&gt;&lt;p&gt;&#034; &#043; message &#043; &#034;&lt;\/p&gt;&lt;\/body&gt;&lt;\/html&gt;&#034;);<br \/>\n    }<\/p>\n<p>    \/\/ \u53d1\u9001\u6587\u4ef6\u54cd\u5e94&#xff0c;\u5c06\u6587\u4ef6\u5185\u5bb9\u8bfb\u53d6\u5e76\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef<br \/>\n    private static void sendFileResponse(OutputStream outputStream, File file) throws IOException {<br \/>\n        PrintWriter writer &#061; new PrintWriter(outputStream, true);<br \/>\n        writer.println(&#034;HTTP\/1.1 200 OK&#034;);<br \/>\n        writer.println(&#034;Content-Type: &#034; &#043; getContentType(file));<br \/>\n        writer.println(&#034;Content-Length: &#034; &#043; file.length());<br \/>\n        writer.println();<br \/>\n        try (FileInputStream fis &#061; new FileInputStream(file)) {<br \/>\n            byte[] buffer &#061; new byte[1024];<br \/>\n            int bytesRead;<br \/>\n            while ((bytesRead &#061; fis.read(buffer))!&#061; -1) {<br \/>\n                outputStream.write(buffer, 0, bytesRead);<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u6839\u636e\u6587\u4ef6\u540e\u7f00\u540d\u786e\u5b9a Content-Type<br \/>\n    private static String getContentType(File file) {<br \/>\n        String name &#061; file.getName().toLowerCase();<br \/>\n        if (name.endsWith(&#034;.html&#034;) || name.endsWith(&#034;.htm&#034;)) {<br \/>\n            return &#034;text\/html&#034;;<br \/>\n        } else if (name.endsWith(&#034;.css&#034;)) {<br \/>\n            return &#034;text\/css&#034;;<br \/>\n        } else if (name.endsWith(&#034;.js&#034;)) {<br \/>\n            return &#034;application\/javascript&#034;;<br \/>\n        } else if (name.endsWith(&#034;.jpg&#034;) || name.endsWith(&#034;.jpeg&#034;)) {<br \/>\n            return &#034;image\/jpeg&#034;;<br \/>\n        } else if (name.endsWith(&#034;.png&#034;)) {<br \/>\n            return &#034;image\/png&#034;;<br \/>\n        } else {<br \/>\n            return &#034;application\/octet-stream&#034;;<br \/>\n        }<br \/>\n    }<br \/>\n} <\/p>\n<p>[\u53ef\u4ee5\u753b\u4e00\u4e2a\u7b80\u5355\u7684\u56fe&#xff0c;\u5c55\u793a StaticResourceProcessor \u5982\u4f55\u67e5\u627e\u6587\u4ef6\u3001\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\u5e76\u53d1\u9001\u76f8\u5e94\u7684\u54cd\u5e94&#xff0c;\u6bd4\u5982\u7528\u6587\u4ef6\u56fe\u6807\u8868\u793a\u9759\u6001\u6587\u4ef6&#xff0c;\u7bad\u5934\u8868\u793a\u67e5\u627e\u548c\u8bfb\u53d6\u8fc7\u7a0b]<\/p>\n<h5 id=\"4-request-\u548c-response-\u7c7b&#xff1a;\u8bf7\u6c42\u4e0e\u54cd\u5e94\u7684\u5bb9\u5668&#x1f4e6;\">4. Request \u548c Response \u7c7b&#xff1a;\u8bf7\u6c42\u4e0e\u54cd\u5e94\u7684\u201c\u5bb9\u5668\u201d&#x1f4e6;<\/h5>\n<p>Request \u7c7b\u5c31\u50cf\u662f\u4e00\u4e2a\u5c0f\u5de7\u7684\u201c\u5bb9\u5668\u201d&#xff0c;\u5b83\u7684\u4efb\u52a1\u662f\u5c01\u88c5\u5ba2\u6237\u7aef\u53d1\u9001\u8fc7\u6765\u7684\u8bf7\u6c42\u6570\u636e&#xff0c;\u6bd4\u5982\u8bf7\u6c42\u65b9\u6cd5&#xff08;GET\u3001POST \u7b49&#xff09;\u548c\u8bf7\u6c42\u8def\u5f84\u3002\u800c Response \u7c7b\u5219\u662f\u670d\u52a1\u5668\u7528\u6765\u751f\u6210\u548c\u53d1\u9001\u54cd\u5e94\u7684\u201c\u5f97\u529b\u52a9\u624b\u201d&#xff0c;\u5b83\u5173\u8054\u7740\u8f93\u51fa\u6d41&#xff0c;\u901a\u8fc7\u8fd9\u4e2a\u8f93\u51fa\u6d41\u5c06\u670d\u52a1\u5668\u7684\u54cd\u5e94\u6570\u636e\u53d1\u9001\u56de\u5ba2\u6237\u7aef\u3002&#x1f3af;<\/p>\n<p>package com.daicy.minitomcat;<\/p>\n<p>public class Request {<br \/>\n    private String method;<br \/>\n    private String path;<\/p>\n<p>    public Request(String method, String path) {<br \/>\n        this.method &#061; method;<br \/>\n        this.path &#061; path;<br \/>\n    }<\/p>\n<p>    public String getMethod() {<br \/>\n        return method;<br \/>\n    }<\/p>\n<p>    public String getPath() {<br \/>\n        return path;<br \/>\n    }<br \/>\n}<br \/>\npackage com.daicy.minitomcat;<\/p>\n<p>import java.io.OutputStream;<\/p>\n<p>public class Response {<br \/>\n    private OutputStream outputStream;<\/p>\n<p>    public Response(OutputStream outputStream) {<br \/>\n        this.outputStream &#061; outputStream;<br \/>\n    }<\/p>\n<p>    public OutputStream getOutputStream() {<br \/>\n        return outputStream;<br \/>\n    }<br \/>\n} <\/p>\n<h4 id=\"&#xff08;\u56db&#xff09;\u4ee3\u7801\u89e3\u6790\u4e0e\u667a\u6167\u7ed3\u6676&#x1f9e0;\">&#xff08;\u56db&#xff09;\u4ee3\u7801\u89e3\u6790\u4e0e\u667a\u6167\u7ed3\u6676&#x1f9e0;<\/h4>\n<li>\u8fde\u63a5\u5668\u7684\u521b\u5efa\u667a\u6167\n<ul>\n<li>HttpConnector \u5de7\u5999\u5730\u76d1\u542c\u5728\u6307\u5b9a\u7aef\u53e3&#xff0c;\u5c31\u50cf\u4e00\u4e2a\u654f\u9510\u7684\u96f7\u8fbe&#xff0c;\u968f\u65f6\u6355\u6349\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42\u3002\u4e00\u65e6\u6709\u8fde\u63a5\u5efa\u7acb&#xff0c;\u5b83\u7acb\u5373\u521b\u5efa\u65b0\u7684 HttpProcessor \u7ebf\u7a0b\u6765\u5904\u7406\u8be5\u8fde\u63a5&#xff0c;\u8fd9\u79cd\u65b9\u5f0f\u5b9e\u73b0\u4e86\u8fde\u63a5\u5668\u4e0e\u8bf7\u6c42\u5904\u7406\u5668\u7684\u5b8c\u7f8e\u89e3\u8026\u3002\u6bcf\u4e2a\u8fde\u63a5\u7684\u5904\u7406\u903b\u8f91\u90fd\u72ec\u7acb\u8fd0\u884c&#xff0c;\u4e92\u4e0d\u5e72\u6270&#xff0c;\u5c31\u50cf\u9ad8\u901f\u516c\u8def\u4e0a\u7684\u591a\u8f66\u9053&#xff0c;\u8f66\u8f86&#xff08;\u8bf7\u6c42&#xff09;\u53ef\u4ee5\u5e76\u884c\u5904\u7406&#xff0c;\u5927\u5927\u63d0\u9ad8\u4e86\u670d\u52a1\u5668\u7684\u5904\u7406\u6548\u7387\u548c\u5e76\u53d1\u80fd\u529b\u3002&#x1f697;&#x1f697;&#x1f697;<\/li>\n<\/ul>\n<\/li>\n<li>\u8bf7\u6c42\u89e3\u6790\u7684\u7cbe\u5999\u4e4b\u5904\n<ul>\n<li>HttpProcessor \u901a\u8fc7\u8bfb\u53d6\u8f93\u5165\u6d41\u4e2d\u7684\u8bf7\u6c42\u884c&#xff0c;\u7136\u540e\u50cf\u4e00\u4e2a\u7ecf\u9a8c\u4e30\u5bcc\u7684\u5206\u6790\u5e08\u4e00\u6837&#xff0c;\u7cbe\u51c6\u5730\u5c06\u5176\u89e3\u6790\u4e3a Request \u5bf9\u8c61&#xff0c;\u63d0\u53d6\u51fa\u8bf7\u6c42\u65b9\u6cd5\u548c\u8def\u5f84\u7b49\u5173\u952e\u4fe1\u606f\u3002\u8fd9\u79cd\u7b80\u5316\u7684\u8bf7\u6c42\u89e3\u6790\u903b\u8f91&#xff0c;\u65e2\u9ad8\u6548\u53c8\u7a33\u5b9a&#xff0c;\u6709\u6548\u907f\u514d\u4e86\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u5bfc\u81f4\u7684\u6df7\u4e71\u4e0e\u9519\u8bef\u3002\u800c\u4e14&#xff0c;\u5b83\u80fd\u4f9d\u636e\u8bf7\u6c42\u8def\u5f84\u5feb\u901f\u5206\u6d41&#xff0c;\u9488\u5bf9\u4e0d\u540c\u7c7b\u578b\u8d44\u6e90&#xff08;\u5982\u9759\u6001\u6587\u4ef6&#xff09;\u4ea4\u7ed9\u4e13\u4e1a\u7684 StaticResourceProcessor \u5904\u7406&#xff0c;\u5982\u540c\u5de5\u5382\u91cc\u7cbe\u51c6\u7684\u5206\u62e3\u5de5\u5e8f&#xff0c;\u8ba9\u6574\u4e2a\u6d41\u7a0b\u6709\u6761\u4e0d\u7d0a&#xff0c;\u4fdd\u969c\u670d\u52a1\u5668\u5feb\u901f\u54cd\u5e94\u5ba2\u6237\u7aef\u8bc9\u6c42\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u9759\u6001\u8d44\u6e90\u5904\u7406\u7684\u8d34\u5fc3\u8bbe\u8ba1\n<ul>\n<li>StaticResourceProcessor \u7edd\u5bf9\u662f\u201c\u7ec6\u8282\u63a7\u201d\u3002\u5b83\u4f9d\u636e\u8bf7\u6c42\u7684 URI \u7cbe\u51c6\u5b9a\u4f4d\u8d44\u6e90&#xff0c;\u5148\u901a\u8fc7\u7c7b\u52a0\u8f7d\u5668\u67e5\u627e\u5bf9\u5e94\u6587\u4ef6\u8def\u5f84&#xff0c;\u4e00\u65e6\u6587\u4ef6\u4e0d\u5b58\u5728&#xff0c;\u5373\u523b\u53d1\u9001 404 \u544a\u77e5\u5ba2\u6237\u7aef&#xff0c;\u6beb\u4e0d\u62d6\u6c93\u3002\u800c\u9762\u5bf9\u5b58\u5728\u7684\u6587\u4ef6&#xff0c;\u53c8\u6839\u636e\u540e\u7f00\u540d\u667a\u80fd\u5224\u65ad Content-Type&#xff0c;\u4ece\u6587\u672c\u6587\u4ef6\u5230\u56fe\u7247\u683c\u5f0f&#xff0c;\u90fd\u80fd\u59a5\u5584\u5305\u88c5\u54cd\u5e94\u5185\u5bb9&#xff0c;\u5c06\u6587\u4ef6\u5185\u5bb9\u9010\u5757\u8bfb\u53d6\u5e76\u53d1\u9001&#xff0c;\u6070\u4f3c\u4e00\u4f4d\u8d34\u5fc3\u7684\u5feb\u9012\u5458&#xff0c;\u628a\u9759\u6001\u8d44\u6e90\u8fd9\u4e2a\u201c\u5305\u88f9\u201d\u7a33\u7a33\u5f53\u5f53\u5730\u9001\u5230\u5ba2\u6237\u7aef\u201c\u624b\u4e2d\u201d&#xff0c;\u8ba9\u7f51\u9875\u5c55\u793a\u3001\u811a\u672c\u8fd0\u884c\u7b49\u6240\u9700\u8d44\u6e90\u53ca\u65f6\u5c31\u4f4d\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u8bf7\u6c42\u4e0e\u54cd\u5e94\u5c01\u88c5\u7684\u5de7\u601d\n<ul>\n<li>Request \u548c Response \u7c7b\u867d\u770b\u4f3c\u7b80\u5355&#xff0c;\u5b9e\u5219\u662f\u201c\u56db\u4e24\u62e8\u5343\u65a4\u201d\u3002Request \u628a\u6742\u4e71\u96f6\u6563\u7684\u5ba2\u6237\u7aef\u521d\u59cb\u4fe1\u606f\u6536\u7eb3\u89c4\u6574&#xff0c;\u8ba9\u540e\u7eed\u5904\u7406\u6d41\u7a0b\u80fd\u6e05\u6670\u201c\u8bfb\u6863\u201d\u63d0\u53d6\u5173\u952e\u6307\u4ee4&#xff1b;Response \u5173\u8054\u8f93\u51fa\u6d41&#xff0c;\u4e3a\u670d\u52a1\u5668\u56de\u5e94\u642d\u5efa\u4f20\u8f93\u901a\u9053&#xff0c;\u5b83\u4eec\u5982\u540c\u69ab\u536f\u7ed3\u6784\u91cc\u7d27\u5bc6\u5951\u5408\u7684\u90e8\u4ef6&#xff0c;\u914d\u5408\u5176\u4ed6\u7ec4\u4ef6&#xff0c;\u4fdd\u969c\u4fe1\u606f\u4ea4\u4e92\u5b8c\u6574\u95ed\u73af&#xff0c;\u4ece\u63a5\u6536\u5230\u56de\u5e94&#xff0c;\u6784\u7b51\u6d41\u7545\u4ea4\u4e92\u94fe\u8def\u3002<\/li>\n<\/ul>\n<\/li>\n<h3 id=\"\u4e8c\u3001\u8fde\u63a5\u5668\u7ec4\u4ef6\u7684\u4f18\u52bf\u4e0e\u5e94\u7528\u573a\u666f&#x1f4a1;\">\u4e8c\u3001\u8fde\u63a5\u5668\u7ec4\u4ef6\u7684\u4f18\u52bf\u4e0e\u5e94\u7528\u573a\u666f&#x1f4a1;<\/h3>\n<h4 id=\"&#xff08;\u4e00&#xff09;\u4f18\u52bf\u5c3d\u663e\">&#xff08;\u4e00&#xff09;\u4f18\u52bf\u5c3d\u663e<\/h4>\n<li>\u9ad8\u6548\u5e76\u53d1\u5904\u7406&#xff1a;\u51ed\u501f HttpConnector \u4e0e HttpProcessor \u7684\u5206\u5de5\u534f\u4f5c&#xff0c;\u591a\u7ebf\u7a0b\u5e76\u884c\u8fd0\u4f5c&#xff0c;\u670d\u52a1\u5668\u53ef\u540c\u65f6\u5e94\u5bf9\u6d77\u91cf\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u5728\u9ad8\u6d41\u91cf\u573a\u666f\u4e0b&#xff0c;\u6bd4\u5982\u70ed\u95e8\u7535\u5546\u4fc3\u9500\u6d3b\u52a8\u65f6&#xff0c;\u7f51\u7ad9\u9875\u9762\u9891\u7e41\u88ab\u8bbf\u95ee\u3001\u5927\u91cf\u56fe\u7247\u548c\u811a\u672c\u52a0\u8f7d&#xff0c;\u90fd\u80fd\u8f7b\u677e\u9a7e\u9a6d&#xff0c;\u4e0d\u4f1a\u8ba9\u7528\u6237\u9677\u5165\u6f2b\u957f\u7b49\u5f85&#xff0c;\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/li>\n<li>\u4ee3\u7801\u6e05\u6670\u53ef\u7ef4\u62a4&#xff1a;\u5404\u7ec4\u4ef6\u5404\u53f8\u5176\u804c\u3001\u903b\u8f91\u89e3\u8026&#xff0c;\u4fee\u6539\u4e00\u5904&#xff08;\u5982\u4f18\u5316 StaticResourceProcessor \u7684\u6587\u4ef6\u8bfb\u53d6\u65b9\u5f0f&#xff09;\u4e0d\u4f1a\u7275\u4e00\u53d1\u800c\u52a8\u5168\u8eab&#xff0c;\u5f00\u53d1\u4eba\u5458\u540e\u7eed\u6392\u67e5\u95ee\u9898\u3001\u589e\u6dfb\u529f\u80fd&#xff0c;\u5faa\u7740\u6e05\u6670\u67b6\u6784\u8109\u7edc\u5373\u53ef&#xff0c;\u4e0d\u50cf\u6742\u4e71\u4ee3\u7801\u201c\u8ff7\u5bab\u201d\u8ba9\u4eba\u6655\u5934\u8f6c\u5411&#xff0c;\u957f\u671f\u7ef4\u62a4\u6210\u672c\u5927\u5e45\u964d\u4f4e\u3002<\/li>\n<li>\u7075\u6d3b\u9002\u914d\u62d3\u5c55&#xff1a;\u67b6\u6784\u8bbe\u8ba1\u9884\u7559\u62d3\u5c55\u7a7a\u95f4&#xff0c;\u82e5\u672a\u6765\u8981\u652f\u6301\u65b0\u7684\u534f\u8bae\u6216\u7279\u6b8a\u8bf7\u6c42\u5904\u7406\u903b\u8f91&#xff0c;\u5728\u5bf9\u5e94\u7ec4\u4ef6&#xff08;\u50cf HttpProcessor \u91cc\u589e\u6dfb\u89e3\u6790\u5206\u652f&#xff09;\u8c03\u6574\u5373\u53ef&#xff0c;\u597d\u6bd4\u7ed9\u5927\u53a6\u9884\u7559\u4e86\u6269\u5efa\u63a5\u53e3&#xff0c;\u80fd\u4e0e\u65f6\u4ff1\u8fdb\u5951\u5408\u4e1a\u52a1\u53d1\u5c55\u3001\u6280\u672f\u8fed\u4ee3\u3002<\/li>\n<h4 id=\"&#xff08;\u4e8c&#xff09;\u5e94\u7528\u573a\u666f\u5e7f\u6cdb\">&#xff08;\u4e8c&#xff09;\u5e94\u7528\u573a\u666f\u5e7f\u6cdb<\/h4>\n<li>\u5c0f\u578b Web \u9879\u76ee\u8d77\u6b65&#xff1a;\u5bf9\u4e8e\u521d\u521b\u516c\u53f8\u642d\u5efa\u7b80\u5355\u5c55\u793a\u578b\u5b98\u7f51&#xff0c;\u6216\u662f\u5f00\u53d1\u8005\u4e2a\u4eba\u7ec3\u624b\u505a\u7684\u535a\u5ba2\u7ad9\u70b9&#xff0c;MiniTomcat \u8fde\u63a5\u5668\u52a9\u529b\u5feb\u901f\u642d\u5efa\u672c\u5730\u670d\u52a1\u5668&#xff0c;\u5904\u7406\u9875\u9762\u8bbf\u95ee\u3001\u8d44\u6e90\u52a0\u8f7d&#xff0c;\u7528\u7cbe\u7b80\u4ee3\u7801\u3001\u660e\u6670\u67b6\u6784\u5b9e\u73b0\u57fa\u7840\u529f\u80fd&#xff0c;\u65e0\u9700\u590d\u6742\u5927\u578b\u670d\u52a1\u5668\u6846\u67b6\u201c\u5927\u6750\u5c0f\u7528\u201d\u3002<\/li>\n<li>\u5185\u90e8\u5de5\u5177\u7cfb\u7edf\u652f\u6491&#xff1a;\u4f01\u4e1a\u5185\u90e8\u8bf8\u5982\u5458\u5de5\u8003\u52e4\u6253\u5361\u7cfb\u7edf\u3001\u7b80\u6613\u6587\u6863\u7ba1\u7406\u5de5\u5177\u7b49&#xff0c;\u4f7f\u7528\u8be5\u8fde\u63a5\u5668\u80fd\u9ad8\u6548\u670d\u52a1\u6709\u9650\u4f46\u9891\u7e41\u4ea4\u4e92\u7684\u5185\u90e8\u5458\u5de5\u7fa4\u4f53&#xff0c;\u7a33\u5b9a\u4f20\u8f93\u6570\u636e\u3001\u5feb\u901f\u54cd\u5e94\u64cd\u4f5c&#xff0c;\u4fdd\u969c\u65e5\u5e38\u529e\u516c\u6d41\u7a0b\u987a\u6ed1\u8fd0\u8f6c\u3002<\/li>\n<li>\u5b66\u4e60\u7814\u7a76\u670d\u52a1\u5668\u5e95\u5c42\u673a\u5236&#xff1a;\u9ad8\u6821\u8ba1\u7b97\u673a\u4e13\u4e1a\u5b66\u751f\u3001\u6280\u672f\u7231\u597d\u8005\u94bb\u7814\u670d\u52a1\u5668\u539f\u7406&#xff0c;MiniTomcat \u8fde\u63a5\u5668\u662f\u7edd\u4f73\u201c\u6559\u5177\u201d&#xff0c;\u900f\u8fc7\u5256\u6790\u3001\u4fee\u6539\u4ee3\u7801&#xff0c;\u76f4\u89c2\u638c\u63e1 HTTP \u8fde\u63a5\u7ba1\u7406\u3001\u8bf7\u6c42\u54cd\u5e94\u6d41\u7a0b&#xff0c;\u4e3a\u6df1\u5165\u63a2\u7d22\u5927\u578b\u670d\u52a1\u5668&#xff08;\u5982 Tomcat \u672c\u5c0a&#xff09;\u7b51\u7262\u77e5\u8bc6\u6839\u57fa\u3002<\/li>\n<h3 id=\"\u4e09\u3001\u4f18\u5316\u4e0e\u5c55\u671b&#x1f308;\">\u4e09\u3001\u4f18\u5316\u4e0e\u5c55\u671b&#x1f308;<\/h3>\n<li>\u6027\u80fd\u4f18\u5316\u8def\u5f84\n<ul>\n<li>\u8fde\u63a5\u6c60\u4f18\u5316&#xff1a;\u5f53\u524d\u8fde\u63a5\u5efa\u7acb\u9500\u6bc1\u9891\u7e41&#xff0c;\u53ef\u5f15\u5165\u8fde\u63a5\u6c60\u6280\u672f&#xff0c;\u9884\u5148\u521b\u5efa\u4e00\u5b9a\u6570\u91cf\u8fde\u63a5\u653e\u4e8e\u201c\u6c60\u5b50\u201d&#xff0c;\u65b0\u8bf7\u6c42\u6309\u9700\u53d6\u7528\u3001\u7528\u5b8c\u5f52\u8fd8&#xff0c;\u51cf\u5c11\u8fde\u63a5\u521b\u5efa\u5f00\u9500&#xff0c;\u63d0\u5347\u541e\u5410\u91cf&#xff0c;\u7c7b\u4f3c\u996d\u5e97\u63d0\u524d\u5907\u597d\u9910\u5177&#xff0c;\u98df\u5ba2\u968f\u5230\u968f\u7528&#xff0c;\u907f\u514d\u4e34\u65f6\u6d17\u5237\u51c6\u5907\u803d\u8bef\u65f6\u95f4\u3002<\/li>\n<li>\u7f13\u5b58\u7b56\u7565\u5347\u7ea7&#xff1a;\u5bf9\u9759\u6001\u8d44\u6e90\u5904\u7406&#xff0c;\u589e\u8bbe\u66f4\u667a\u80fd\u7f13\u5b58&#xff0c;\u4f9d\u636e\u6587\u4ef6\u70ed\u5ea6\u3001\u66f4\u65b0\u9891\u7387\u51b3\u5b9a\u7f13\u5b58\u65f6\u957f\u4e0e\u5c42\u7ea7&#xff0c;\u70ed\u95e8\u4e14\u5c11\u53d8\u6587\u4ef6&#xff08;\u5982\u901a\u7528 CSS \u6837\u5f0f\u8868&#xff09;\u957f\u671f\u7f13\u5b58&#xff0c;\u964d\u4f4e\u78c1\u76d8\u8bfb\u53d6&#xff0c;\u52a0\u901f\u54cd\u5e94&#xff0c;\u5982\u540c\u628a\u5e38\u770b\u4e66\u7c4d\u653e\u684c\u9762\u968f\u624b\u53ef\u53d6&#xff0c;\u800c\u975e\u6bcf\u6b21\u56de\u4e66\u67b6\u7ffb\u627e\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u529f\u80fd\u62d3\u5c55\u9050\u60f3\n<ul>\n<li>\u652f\u6301\u66f4\u591a\u534f\u8bae&#xff1a;\u4e0d\u6b62\u4e8e HTTP&#xff0c;\u8003\u8651\u62d3\u5c55\u652f\u6301 WebSocket \u7b49\u65b0\u5174\u534f\u8bae&#xff0c;\u6ee1\u8db3\u5b9e\u65f6\u4ea4\u4e92\u9700\u6c42&#xff0c;\u50cf\u5728\u7ebf\u804a\u5929\u3001\u80a1\u7968\u5b9e\u65f6\u884c\u60c5\u63a8\u9001\u573a\u666f&#xff0c;\u8ba9\u670d\u52a1\u5668\u80fd\u9002\u914d\u591a\u5143\u7f51\u7edc\u4ea4\u4e92&#xff0c;\u62d3\u5bbd\u670d\u52a1\u8fb9\u754c\u3002<\/li>\n<li>\u5b89\u5168\u52a0\u56fa\u5c42\u9762&#xff1a;\u878d\u5165\u52a0\u5bc6\u4f20\u8f93\u3001\u9632\u653b\u51fb\u6a21\u5757&#xff0c;\u9632\u8303\u5e38\u89c1\u7f51\u7edc\u653b\u51fb&#xff08;\u5982 XSS\u3001CSRF&#xff09;&#xff0c;\u5728\u6570\u636e\u654f\u611f\u7684\u7535\u5546\u652f\u4ed8\u3001\u533b\u7597\u4fe1\u606f\u7cfb\u7edf\u5e94\u7528\u573a\u666f&#xff0c;\u5b88\u62a4\u4f20\u8f93\u4fe1\u606f\u5b89\u5168&#xff0c;\u4e3a\u670d\u52a1\u5668\u7a7f\u4e0a\u575a\u56fa\u201c\u94e0\u7532\u201d\u3002<\/li>\n<\/ul>\n<\/li>\n<p>\u603b\u4e4b&#xff0c;MiniTomcat \u8fde\u63a5\u5668\u7ec4\u4ef6\u4ee5\u5176\u7cbe\u5de7\u8bbe\u8ba1\u3001\u5b9e\u7528\u529f\u80fd\u5728\u6784\u5efa\u670d\u52a1\u5668\u8def\u4e0a\u8e0f\u51fa\u575a\u5b9e\u4e00\u6b65&#xff0c;\u6301\u7eed\u6316\u6398\u4f18\u5316\u3001\u62d3\u5c55\u6f5c\u80fd&#xff0c;\u5b9a\u80fd\u5728\u66f4\u591a\u590d\u6742\u591a\u53d8\u573a\u666f\u5927\u653e\u5f02\u5f69&#xff0c;\u4e3a\u7f51\u7edc\u4ea4\u4e92\u4e16\u754c\u7b51\u7262\u6839\u57fa\u3001\u8f93\u9001\u52a8\u529b&#xff0c;\u5404\u4f4d\u6280\u672f\u8fbe\u4eba\u4e0d\u59a8\u6df1\u6316\u7ec6\u7a76\u3001\u52a8\u624b\u5b9e\u8df5&#xff0c;\u89e3\u9501\u66f4\u591a\u60ca\u559c\u4e0e\u53ef\u80fd&#xff01;<\/p>\n<p>\u4e13\u9898\u94fe\u63a5&#xff1a;\u5206\u7c7b&#xff1a;MiniTomcat &#8211; \u4ee3\u8001\u5e08\u7684\u535a\u5ba2<\/p>\n<p>\u9879\u76ee\u6e90\u4ee3\u7801\u5730\u5740&#xff1a;<\/p>\n<p>https:\/\/github.com\/daichangya\/MiniTomcat\/tree\/chapter3\/mini-tomcat<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.5k\u6b21\uff0c\u70b9\u8d5e40\u6b21\uff0c\u6536\u85cf10\u6b21\u3002\u5728\u672c\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u5f15\u5165\u8fde\u63a5\u5668\uff08Connector\uff09\u7ec4\u4ef6\uff0c\u7528\u4e8e\u7ba1\u7406 HTTP \u8fde\u63a5\u548c\u8bf7\u6c42\u6570\u636e\u7684\u89e3\u6790\u3002\u8fde\u63a5\u5668\u7684\u4f5c\u7528\u662f\u8d1f\u8d23\u5ba2\u6237\u7aef\u7684\u7f51\u7edc\u8fde\u63a5\uff0c\u540c\u65f6\u89e3\u8026\u7f51\u7edc\u4f20\u8f93\u548c\u8bf7\u6c42\u5904\u7406\u7684\u903b\u8f91\u3002_connector \u521b\u5efa\u8fde\u63a5<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,1868,223],"topic":[],"class_list":["post-24202","post","type-post","status-publish","format-standard","hentry","category-server","tag-java","tag-minitomcat","tag-tomcat"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u7b2c\u4e09\u7ae0\uff1aMiniTomcat-\u8fde\u63a5\u5668\uff08Connector\uff09\u7ec4\u4ef6\u5927\u63ed\u79d8\uff1a\u6784\u5efa\u5f3a\u5927\u670d\u52a1\u5668\u7684\u5173\u952e\u4e00\u6b65\uff01 - \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\/24202.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u7b2c\u4e09\u7ae0\uff1aMiniTomcat-\u8fde\u63a5\u5668\uff08Connector\uff09\u7ec4\u4ef6\u5927\u63ed\u79d8\uff1a\u6784\u5efa\u5f3a\u5927\u670d\u52a1\u5668\u7684\u5173\u952e\u4e00\u6b65\uff01 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.5k\u6b21\uff0c\u70b9\u8d5e40\u6b21\uff0c\u6536\u85cf10\u6b21\u3002\u5728\u672c\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u5f15\u5165\u8fde\u63a5\u5668\uff08Connector\uff09\u7ec4\u4ef6\uff0c\u7528\u4e8e\u7ba1\u7406 HTTP \u8fde\u63a5\u548c\u8bf7\u6c42\u6570\u636e\u7684\u89e3\u6790\u3002\u8fde\u63a5\u5668\u7684\u4f5c\u7528\u662f\u8d1f\u8d23\u5ba2\u6237\u7aef\u7684\u7f51\u7edc\u8fde\u63a5\uff0c\u540c\u65f6\u89e3\u8026\u7f51\u7edc\u4f20\u8f93\u548c\u8bf7\u6c42\u5904\u7406\u7684\u903b\u8f91\u3002_connector \u521b\u5efa\u8fde\u63a5\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/24202.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-19T01:42:04+00:00\" \/>\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=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/24202.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/24202.html\",\"name\":\"\u7b2c\u4e09\u7ae0\uff1aMiniTomcat-\u8fde\u63a5\u5668\uff08Connector\uff09\u7ec4\u4ef6\u5927\u63ed\u79d8\uff1a\u6784\u5efa\u5f3a\u5927\u670d\u52a1\u5668\u7684\u5173\u952e\u4e00\u6b65\uff01 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-19T01:42:04+00:00\",\"dateModified\":\"2025-04-19T01:42:04+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/24202.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/24202.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/24202.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u7b2c\u4e09\u7ae0\uff1aMiniTomcat-\u8fde\u63a5\u5668\uff08Connector\uff09\u7ec4\u4ef6\u5927\u63ed\u79d8\uff1a\u6784\u5efa\u5f3a\u5927\u670d\u52a1\u5668\u7684\u5173\u952e\u4e00\u6b65\uff01\"}]},{\"@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":"\u7b2c\u4e09\u7ae0\uff1aMiniTomcat-\u8fde\u63a5\u5668\uff08Connector\uff09\u7ec4\u4ef6\u5927\u63ed\u79d8\uff1a\u6784\u5efa\u5f3a\u5927\u670d\u52a1\u5668\u7684\u5173\u952e\u4e00\u6b65\uff01 - \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\/24202.html","og_locale":"zh_CN","og_type":"article","og_title":"\u7b2c\u4e09\u7ae0\uff1aMiniTomcat-\u8fde\u63a5\u5668\uff08Connector\uff09\u7ec4\u4ef6\u5927\u63ed\u79d8\uff1a\u6784\u5efa\u5f3a\u5927\u670d\u52a1\u5668\u7684\u5173\u952e\u4e00\u6b65\uff01 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.5k\u6b21\uff0c\u70b9\u8d5e40\u6b21\uff0c\u6536\u85cf10\u6b21\u3002\u5728\u672c\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u5f15\u5165\u8fde\u63a5\u5668\uff08Connector\uff09\u7ec4\u4ef6\uff0c\u7528\u4e8e\u7ba1\u7406 HTTP \u8fde\u63a5\u548c\u8bf7\u6c42\u6570\u636e\u7684\u89e3\u6790\u3002\u8fde\u63a5\u5668\u7684\u4f5c\u7528\u662f\u8d1f\u8d23\u5ba2\u6237\u7aef\u7684\u7f51\u7edc\u8fde\u63a5\uff0c\u540c\u65f6\u89e3\u8026\u7f51\u7edc\u4f20\u8f93\u548c\u8bf7\u6c42\u5904\u7406\u7684\u903b\u8f91\u3002_connector \u521b\u5efa\u8fde\u63a5","og_url":"https:\/\/www.wsisp.com\/helps\/24202.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-19T01:42:04+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/24202.html","url":"https:\/\/www.wsisp.com\/helps\/24202.html","name":"\u7b2c\u4e09\u7ae0\uff1aMiniTomcat-\u8fde\u63a5\u5668\uff08Connector\uff09\u7ec4\u4ef6\u5927\u63ed\u79d8\uff1a\u6784\u5efa\u5f3a\u5927\u670d\u52a1\u5668\u7684\u5173\u952e\u4e00\u6b65\uff01 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-19T01:42:04+00:00","dateModified":"2025-04-19T01:42:04+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/24202.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/24202.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/24202.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u7b2c\u4e09\u7ae0\uff1aMiniTomcat-\u8fde\u63a5\u5668\uff08Connector\uff09\u7ec4\u4ef6\u5927\u63ed\u79d8\uff1a\u6784\u5efa\u5f3a\u5927\u670d\u52a1\u5668\u7684\u5173\u952e\u4e00\u6b65\uff01"}]},{"@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\/24202","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=24202"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/24202\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=24202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=24202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=24202"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=24202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}