tomcat原理解析(一):一个简单的实现

一 概述

       前段时间去面试,被人问到了tomcat实现原理。由于平时没怎么关注容器的实现细节,这个问题基本没回答上来。所以最近花了很多时间一直在网上找资料和看tomcat的源码来研究里面处理一个HTTP请求的流程。网上讲tomcat的帖子比较多,大多都是直接切入主题看其源码,从我个人感受来说直接研究其源码实现比较难理解和非常枯燥,需要由简到难,慢慢深入。

二  一个简单tomcat服务器实现

        tomat是一个servlet容器,来处理http请求。在平时的使用中我们都会再浏览器中输入http地址来访问服务资源,比如格式http://host[":"port][abs_path]。从浏览器到服务端的一次请求都遵循http协议,在网络上其实走仍然是tcp协议,即我们常使用的socket来处理客户端和服务器的交互。根据输入的http地址可以知道服务器的IP地址和端口,根据这两个参数就可以定位到服务器的唯一地址。tomcat根据http地址端口后面的资源路径就可以知道反馈什么样的资源给浏览器。下面给出了一个非常简单的代码模拟了tomcat的简单实现

 

[html] view plaincopy
  1. package com;  
  2.   
  3. import java.io.*;  
  4. import java.net.ServerSocket;  
  5. import java.net.Socket;  
  6. import java.net.URLDecoder;  
  7. import java.util.StringTokenizer;  
  8.   
  9. public class TomcatServer {  
  10.   
  11.     private final static int PORT = 8080;  
  12.   
  13.     public static void main(String[] args) {  
  14.   
  15.         try {  
  16.             ServerSocket server = new ServerSocket(PORT);//根据端口号启动一个serverSocket  
  17.             ServletHandler servletHandler=new ServletHandler(server);  
  18.             servletHandler.start();  
  19.         } catch (Exception e) {  
  20.             e.printStackTrace();  
  21.         }  
  22.   
  23.     }  
  24.   
  25.   
  26.   
  27.     private static class ServletHandler extends Thread{  
  28.         ServerSocket server=null;  
  29.         public ServletHandler(ServerSocket server){  
  30.             this.server=server;  
  31.         }  
  32.   
  33.   
  34.         @Override  
  35.         public void run() {  
  36.             while (true) {  
  37.                 try {  
  38.                     Socket client = null;  
  39.                     client = server.accept();//ServerSocket阻塞等待客户端请求数据  
  40.                     if (client != null) {  
  41.                         try {  
  42.                             System.out.println("接收到一个客户端的请求");  
  43.   
  44.                             //根据客户端的Socket对象获取输入流对象。  
  45.                             //封装字节流到字符流  
  46.                             BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));  
  47.   
  48.                             // GET /test.jpg /HTTP1.1  
  49.                             //http请求由三部分组成,分别是:请求行、消息报头、请求正文。  
  50.                             //这里取的第一行数据就是请求行。http协议详解可以参考http://www.cnblogs.com/li0803/archive/2008/11/03/1324746.html说的很详细  
  51.                             String line = reader.readLine();  
  52.   
  53.                             System.out.println("line: " + line);  
  54.   
  55.                             //拆分http请求路径,取http需要请求的资源完整路径  
  56.                             String resource = line.substring(line.indexOf('/'),line.lastIndexOf('/') - 5);  
  57.   
  58.                             System.out.println("the resource you request is: "+ resource);  
  59.   
  60.                             resource = URLDecoder.decode(resource, "UTF-8");  
  61.   
  62.                             //获取到这次请求的方法类型,比如get或post请求  
  63.                             String method = new StringTokenizer(line).nextElement().toString();  
  64.   
  65.                             System.out.println("the request method you send is: "+ method);  
  66.   
  67.                             //继续循环读取浏览器客户端发出的一行一行的数据  
  68.                             while ((line = reader.readLine()) != null) {  
  69.                                 if (line.equals("")) {//当line等于空行的时候标志Header消息结束  
  70.                                     break;  
  71.                                 }  
  72.                                 System.out.println("the Http Header is : " + line);  
  73.                             }  
  74.   
  75.                             //如果是POST的请求,直接打印POST提交上来的数据  
  76.                             if ("post".equals(method.toLowerCase())) {  
  77.                                 System.out.println("the post request body is: "  
  78.                                         + reader.readLine());  
  79.                             }else if("get".equals(method.toLowerCase())){  
  80.                                 //判断是get类型的http请求处理  
  81.                                 //根据http请求的资源后缀名来确定返回数据  
  82.   
  83.                                 //比如下载一个图片文件,我这里直接给定一个图片路径来模拟下载的情况  
  84.                                 if (resource.endsWith(".jpg")) {  
  85.                                     transferFileHandle("d://123.jpg", client);  
  86.                                     closeSocket(client);  
  87.                                     continue;  
  88.   
  89.                                 } else {  
  90.   
  91.                              //直接返回一个网页数据  
  92.                              //其实就是将html的代码以字节流的形式写到IO中反馈给客户端浏览器。  
  93.                              //浏览器会根据http报文“Content-Type”来知道反馈给浏览器的数据是什么格式的,并进行什么样的处理  
  94.   
  95.                              PrintStream writer = new PrintStream(client.getOutputStream(), true);  
  96.                              writer.println("HTTP/1.0 200 OK");// 返回应答消息,并结束应答  
  97.                              writer.println("Content-Type:text/html;charset=utf-8");  
  98.                              writer.println();  
  99.                              //writer.println("Content-Length:" + html.getBytes().length);// 返回内容字节数  
  100.                              writer.println("<html><body>");  
  101.                              writer.println("<href='www.baidu.com'>百度</a>");  
  102.                              writer.println("<img src='https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png'></img>");  
  103.                              writer.println("</html></body>");  
  104.   
  105.   
  106.                              //writer.println("HTTP/1.0 404 Not found");// 返回应答消息,并结束应答  
  107.                              writer.println();// 根据 HTTP 协议, 空行将结束头信息  
  108.                              writer.close();  
  109.                              closeSocket(client);//请求资源处理完毕,关闭socket链接  
  110.                              continue;  
  111.                                 }  
  112.                             }  
  113.   
  114.   
  115.   
  116.                         } catch (Exception e) {  
  117.                             System.out.println("HTTP服务器错误:"  
  118.                                     + e.getLocalizedMessage());  
  119.                         }  
  120.                     }  
  121.                 } catch (Exception e) {  
  122.                     e.printStackTrace();  
  123.                 }  
  124.             }  
  125.         }  
  126.   
  127.         private void closeSocket(Socket socket) {  
  128.             try {  
  129.                 socket.close();  
  130.             } catch (IOException ex) {  
  131.                 ex.printStackTrace();  
  132.             }  
  133.             System.out.println(socket + "离开了HTTP服务器");  
  134.         }  
  135.   
  136.         private void transferFileHandle(String path, Socket client) {  
  137.   
  138.             File fileToSend = new File(path);  
  139.   
  140.             if (fileToSend.exists() && !fileToSend.isDirectory()) {  
  141.                 try {  
  142.                     //根据Socket获取输出流对象,将访问的资源数据写入到输出流中  
  143.                     PrintStream writer = new PrintStream(client.getOutputStream());  
  144.                     writer.println("HTTP/1.0 200 OK");// 返回应答消息,并结束应答  
  145.                     writer.println("Content-Type:application/binary");  
  146.                     writer.println("Content-Length:" + fileToSend.length());// 返回内容字节数  
  147.                     writer.println();// 根据 HTTP 协议, 空行将结束头信息  
  148.   
  149.                     FileInputStream fis = new FileInputStream(fileToSend);  
  150.                     byte[] buf = new byte[fis.available()];  
  151.                     fis.read(buf);  
  152.                     writer.write(buf);  
  153.                     writer.close();  
  154.                     fis.close();  
  155.                 } catch (IOException e) {  
  156.                     e.printStackTrace();  
  157.                 }  
  158.             }  
  159.         }  
  160.   
  161.     }  
  162.   
  163. }  

 

