亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

《How Tomcat Works》讀書筆記一

系統 1808 0

?

1. "《How Tomcat Works_Chapter 1: A Simple Web Server》"
?? 1.1: HTTP hypetext transfer protocal,
?? 1.2: Request: split a uri from address input
?? 1.3: Response: get the uri to read the file from the servers
?? 1.4: HttpServer: new a socket to receive & send bytes info
2. "《How Tomcat Works_Chapter 2: A Simple Servlet Container》"
?? 2.1: When the servlet is called for the first time, load the servlet class and call the
??????? servlet's' init method (once only)
?? 2.2: For each request, construct an instance of javax.servlet.ServletRequest and
??????? an instance of javax.servlet.ServletResponse.
?? 2.3: Invoke the servlet's' service method, passing the ServletRequest and
??????? ServletResponse objects.
?? 2.4: When the servlet class is shut down, call the servlet's' destroy method and
??????? unload the servlet class.

HttpServer.java:

      
         1
      
      
        package
      
      
         chap1_ASimpleWebServer;


      
      
         2
      
      
        import
      
      
         java.io.File;


      
      
         3
      
      
        import
      
      
         java.io.IOException;


      
      
         4
      
      
        import
      
      
         java.io.InputStream;


      
      
         5
      
      
        import
      
      
         java.io.OutputStream;


      
      
         6
      
      
        import
      
      
         java.net.InetAddress;


      
      
         7
      
      
        import
      
      
         java.net.ServerSocket;


      
      
         8
      
      
        import
      
      
         java.net.Socket;


      
      
         9
      
      
        10
      
      
        11
      
      
        public
      
      
        class
      
      
         HttpServer {


      
      
        12
      
      
        public
      
      
        static
      
      
        final
      
       String WEB_ROOT = System.getProperty("user.dir") + File.separator + "webroot"
      
        ;


      
      
        13
      
      
        14
      
      
        private
      
      
        static
      
      
        final
      
       String SHUTDOWN_COMMAND = "/SHUTDOWN"
      
        ;


      
      
        15
      
      
        16
      
      
        private
      
      
        boolean
      
       shutdown = 
      
        false
      
      
        ;


      
      
        17
      
      
        18
      
      
        public
      
      
        void
      
      
         await() {


      
      
        19
      
               ServerSocket serverSocket = 
      
        null
      
      
        ;


      
      
        20
      
      
        int
      
       port = 8080
      
        ;


      
      
        21
      
      
        try
      
      
         {


      
      
        22
      
                   serverSocket = 
      
        new
      
       ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"
      
        ));


      
      
        23
      
               } 
      
        catch
      
      
         (IOException e) {


      
      
        24
      
      
                    e.printStackTrace();


      
      
        25
      
                   System.exit(1
      
        );


      
      
        26
      
      
                }


      
      
        27
      
      
        //
      
      
         loop waiting for a request
      
      
        28
      
      
        while
      
       (!
      
        shutdown) {


      
      
        29
      
                   Socket socket = 
      
        null
      
      
        ;


      
      
        30
      
                   InputStream input = 
      
        null
      
      
        ;


      
      
        31
      
                   OutputStream output = 
      
        null
      
      
        ;


      
      
        32
      
      
        try
      
      
         {


      
      
        33
      
                       socket =
      
         serverSocket.accept();


      
      
        34
      
                       input =
      
         socket.getInputStream();


      
      
        35
      
                       output =
      
         socket.getOutputStream();


      
      
        36
      
      
        //
      
      
         create Request object & parse
      
      
        37
      
                       Request request = 
      
        new
      
      
         Request(input);


      
      
        38
      
      
                        request.parse();


      
      
        39
      
      
        //
      
      
         create Response object
      
      
        40
      
                       Response response = 
      
        new
      
      
         Response(output);


      
      
        41
      
      
                        response.setRequest(request);


      
      
        42
      
      
                        response.sendResource();


      
      
        43
      
      
        //
      
      
         close socket
      
      
        44
      
      
                        socket.close();


      
      
        45
      
      
        //
      
      
         check if the previous URI is a shutdown command
      
      
        46
      
                       shutdown =
      
         request.getUri().equals(SHUTDOWN_COMMAND);


      
      
        47
      
                   } 
      
        catch
      
      
         (IOException e) {


      
      
        48
      
      
                        e.printStackTrace();


      
      
        49
      
      
        continue
      
      
        ;


      
      
        50
      
      
                    }


      
      
        51
      
      
                }


      
      
        52
      
      
            }


      
      
        53
      
      
        54
      
      
        public
      
      
        static
      
      
        void
      
      
         main(String[] args) {


      
      
        55
      
      
                System.out.println(WEB_ROOT);


      
      
        56
      
               HttpServer server = 
      
        new
      
      
         HttpServer();


      
      
        57
      
      
                server.await();


      
      
        58
      
      
            }


      
      
        59
      
       }
    

