How Tomcat Works 读书笔记(第一章)
进来在看《How Tomcat Works》这本书,将读书笔记贴在这里,好记性不如烂笔头。最简单的一个服务器,一个很简单的结果。
HttpServer构建ServerSocket,每次当请求到来是创建一个Socket,并创建Request,Response对象,根据URI读取位于WebRoot底下的静态资源。
类如下:
package com.ex01.pyrmont;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import java.net.UnknownHostException;public class HttpServer {public final static String WEB_ROOT = "webRoot";public final static String SHUT_DOWN = "/SHUTDOWN";private boolean shutdown = false;public static void main(String[] args) {HttpServer httpServer = new HttpServer();httpServer.await();}public void await() {int port = 8080;ServerSocket serverSocket = null;try {serverSocket = new ServerSocket(port,1,InetAddress.getByName("127.0.0.1"));} catch (UnknownHostException e) {e.printStackTrace();System.exit(1);} catch (IOException e) {System.exit(1);}Socket socket = null;InputStream input;OutputStream out;while(!shutdown){try {socket = serverSocket.accept();input = socket.getInputStream();out = socket.getOutputStream();Request request = new Request(input);request.parse();Response response = new Response(out);response.setRequest(request);response.sendStaticResource();input.close();out.close();socket.close();shutdown = request.getUri().equals(SHUT_DOWN);} catch (IOException e) {e.printStackTrace();} }}}
package com.ex01.pyrmont;import java.io.IOException;import java.io.InputStream;public class Request {private InputStream input;private String uri;public Request() {}public Request(InputStream input){this.input = input;}public void parse(){StringBuffer buffer =new StringBuffer(2048);byte[] buf = new byte;int len = 0;try {len = input.read(buf);} catch (IOException e) {len = 0;e.printStackTrace();}for(int i = 0; i < len; i++){buffer.append((char)buf);}uri = parseUri(buffer.toString());}private String parseUri(String uriStr){int spaceIndex = uriStr.indexOf(" ");if(spaceIndex != -1){int spaceSecondIndex = uriStr.indexOf(" ", spaceIndex + 1);if(spaceSecondIndex > spaceIndex){return uriStr.substring(spaceIndex + 1,spaceSecondIndex);}}return null;}public String getUri(){return uri;}}
package com.ex01.pyrmont;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.OutputStream;public class Response {public final static int BUFFER_SIZE = 2048;private OutputStream out;private Request request;public Response() {}public Response(OutputStream out) {this.out = out;}public void sendStaticResource() throws IOException {String uri = request.getUri();FileInputStream fis = null;File file = new File(HttpServer.WEB_ROOT, uri);if (file.exists()) {fis = new FileInputStream(file);byte[] buf = new byte;int len = fis.read(buf);while (len != -1) {out.write(buf, 0, len);len = fis.read(buf);}} else {// file not foundString 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>";out.write(errorMessage.getBytes());}if(fis != null){fis.close();}}public void setRequest(Request request) {this.request = request;}}
上面的程序比较简单,但也有一定的问题:
1. 比如获取URI的过程,感觉就很不好,没有一定的容错性
2. 异常的处理比较混乱等
虽然这些tomcat都得到了很好的处理,并且有很多值得借鉴的地方,但是思考一下到后面估计和它有一定的共鸣。
源码在google这里面有http://www.google.com/codesearch/p?hl=en&sa=N&ct=rx&cd=9#X8Q3DKkF7lI/HowTomcatWorks/:java
页:
[1]