三  实践

    1.在浏览器中输入http://localhost:8080/123.jpg 链接,可以看到浏览器里面就将123.jpg下载到本地了。

    2.在浏览器中输入一个服务器不能识别的请求后缀比如http://localhost:8080/123.jpg1,可以看到浏览器打开了一个网页。如下图:点击里面的百度链接可以跳转

    3.后台tomcat服务器打印的http请求报文

 

      接收到一个客户端的请求
line: GET /123.jpg1 HTTP/1.1
the resource you request is: /123.jpg1
the request method you send is: GET
the Http Header is : Host: localhost:8080
the Http Header is : Connection: keep-alive
the Http Header is : Pragma: no-cache
the Http Header is : Cache-Control: no-cache
the Http Header is : Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
the Http Header is : Upgrade-Insecure-Requests: 1
the Http Header is : User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
the Http Header is : Accept-Encoding: gzip, deflate, sdch
the Http Header is : Accept-Language: zh-CN,zh;q=0.8
Socket[addr=/0:0:0:0:0:0:0:1,port=57864,localport=8080]离开了HTTP服务器

四  总结

从整个代码和测试情况来看,一次http请求其实就是一次socket套接字的处理。浏览器发起scoket的请求,tomcat服务器接受请求,并根据请求的路径定位客户端需要访问的资源。  只是socket客户端和服务器数据在交互时,都遵守着http协议规范。当然真正的tomcat容器比这个demo实现要复杂的很多,这个简易的tomcat服务器能够帮我们更好的理解tomcat源码。