?

Request.java:

      
        package
      
      
         chap1_ASimpleWebServer;


      
      
        import
      
      
         java.io.IOException;


      
      
        import
      
      
         java.io.InputStream;






      
      
        public
      
      
        class
      
      
         Request {

    
      
      
        private
      
      
         InputStream input;

    
      
      
        private
      
      
         String uri;

    

    
      
      
        public
      
      
         Request (InputStream input) {

        
      
      
        this
      
      .input =
      
         input;

    }

    

    
      
      
        public
      
      
        void
      
      
         parse() {

        StringBuffer request 
      
      = 
      
        new
      
       StringBuffer(2048
      
        );

        
      
      
        int
      
      
         len;

        
      
      
        byte
      
      [] buffer = 
      
        new
      
      
        byte
      
      [2048
      
        ];

        
      
      
        try
      
      
         {

            len 
      
      =
      
         input.read(buffer);

        } 
      
      
        catch
      
      
         (IOException e) {

            e.printStackTrace();

            len 
      
      = -1
      
        ;

        }

        
      
      
        for
      
       (
      
        int
      
       i = 0; i < len; i++
      
        ) {

            request.append((
      
      
        char
      
      )buffer[i]);  
      
        //
      
      
         do not forget cast type byte to char
      
      
                }

        System.out.println(request.toString());

        uri 
      
      =
      
         parseUri(request.toString());

    }

    

    
      
      
        private
      
      
         String parseUri(String requestString) {

        
      
      
        int
      
      
         index1, index2;

        index1 
      
      = requestString.indexOf(' '
      
        );

        
      
      
        if
      
       (index1 != -1
      
        ) {

            index2 
      
      = requestString.indexOf(' ', index1 + 1
      
        );

            
      
      
        if
      
       (index2 >
      
         index1) {

                
      
      
        return
      
       requestString.substring(index1 + 1
      
        , index2);

            }

        }

        
      
      
        return
      
      
        null
      
      
        ;

    }

    

    
      
      
        public
      
      
         String getUri() {

        
      
      
        return
      
      
         uri;

    }

}
      
    

?

?

