深入剖析Tomcat-自定义Tomcat(1)

396 阅读3分钟

    作为一个Java后端开发,Tomcat是我们经常会用到的技术,但大多数时候仅仅停留在使用上,并不了解其真正的原理。其作为应用最广泛的Servlet容器,优秀的架构和思路非常值得我们去学习。了解其原理对我们的技术也有很大的提升。所以,现在开始跟着《深入剖析Tomcat》这本书来一步一步对Tomcat做一个深入的了解

一、概念介绍

1、Servlet容器的作用

    Servlet容器主要接收客户端请求,为每一个请求创建一个请求对象request和一个响应对象response,并将request和response作为参数调用Servlet的service方法,Servlet从请求中读取信息,并通过response发送响应信息。

2、HTTP

    HTTP协议是一种基于TCP/IP通信协议来传递数据的,是一种基于“请求-响应”的协议。客户端发出请求,服务器响应请求。请求消息的格式包括:请求行、请求头、空行和请求数据。如:

GET /hello HTTP/1.1
Host: localhost:8080
Connection: keep-alive

    响应格式如:

HTTP/1.1 200 OK
content-encoding: gzip
content-length: 28
content-type: text/html; charset=UTF-8
date: Wed, 23 Mar 2022 14:11:40 GMT

3、Socket

    套接字是网络连接的端点。套接字使应用程序可以从网络中读取数据、向网络中写入数据。不同计算机上的两个应用程序可以通过连接发送或接收字节流,以此达到通信的目的。Java中使用java.net.Socket类来实现套接字。

二、实现一个Web服务器

    一个最简单的Web服务器包含以下几个类:HttpServer(web服务器)、Request(请求对象)、Response(响应对象)。

public class HttpServer {
    /**
     * 存放静态资源的目录
     */
    public static final String WEB_ROOT = System.getProperty("user.dir") + File.separator + "webroot";
    /**
     * 关闭命令。若要关闭服务器,可以在请求路径后面拼接SHUTDOWN命令,比如:http://localhost:8080/SHUTDOWN
     */
    private static final String SHUTDOWN_COMMAND = "/SHUTDOWN";
    /**
     * 是否收到关闭的命令
     */
    private boolean shutdown = false;

    public static void main(String[] args) {
        HttpServer server = new HttpServer();
        server.await();
    }

    public void await() {
        ServerSocket serverSocket = null;
        int port = 8080;
        try {
            // 开启一个ServerSocket
            serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
        // 循环等待连接
        while (!shutdown) {
            Socket socket;
            InputStream input;
            OutputStream output;
            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();

                socket.close();
                // 接收到关闭命令
                shutdown = request.getUri().equals(SHUTDOWN_COMMAND);
            } catch (IOException e) {
                e.printStackTrace();
                continue;
            }
        }
    }
}
public class Request {

    private String uri;
    private InputStream input;

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

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

    /**
     * 用于解析http请求中的原始数据
     */
    public void parse() {
        StringBuffer request = new StringBuffer(2048);
        int i;
        byte[] buffer = new byte[2048];
        try {
            i = input.read(buffer);
        } catch (Exception e) {
            e.printStackTrace();
            i = -1;
        }
        for (int j = 0; j < i; j++) {
            request.append((char) buffer[j]);
        }
        System.out.println(request);
        uri = parseUri(request.toString());
    }

    /**
     * 从http请求行中获取uri
     *
     * @param requestString
     * @return
     */
    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 class Response {

    private static final int BUFFER_SIZE = 1024;

    Request request;

    OutputStream output;

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

    public void setRequest(Request request) {
        this.request = request;
    }

    /**
     * 返回一个静态资源到浏览器
     */
    public void sendStaticResource() {
        byte[] bytes = new byte[BUFFER_SIZE];
        FileInputStream fis = null;
        try {
            File file = new File(HttpServer.WEB_ROOT, request.getUri());
            // 在项目的根路径下读取是否存在静态资源,如果存在则返回静态资源,不存在则返回404
            if (file.exists()) {
                String header = "HTTP/1.1 200 OK\n" +
                        "Content-Type: text/html\n" +
                        "\r\n";
                output.write(header.getBytes());
                fis = new FileInputStream(file);
                int ch = fis.read(bytes, 0, BUFFER_SIZE);
                while (ch != -1) {
                    output.write(bytes, 0, ch);
                    ch = fis.read(bytes, 0, BUFFER_SIZE);
                }
            } else {
                String errorMessage = "HTTP/1.1 404 File Not Found\n" +
                        "Content-Type: text/html\n" +
                        "\r\n" +
                        "<html><body><h1>File Not Found</h1></body></html>";

                output.write(errorMessage.getBytes());
                output.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

    在项目根目录下创建webroot文件夹,并新建index.html。启动HttpServer,然后在浏览器中输入:http://localhost:8080/index.html

    输入其他没有资源的路径则返回:

    以上代码托管在:gitee.com/bailiny/my-…

下一篇 深入剖析Tomcat-自定义Tomcat(2)