转载于:https://www.cnblogs.com/csguo/p/7499395.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/428771.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

1065 A+B and C (64bit) (20 分)

1065 AB and C (64bit) (20 分) Given three integers A, B and C in [−2 ​63 ​​ ,2 ​63 ​​ ], you are supposed to tell whether AB>C. Input Specification: The first line of the input gives the positive number of test cases, T (≤10). Then T test cases…

7-3 寻找大富翁 (25 分)

7-3 寻找大富翁 (25 分) 胡润研究院的调查显示&#xff0c;截至2017年底&#xff0c;中国个人资产超过1亿元的高净值人群达15万人。假设给出N个人的个人资产值&#xff0c;请快速找出资产排前M位的大富翁。输入格式: 输入首先给出两个正整数N&#xff08;≤10 ​6 ​​ &#…

7-8 德才论 (25 分)(C语言实现)

7-8 德才论 (25 分) 宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”&#xff1a;“是故才德全尽谓之圣人&#xff0c;才德兼亡谓之愚人&#xff0c;德胜才谓之君子&#xff0c;才胜德谓之小人。凡取人之术&#xff0c;苟不得圣人&#xff0c;君子而与之&#xff0c;…

899. 编辑距离

编辑距离 #include <iostream> #include <cstring> #include <algorithm> using namespace std; int f[1001][1001]; int main() {char ch[1001][13];int n, m;cin >> n >> m;for (int i 0; i < n; i){scanf("%s",ch[i]1);}while…

282. 石子合并

石子合并 #include<algorithm> #include<iostream> using namespace std; const int N 303; int f[N][N]; int s[N]; int main() {int n;cin>>n;for (int i1;i<n;i) cin>>s[i],s[i]s[i-1];for (int len 2;len<n;len)//len长度{for (int i1;il…

868. 筛质数

筛质数 埃及筛法&#xff1a; #include<iostream> #include<cstring> #include<algorithm> using namespace std; int main() {int n,cnt0;bool p[1000010];memset(p,true,sizeof(p));cin>>n;for (int i1;i<n;i){if (p[i]true) cnt;for (int j ii…

870. 约数个数

约数个数 约数个数就是所有数的质因子的个数加一相乘 #include <iostream> #include <set> #include <map> #include <cmath> using namespace std; const int mod 1e9 7; int main() {map<int, int> mp;int n, m;cin >> n;while (n--)…

900. 整数划分

整数划分 两种方案&#xff1a; 第一种&#xff1a;f[i][j] f[i-1][j-1]f[i-j][j]//i表示数量&#xff0c;j表示分几个数 #include <iostream> using namespace std; const int N 1050; const int mod 1e9 7; int f[N][N]; int main() {int n;cin >> n;f[0][0…

3617. 子矩形计数

