zoukankan      html  css  js  c++  java
  • Tomcat系列(6)——Tomcat处理一个HTTP请求的过程

    Tomcat的架构图

     
                  图三:Tomcat Server处理一个HTTP请求的过程

    处理HTTP请求过程

    假设来自客户的请求为:http://localhost:8080/test/index.jsp 请求被发送到本机端口8080

    1、用户点击网页内容,请求被发送到本机端口8080,被在那里监听的Coyote HTTP/1.1 Connector获得。 
    2、Connector把该请求交给它所在的Service的Engine来处理,并等待Engine的回应。 
    3、Engine获得请求localhost/test/index.jsp,匹配所有的虚拟主机Host。 
    4、Engine匹配到名为localhost的Host(即使匹配不到也把请求交给该Host处理,因为该Host被定义为该Engine的默认主机),名为localhost的Host获得请求/test/index.jsp,匹配它所拥有的所有的Context。Host匹配到路径为/test的Context(如果匹配不到就把该请求交给路径名为“ ”的Context去处理)。 
    5、path=“/test”的Context获得请求/index.jsp,在它的mapping table中寻找出对应的Servlet。Context匹配到URL PATTERN为*.jsp的Servlet,对应于JspServlet类。 
    6、构造HttpServletRequest对象和HttpServletResponse对象,作为参数调用JspServlet的doGet()或doPost().执行业务逻辑、数据存储等程序。 
    7、Context把执行完之后的HttpServletResponse对象返回给Host。 
    8、Host把HttpServletResponse对象返回给Engine。 
    9、Engine把HttpServletResponse对象返回Connector。 
    10、Connector把HttpServletResponse对象返回给客户Browser。

    简单模拟Tomcat

      tomcat是通过socket和浏览器获得连接,因为可能有多个请求,所以要用到多线程去接收,通过io流来传递数据。   

    package Server;
    import java.io.*;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.URLDecoder;
    import java.util.StringTokenizer;
    public class TomcatServer {
        private final static int PORT = 8080;
        public static void main(String[] args) {
            try {
                ServerSocket server = new ServerSocket(PORT);// 根据端口号启动一个serverSocket
                ServletHandler servletHandler = new ServletHandler(server);
                servletHandler.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private static class ServletHandler extends Thread {
            ServerSocket server = null;
            public ServletHandler(ServerSocket server) {
                this.server = server;
            }
            @Override
            public void run() {
                while (true) {
                    try {
                        Socket client = null;
                        client = server.accept();// ServerSocket阻塞等待客户端请求数据
                        if (client != null) {
                            try {
                                System.out.println("接收到一个客户端的请求");
                                // 根据客户端的Socket对象获取输入流对象。
                                // 封装字节流到字符流
                                BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
                                // GET /test.jpg /HTTP1.1
                                // http请求由三部分组成,分别是:请求行、消息报头、请求正文。
                                // 这里取的第一行数据就是请求行。http协议详解可以参考http://www.cnblogs.com/li0803/archive/2008/11/03/1324746.html说的很详细
                                String line = reader.readLine();
                                System.out.println("line: " + line);
                                // 拆分http请求路径,取http需要请求的资源完整路径
                                String resource = line.substring(line.indexOf('/'), line.lastIndexOf('/') - 5);
                                System.out.println("the resource you request is: " + resource);
                                resource = URLDecoder.decode(resource, "UTF-8");
                                // 获取到这次请求的方法类型,比如get或post请求
                                String method = new StringTokenizer(line).nextElement().toString();
                                System.out.println("the request method you send is: " + method);
                                // 继续循环读取浏览器客户端发出的一行一行的数据
                                while ((line = reader.readLine()) != null) {
                                    if (line.equals("")) {// 当line等于空行的时候标志Header消息结束
                                        break;
                                    }
                                    System.out.println("the Http Header is : " + line);
                                }
                                // 如果是POST的请求,直接打印POST提交上来的数据
                                if ("post".equals(method.toLowerCase())) {
                                    System.out.println("the post request body is: " + reader.readLine());
                                } else if ("get".equals(method.toLowerCase())) {
                                    // 判断是get类型的http请求处理
                                    // 根据http请求的资源后缀名来确定返回数据
                                    // 比如下载一个图片文件,我这里直接给定一个图片路径来模拟下载的情况
                                    if (resource.endsWith(".jpg")) {
                                        transferFileHandle("d://1.jpg", client);
                                        closeSocket(client);
                                        continue;
                                    } else {
                                        // 直接返回一个网页数据
                                        // 其实就是将html的代码以字节流的形式写到IO中反馈给客户端浏览器。
                                        // 浏览器会根据http报文“Content-Type”来知道反馈给浏览器的数据是什么格式的,并进行什么样的处理
                                        PrintStream writer = new PrintStream(client.getOutputStream(), true);
                                        writer.println("HTTP/1.0 200 OK");// 返回应答消息,并结束应答
                                        writer.println("Content-Type:text/html;charset=utf-8");
                                        writer.println();
                                        // writer.println("Content-Length:" +
                                        // html.getBytes().length);// 返回内容字节数
                                        writer.println("<html><body>");
                                        writer.println("<a href='www.baidu.com'>百度</a>");
                                        writer.println(
                                        "<img src='https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png'></img>");
                                        writer.println("</html></body>");
                                        // writer.println("HTTP/1.0 404 Not
                                        // found");// 返回应答消息,并结束应答
                                        writer.println();// 根据 HTTP 协议, 空行将结束头信息
                                        writer.close();
                                        closeSocket(client);// 请求资源处理完毕,关闭socket链接
                                        continue;
                                    }
                                }
                            } catch (Exception e) {
                                System.out.println("HTTP服务器错误:" + e.getLocalizedMessage());
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            private void closeSocket(Socket socket) {
                try {
                    socket.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                System.out.println(socket + "离开了HTTP服务器");
            }
            private void transferFileHandle(String path, Socket client) {
                File fileToSend = new File(path);
                if (fileToSend.exists() && !fileToSend.isDirectory()) {
                    try {
                        // 根据Socket获取输出流对象,将访问的资源数据写入到输出流中
                        PrintStream writer = new PrintStream(client.getOutputStream());
                        writer.println("HTTP/1.0 200 OK");// 返回应答消息,并结束应答
                        writer.println("Content-Type:application/binary");
                        writer.println("Content-Length:" + fileToSend.length());// 返回内容字节数
                        writer.println();// 根据 HTTP 协议, 空行将结束头信息
                        FileInputStream fis = new FileInputStream(fileToSend);
                        byte[] buf = new byte[fis.available()];
                        fis.read(buf);
                        writer.write(buf);
                        writer.close();
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    抄录网址

    1. 解析Tomcat内部结构和请求过程
    2. Tomcat运行过程和简单模拟
  • 相关阅读:
    【微信小程序】后台访问-net::ERR_CONNECTION_REFUSED 及 不是request合法域名问题 的解决
    【微信小程序】引用FontAwesome字体
    【微信小程序】开发工具使用技巧(七)
    【微信小程序】小程序扩展能力(六)
    【微信小程序】小程序云开发(五)
    【微信小程序】小程序框架(四)
    javascript学习笔记
    HTML&CSS学习笔记
    mysql学习笔记
    c++ primer 笔记 (四)
  • 原文地址:https://www.cnblogs.com/haimishasha/p/10744696.html
Copyright © 2011-2022 走看看