欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

《How Tomcat Works》讀書筆記一

系統(tǒng) 1919 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 運(yùn)行, 在頁面上即可,結(jié)果如下: 《How Tomcat Works》讀書筆記一

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

?

?

?

《How Tomcat Works》讀書筆記一


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 国产原创视频在线 | 亚洲图片欧洲电影 | 性做爰片免费视频毛片中文ILO | 国产精选91热在线观看 | a高清免费毛片久久 | 国产97人妻人人做人碰人人爽 | 且试天下修久容 | 99re国产| 性高湖久久久久久久久aaaaa | 色爱区综合 | a网站在线观看 | xxnxx中国18 | 久久久久久久久久久9精品视频 | 免费福利视频在线观看 | 日韩精品在线一区 | 欧美三级视频 | 小宝与康熙粤语 | 日韩av资源站 | 久色视频在线观看 | 日本一区二区三区久久久 | 超碰97青青草 | www.日韩| 国产99久 | www.久久久.com | 日韩一区二区在线观看 | 蜜桃官网 | 国产这里只有精品 | 亚洲第一视频 | 亚洲欧美电影 | 国产在线不卡 | 99久久一区二区 | 国产成人99| 蜜桃日韩| 91精品欧美久久久久久动漫 | 大象一区 | 自拍偷拍第一页 | 激情视频免费 | 青草国产超碰人人添人人碱 | 九二淫黄大片看片 | 成人毛片久久 | 欧美一区二区三区久久精品 |