How Tomcat Works(二)
我们这些可怜虫,只有沿着大神的思路,这样我们才能进步得更快;因为我们不是跟大神处于同一级别上。所以我这里是参考《How Tomcat Works》这本英文版的大作来理解tomcat的工作原理
本人认为,Tomcat容器源码是学习java编程以及设计模式等的绝佳范例,深入理解其源码对我辈开发人员的编程水平提高大有裨益!
我们可以从该书指定的官方网址下载相关示例源码 http://www.brainysoftware.com,本文顺着作者的思路,介绍一个简单的web服务器
我们知道,web服务器是使用http协议与客户端进行通信,所以读者有必要先了解http协议格式;基于java的web服务器会使用两个重要的类 java.net.Socket与java.net.ServerSocket(服务器端与客户端通过Socket通信)
关于http协议,网上的资料汗牛充栋,本人在这里加上简略的描述(http协议基于tcp协议)
http客户端请求包括如下部分:
Method-URI-Protocol/Version 方法-地址-版本
Request header 请求头
Entity body 请求实体
比如http://www.outletss.com/ 是本人以前帮客户做的网站,如果我们在浏览器打开该url地址,实际上客户端向服务器发送了如下格式的消息
GET http://www.outletss.com/ HTTP/1.1
Host: www.outletss.com
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 5.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
Cookie: lzstat_uv=12863370662865423613|2989608; CKFinder_Path=Images%3A%2F%3A1; JSESSIONID=D7F9EC74149CB674D19A253B46273A77; lzstat_ss=1366758708_0_1375562495_2989608
http服务器端响应包括如下部分:
Protocol-Status code-Description 协议状态 描述代码
Response headers 响应头
Entity body 响应实体
然后服务器端向客户端响应了如下格式的消息
HTTP/1.1 200 OK
Connection: close
Date: Sat, 03 Aug 2013 15:00:30 GMT
Server: Microsoft-IIS/6.0
X-UA-Compatible: IE=EmulateIE7
X-Powered-By: ASP.NET
Set-Cookie: JSESSIONID=0A5B07FF5661CA6F8D87937A54B4EEF5; Path=/; HttpOnly
Content-Type: text/html;charset=UTF-8
Content-Language: zh-CN <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
//这里省略了html代码
</html>
基于java的Socket编程,可以参考java网络编程相关资料,Socket服务器端与客户端的网络交互与本地文件系统I/O具有一致的编程模型,基本上也是输入流与输出流的概念(如果你不懂输入流输出流的概念,基本上还要去修炼)
Socket分为客户端与服务器端,Socket表示客户端套接字,ServerSocket表示服务器端套接字,我们参考书中示例,看一个简单的服务器怎么实现
HttpServer类表示一个web服务器,示例代码如下:
public class HttpServer { /** WEB_ROOT is the directory where our HTML and other files reside.
* For this package, WEB_ROOT is the "webroot" directory under the working
* directory.
* The working directory is the location in the file system
* from where the java command was invoked.
*/
public static final String WEB_ROOT =
System.getProperty("user.dir") + File.separator + "webroot"; // shutdown command
private static final String SHUTDOWN_COMMAND = "/SHUTDOWN"; // the shutdown command received
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 = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
}
catch (IOException e) {
e.printStackTrace();
System.exit(1);
} // Loop waiting for a request
while (!shutdown) {
Socket socket = null;
InputStream input = null;
OutputStream output = null;
try {
socket = serverSocket.accept();
input = socket.getInputStream();
output = socket.getOutputStream(); // create Request object and parse
Request request = new Request(input);
request.parse(); // create Response object
Response response = new Response(output);
response.setRequest(request);
response.sendStaticResource(); // Close the socket
socket.close(); //check if the previous URI is a shutdown command
shutdown = request.getUri().equals(SHUTDOWN_COMMAND);
}
catch (Exception e) {
e.printStackTrace();
continue;
}
}
}
}
在上面代码里面,首先创建一个ServerSocket实例,然后用一个while循环监听客户端请求,接收到客户端请求后,通过ServerSocket实例的accept方法返回Socket实例,将该Socket实例的输入流与输出流封装成Request实例与Response实例,并调用Response实例的void sendStaticResource()方法响应请求。
Request类代码如下:
public class Request { private InputStream input;
private String uri; public Request(InputStream input) {
this.input = input;
} public void parse() {
// Read a set of characters from the socket
StringBuffer request = new StringBuffer(2048);
int i;
byte[] buffer = new byte[2048];
try {
i = input.read(buffer);
}
catch (IOException e) {
e.printStackTrace();
i = -1;
}
for (int j=0; j<i; j++) {
request.append((char) buffer[j]);
}
System.out.print(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;
} }
上面的void parse()方法是解析客户端的请求参数,这里是解析客户端请求的URL地址
Response类的代码如下:
/*
HTTP Response = Status-Line
*(( general-header | response-header | entity-header ) CRLF)
CRLF
[ message-body ]
Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
*/ 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() throws IOException {
byte[] bytes = new byte[BUFFER_SIZE];
FileInputStream fis = null;
try {
File file = new File(HttpServer.WEB_ROOT, request.getUri());
if (file.exists()) {
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 {
// file not found
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) {
// thrown if cannot instantiate a File object
System.out.println(e.toString() );
}
finally {
if (fis!=null)
fis.close();
}
}
}
这里最重要的是void sendStaticResource()方法,用于向输出流写入数据(这里是静态文件),响应客户端请求
本文介绍的是一个最简单的web服务器,Tomcat容器的复杂性远不止如此简单,待后文接着分析
---------------------------------------------------------------------------
本系列How Tomcat Works系本人原创
转载请注明出处 博客园 刺猬的温驯
本人邮箱: chenying998179#163.com (#改为@)
本文链接 http://www.cnblogs.com/chenying99/p/3229192.html
How Tomcat Works(二)的更多相关文章
- How Tomcat works — 二、tomcat启动(1)
主要介绍tomcat启动涉及到的一些接口和类. 目录 概述 tomcat包含的组件 server和service Lifecycle Container Connector 总结 概述 tomcat作 ...
- How Tomcat works — 四、tomcat启动(3)
上一节说到StandardService负责启动其子组件:container和connector,不过注意,是有先后顺序的,先启动container,再启动connector,这一节先来看看conta ...
- How Tomcat Works(十二)
tomcat容器通过一个称为Session管理器的组件来管理建立的Session对象,该组件由org.apache.catalina.Manager接口表示:Session管理器必须与一个Contex ...
- how tomcat works 读书笔记(二)----------一个简单的servlet容器
app1 (建议读者在看本章之前,先看how tomcat works 读书笔记(一)----------一个简单的web服务器 http://blog.csdn.net/dlf123321/arti ...
- How Tomcat Works(二十)
要使用一个web应用程序,必须要将表示该应用程序的Context实例部署到一个host实例中.在tomcat中,context实例可以用war文件的形式来部署,也可以将整个web应用拷贝到Tomcat ...
- 攻城狮在路上(肆)How tomcat works(零) 前言说明
最近几篇是关于How tomcat works一书的读书笔记. 通过数个章节逐渐实现一个tomcat的功能. 源码下载地址:http://zhidao.baidu.com/share/7007af0f ...
- How Tomcat Works(十四)补充
在How Tomcat Works(十四)中,本人并没有对javax.servlet.Filter及javax.servlet.FilterChain做详细的描述,本文在这里做一下补充 FilterC ...
- How Tomcat Works(十八)
在前面的文章中,如果我们要启动tomcat容器,我们需要使用Bootstrap类来实例化连接器.servlet容器.Wrapper实例和其他组件,然后调用各个对象的set方法将它们关联起来:这种配置应 ...
- How Tomcat Works(十七)
在前面的文章中,已经学会了如何通过实例化一个连接器和容器来获得一个servlet容器,并将连接器和容器相关联:但在前面的文章中只有一个连接器可用,该连接器服务8080端口上的HTTP请求,无法添加另一 ...
随机推荐
- GLSL学习_高斯滤波
使用RenderMonkey: 从自带的sobel修改的. vertex: uniform float height; varying vec4 vTexCoords00; varying vec4 ...
- @Repository @Resource
Spring的注解形式:@Repository.@Service.@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean. @Repository.@Service.@C ...
- Python 删除 数组
numpy删除一列 从0开始,第三个参数是第几个维度 可以多删几个
- (七)7.2 应用机器学习方法的技巧,准确率,召回率与 F值
建立模型 当使用机器学习的方法来解决问题时,比如垃圾邮件分类等,一般的步骤是这样的: 1)从一个简单的算法入手这样可以很快的实现这个算法,并且可以在交叉验证集上进行测试: 2)画学习曲线以决定是否更多 ...
- Android堆栈分析
在开发中,与界面跳转联系比较紧密的概念是Task(任务)和Back Stack(回退栈).activity的启动模式会影响Task和Back Stack的状态, 进而影响用户体验.除了启动模式之外,I ...
- $(document).Ready()方法 VS OnLoad事件 VS $(window).load()方法
$(document).Ready()方法 VS OnLoad事件 VS $(window).load()方法接触JQuery一般最先学到的是何时启动事件.在曾经很长一段时间里,在页面载入后引发的事件 ...
- api-ms-win-crt-runtime-l1-1-0.dll丢失问题
笔者是在安装python 3.5 后,启动时提示该文件丢失的问题的,如下所示.
- LoadRunner error -27728
错误现象1:Action.c(16): Error -27728: Step download timeout (120 seconds) has expired whendownloading no ...
- js获取浏览器高度和宽度值,尽量的考虑了多浏览器。
js获取浏览器高度和宽度值,尽量的考虑了多浏览器. IE中: document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ...
- linux grub
grub 是目前使用最广泛的linux引导装载程序,旧的lilo这个引导装载程序很少见了,grub优点: 支持较多哦的文件系统,可以使用grub主程序在文件系统中查找内核文件名 启动的时候,可以自行编 ...