How tomcat works學習筆記(1)

1.  servlet容器是如何工作的?

  • 創建一個Request對象,填充一些信息比如參數、headers、cookies、查詢字符串、URI等。一個Request對象是javax.servlet.ServletRequest或javax.servlet.http.ServletRequest接口的實例。
  • 創建一個Response對象,用於調用的servlet向客戶端傳遞響應信息。其是javax.servlet.ServletResponse或javax.servlet.http.ServletResponse的實例。
  • 調用servlet的service方法,傳遞request和response對象。servlet從request對象中讀取值,向response對象中寫入值。

2.Catalina主要模塊

 

  • Connector,連接器主要是連接請求到容器。它的工作是爲每一個接收到http請求構建一個request和response對象,接下來傳遞給待處理的容器。
  • Container,容器從連接器接收到request和response對象,負責調用servlet的service方法。

連接器和容器是多對1的關係(*對1)

 

3.tomcat4和5對比

 

  • tomcat5支持servlet2.4和jsp2.0規範,tomcat4支持servlet2.3和jsp1.2
  • tomcat5擁有比tomcat4更高效的默認連接器
  • tomcat5使用更少的資源。因爲tomcat5共享一個後臺處理的線程,而tomcat4的每個模塊都有自己的後臺處理線程。
  • tomcat5代碼更簡潔。因爲不需要一個mapper組件來找到一個子組件。
一個簡單的HttpServer代碼示例:
流程圖:


 
package com.flyer.tomcat.first;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 一個簡單的http服務器
 * <p />
 * 
 * @author Administrator
 */
public class HttpServer {

    private final static String SHUTDOWN_COMMAND = "/shutdown";
    
    public final static String WEB_ROOT = System.getProperty("user.dir") + File.separator + "webroot";

    private boolean shutdown = false;

    public static void main(String[] args) {
        System.out.println("server start");
        System.out.println(WEB_ROOT);
        HttpServer server = new HttpServer();
        try {
            server.await();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    private void await() throws IOException {

        ServerSocket serverSocket = new ServerSocket(9090,1,InetAddress.getByName("127.0.0.1"));
        InputStream input = null;
        OutputStream output = null;
        while (!shutdown) {
            Socket socket = null;
            try {
                socket = serverSocket.accept();
                input = socket.getInputStream();
                output = socket.getOutputStream();
                Request request = new Request(input);
                request.parse();
                Response response = new Response(output);
                response.setRequest(request);
                response.sendStaticResource();
                shutdown = request.getUri().equals(SHUTDOWN_COMMAND);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                socket.close();
                input.close();
                output.close();
            }
        }

    }

}
 Request類
package com.flyer.tomcat.first;

import java.io.IOException;
import java.io.InputStream;

public class Request {

    private byte[] buffer = new byte[1024];

    private InputStream input;

    private String uri;

    public Request(InputStream input) {
        this.setInput(input);
    }

    /**
     * @return
     */
    public InputStream getInput() {
        return input;
    }

    /**
     * @param
     */
    public void setInput(InputStream input) {
        this.input = input;
    }

    public void parse() {
        int count = 0;
        StringBuilder sb = new StringBuilder();
        try {
            count = input.read(buffer);
        } catch (IOException e) {
            count = -1;
        }
        if (count != -1) {
            for (int i = 0; i < count; i++) {
                sb.append((char) buffer[i]);
            }
        }

        uri = parseUri(sb.toString());
    }

    private String parseUri(String requestString) {
        int index1, index2;
        String uri = requestString;
        index1 = uri.indexOf(" ");
        if (index1 != -1) {
            index2 = uri.indexOf(" ", index1 + 1);
            if (index1 < index2) {
                uri = uri.substring(index1 + 1, index2);
                return uri;
            }
        }
        return "";

    }

    /**
     * @return
     */
    public String getUri() {
        return uri;
    }

    // public static void main(String[] args) {
    // Request request = new Request(new InputStream() {
    //
    // @Override
    // public int read() throws IOException {
    // // TODO Auto-generated method stub
    // return 0;
    // }
    //
    // });
    // String test = "GET /index.jsp HTTP/1.1";
    // System.out.println(request.parseUri(test));
    // }

}
 Response類
package com.flyer.tomcat.first;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Response {

    public final static int BUFFER_SIZE = 1024;

    private Request request;

    private OutputStream output;

    public Response(OutputStream output) {
        this.setOutput(output);
    }

    /**
     * @return
     */
    public Request getRequest() {
        return request;
    }

    /**
     * @param
     */
    public void setRequest(Request request) {
        this.request = request;
    }

    public void sendStaticResource() {
        byte[] buffer = new byte[BUFFER_SIZE];
        InputStream inputStream = null;
        try {
            File file = new File(HttpServer.WEB_ROOT + request.getUri());
            if (file.exists()) {
                inputStream = new FileInputStream(file);
                int count = 0;
                while ((count = inputStream.read(buffer)) != -1) {
                    output.write(buffer, 0, count);
                }
            } else {
                String errorMessage = "HTTP/1.1 404 File not found\r\n" + "Content-Type : text/html\r\n"
                    + "Content-length:23\r\n" + "\r\n" + "<h1>File not Found</h1>";
                output.write(errorMessage.getBytes());
            }
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    /**
     * @return
     */
    public OutputStream getOutput() {
        return output;
    }

    /**
     * @param
     */
    public void setOutput(OutputStream output) {
        this.output = output;
    }

}
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章