子矩形计数 #include <iostream> #include <cmath> using namespace std; int a[40041], b[40041]; //int num1[40041],num2[40041]; pair<int, int> f[40041]; int main() {int n, m, k;int num;cin >> n >> m >> k;int cou1 0, cou2 …

常用的一些表和字段

1.【VBAK:销售凭证】 字段&#xff1a;vbeln(销售凭证)、erdat(创建日期)、ername(创建者)、auart(销售类型)、bstnk(采购订单)、bsark(采购订单类型)、bstdk(采购日期)2.【VBAP:销售凭证行项目】 字段&#xff1a;vbeln(销售凭证)、posnr(项目)、matnr(物料)、werks(工厂)、lg…

手风琴案例jquery写法

今天我用jquary来写一下手风琴案例&#xff0c;这个案例在平时的项目中很经常会见到&#xff0c;要想实现效果用jquary来写其实很简单&#xff0c;其实一句话就是jquary的方法的调用。 首先我们先来分析一下手风琴案例实际实现的效果&#xff0c;就是点击当前的标题&#xff0c…

801. 二进制中1的个数

二进制中1的个数 #include<iostream> using namespace std; int h; int main() {int n,num;cin>>n;for (int i0;i<n;i){cin>>num;int cou 0;for (int i0;i<31;i){h num>>i&1;if (h1) cou;}cout<<cou<<" ";} }

D. Omkar and Medians

D. Omkar and Medians Uh oh! Ray lost his array yet again! However, Omkar might be able to help because he thinks he has found the OmkArray of Rays array. The OmkArray of an array a with elements a1,a2,…,a2k−1, is the array b with elements b1,b2,…,bk su…

元组tuple

另一种有序列表叫元组&#xff1a;tuple。tuple和list非常类似&#xff0c;但是tuple一旦初始化就不能修改&#xff0c;比如同样是列出同学的名字&#xff1a; >>> classmates (Michael, Bob, Tracy)现在&#xff0c;classmates这个tuple不能变了&#xff0c;它也没有…

3493. 最大的和

最大的和 滑动窗口 #include <iostream> #define int long long using namespace std; signed main() {int n, k, sum 0, a[101001], b[100101], c[100101];cin >> n >> k;for (int i 1; i < n; i){cin >> a[i];}for (int i 1; i < n; i){ci…

delphi ---ttoolbar,ttoolbutton

1、button style&#xff1a;tbsButton&#xff0c;tbsCheck&#xff0c;tbsDivider&#xff0c;tbsDropDown&#xff0c;tbsSeparator&#xff0c;tbsTextButton tbsButton&#xff1a;普通的控件 tbsSeparator&#xff1a;用作分隔 tbsDropDown&#xff1a;下拉控件&#xff…

3481. 阶乘的和

阶乘的和 #include <iostream> #include <unordered_set> using namespace std; unordered_set<int> S; int main() {int f[11];f[0] 1;for (int i 1; i < 10; i){f[i] f[i - 1] * i;}for (int i 1; i < 1 << 10; i){int s 0;for (int j …

win7下的nginx小demo

一直大概知道nginx怎么玩,但是不看文档又蒙蔽.在这记录一下,以后好查看 下载tomcat,改index.jsp http://tomcat.apache.org/download-80.cgi tomcat9已经出来了,但是自己用了一次,闪退,换tomcat8,开启成功.(tomcat9这个原因有时间在琢磨) 修改tomcat的index.jsp 然后在index.js…

understand的安装

1.win7 64位下安装 1&#xff09;下载Understand.4.0.908.x64.rar。 2&#xff09;解压之&#xff0c;直接运行里面的Understand-4.0.908-Windows-64bit.exe。 3&#xff09;选择如下 之后&#xff0c;点击“Add Eval or SDL (RegCode)”&#xff0c;如下图&#xff1a; 4&…

C. Number of Pairs

C. Number of Pairs You are given an array a of n integers. Find the number of pairs (i,j) (1≤i<j≤n) where the sum of aiaj is greater than or equal to l and less than or equal to r (that is, l≤aiaj≤r). For example, if n3, a[5,1,2], l4 and r7, then t…