Response.java:

      
         1
      
      
        package
      
      
         chap1_ASimpleWebServer;


      
      
         2
      
      
        import
      
      
         java.io.File;


      
      
         3
      
      
        import
      
      
         java.io.FileInputStream;


      
      
         4
      
      
        import
      
      
         java.io.IOException;


      
      
         5
      
      
        import
      
      
         java.io.OutputStream;


      
      
         6
      
      
         7
      
      
         8
      
      
        public
      
      
        class
      
      
         Response {


      
      
         9
      
      
        private
      
      
        static
      
      
        final
      
      
        int
      
       BUFFER_SIZE = 1024
      
        ;


      
      
        10
      
      
            Request request;


      
      
        11
      
      
            OutputStream output;


      
      
        12
      
      
        13
      
      
        public
      
      
         Response (OutputStream output) {


      
      
        14
      
      
        this
      
      .output =
      
         output;


      
      
        15
      
      
            }


      
      
        16
      
      
        17
      
      
        public
      
      
        void
      
      
         setRequest(Request request) {


      
      
        18
      
      
        this
      
      .request =
      
         request;


      
      
        19
      
      
            }


      
      
        20
      
      
        21
      
      
        public
      
      
        void
      
       sendResource() 
      
        throws
      
      
         IOException {


      
      
        22
      
      
        byte
      
      [] bytes = 
      
        new
      
      
        byte
      
      
        [BUFFER_SIZE];


      
      
        23
      
               FileInputStream fis = 
      
        null
      
      
        ;


      
      
        24
      
      
        try
      
      
         {


      
      
        25
      
                   File file = 
      
        new
      
      
         File(HttpServer.WEB_ROOT, request.getUri());


      
      
        26
      
      
                    System.out.println(request.getUri().toString());


      
      
        27
      
      
        if
      
      
         (file.exists()) {




      
      
        28
      
                       fis = 
      
        new
      
      
         FileInputStream(file);


      
      
        29
      
      
        int
      
       len = fis.read(bytes, 0
      
        , BUFFER_SIZE);


      
      
        30
      
      
        while
      
       (len != -1
      
        ) {


      
      
        31
      
                           output.write(bytes, 0
      
        , len);


      
      
        32
      
                           len = fis.read(bytes, 0
      
        , BUFFER_SIZE);


      
      
        33
      
      
                        }


      
      
        34
      
                   } 
      
        else
      
      
         {


      
      
        35
      
                       String errorMessage = "HTTP/1.1 404 File Not Found\r\n" +


      
        36
      
                                             "ContentType: text/html\r\n" +


      
        37
      
                                             "ContentLength: 23\r\n\r\n" +


      
        38
      
                                             "<head>File Not Found</head>"
      
        ;


      
      
        39
      
      
                        output.write(errorMessage.getBytes());


      
      
        40
      
      
                    }


      
      
        41
      
               } 
      
        catch
      
      
         (Exception e) {


      
      
        42
      
      
                    e.printStackTrace();


      
      
        43
      
               } 
      
        finally
      
      
         {


      
      
        44
      
      
        if
      
       (fis != 
      
        null
      
      
        ) {


      
      
        45
      
      
                        fis.close();


      
      
        46
      
      
                    }


      
      
        47
      
      
                }


      
      
        48
      
      
            }


      
      
        49
      
       }
    

簡單的html, 放在webroot文件夾下:

      
        <
      
      
        html
      
      
        >
      
      
        <
      
      
        head
      
      
        >
      
      
        <
      
      
        title
      
      
        >
      
      Hello Tomcat!
      
        </
      
      
        title
      
      
        >
      
      
        </
      
      
        head
      
      
        >
      
      
        <
      
      
        body
      
      
        >
      
      
        <
      
      
        img 
      
      
        src
      
      
        ="./images/tomcat.jpg"
      
      
        >
      
      
        <
      
      
        br
      
      
        >
      
      
        <
      
      
        h1
      
      
        >
      
      It's Works!
      
        </
      
      
        h1
      
      
        >
      
      
        .

    
      
      
        </
      
      
        body
      
      
        >
      
      
        </
      
      
        html
      
      
        >
      
    

直接java application 運行, 在頁面上即可,結果如下: 《How Tomcat Works》讀書筆記一

光是如此是遠遠不夠的, 所以我們還要增加對java文件的支持(Servlet文件)。。。

?

?

?

《How Tomcat Works》讀書筆記一


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日日干日日射 | 999精品在线| 久久久四虎成人永久免费网站 | 欧美成人性视频在线黑白配 | 国产综合色在线视频区 | 日日操夜夜操狠狠操 | 日韩中文字幕在线观看视频 | 国产一区二区免费播放 | 国产精品一区二区三 | 免费无毒片在线观看 | 欧美日韩加勒比一区二区三区 | 精品亚洲一区二区三区在线播放 | 美女视频免费在线观看 | 免费播放欧美毛片 | 欧美成人免费视频a | 亚洲综合激情六月婷婷在线观看 | 久久久久久久久性潮 | 精品视频在线免费观看 | 四虎网址换成什么了2021 | 久久艹免费视频 | 亚洲最大免费视频网 | 黑人巨大精品战中国美女 | 国产精品5 | 国产黄毛片 | 99视频网址 | 日日碰夜夜操 | 天天干夜夜爽天天操夜夜爽视频 | 国产一区二区久久精品 | 欧美另类成人 | 青青草国产免费一区二区 | 天天操伊人 | 国产亚洲精品久久久久91网站 | 亚洲日本久久一区二区va | 手机观看毛片 | 欧美一级成人影院免费的 | 精品国产日韩亚洲一区91 | 91国内在线视频 | 日本高清免费不卡在线播放 | 日日夜夜网站 | 4hu永久影院在线四虎 | 国产精品videossex激情 |