进来在看《How Tomcat Works》这本书,将读书笔记贴在这里,好记性不如烂笔头。

进来在看《How Tomcat Works》这本书,将读书笔记贴在这里,好记性不如烂笔头。 
最简单的一个服务器,一个很简单的结果。 
HttpServer构建ServerSocket,每次当请求到来是创建一个Socket,并创建Request,Response对象,根据URI读取位于WebRoot底下的静态资源。 
类如下: 
Java代码   收藏代码
  1. package com.ex01.pyrmont;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.io.OutputStream;  
  6. import java.net.InetAddress;  
  7. import java.net.ServerSocket;  
  8. import java.net.Socket;  
  9. import java.net.UnknownHostException;  
  10.   
  11. public class HttpServer {  
  12.     public final static String WEB_ROOT = "webRoot";  
  13.       
  14.     public final static String SHUT_DOWN = "/SHUTDOWN";  
  15.       
  16.     private boolean shutdown = false;  
  17.       
  18.     public static void main(String[] args) {  
  19.         HttpServer httpServer = new HttpServer();  
  20.         httpServer.await();  
  21.     }  
  22.       
  23.     public void await() {  
  24.         int port = 8080;  
  25.         ServerSocket serverSocket = null;  
  26.             try {  
  27.                 serverSocket = new ServerSocket(port,1,InetAddress.getByName("127.0.0.1"));  
  28.             } catch (UnknownHostException e) {  
  29.                 e.printStackTrace();  
  30.                 System.exit(1);  
  31.             } catch (IOException e) {  
  32.                 System.exit(1);  
  33.             }  
  34.         Socket socket = null;  
  35.         InputStream input;  
  36.         OutputStream out;  
  37.           
  38.         while(!shutdown){  
  39.             try {  
  40.                 socket = serverSocket.accept();  
  41.                 input = socket.getInputStream();  
  42.                 out = socket.getOutputStream();  
  43.                 Request request = new Request(input);  
  44.                 request.parse();  
  45.                   
  46.                 Response response = new Response(out);  
  47.                 response.setRequest(request);  
  48.                 response.sendStaticResource();  
  49.                   
  50.                 input.close();  
  51.                 out.close();  
  52.                 socket.close();  
  53.                   
  54.                 shutdown = request.getUri().equals(SHUT_DOWN);  
  55.             } catch (IOException e) {  
  56.                 e.printStackTrace();  
  57.             }   
  58.         }  
  59.           
  60.           
  61.     }  
  62. }  

Java代码   收藏代码
  1. package com.ex01.pyrmont;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5.   
  6. public class Request {  
  7.     private InputStream input;  
  8.       
  9.     private String uri;  
  10.       
  11.     public Request() {}  
  12.       
  13.     public Request(InputStream input){  
  14.         this.input = input;  
  15.     }  
  16.       
  17.     public void parse(){  
  18.         StringBuffer buffer =  new StringBuffer(2048);  
  19.         byte[] buf = new byte[2048];  
  20.         int len = 0;  
  21.         try {  
  22.             len = input.read(buf);  
  23.         } catch (IOException e) {  
  24.             len = 0;  
  25.             e.printStackTrace();  
  26.         }  
  27.         for(int i = 0; i < len; i++){  
  28.             buffer.append((char)buf[i]);  
  29.         }  
  30.         uri = parseUri(buffer.toString());  
  31.     }  
  32.       
  33.     private String parseUri(String uriStr){  
  34.         int spaceIndex = uriStr.indexOf(" ");  
  35.         if(spaceIndex != -1){  
  36.             int spaceSecondIndex = uriStr.indexOf(" ", spaceIndex + 1);  
  37.             if(spaceSecondIndex > spaceIndex){  
  38.                 return uriStr.substring(spaceIndex + 1,spaceSecondIndex);  
  39.             }  
  40.         }  
  41.         return null;  
  42.     }  
  43.       
  44.     public String getUri(){  
  45.         return uri;  
  46.     }  
  47. }  

Java代码   收藏代码
  1. package com.ex01.pyrmont;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.IOException;  
  6. import java.io.OutputStream;  
  7.   
  8. public class Response {  
  9.     public final static int BUFFER_SIZE = 2048;  
  10.     private OutputStream out;  
  11.     private Request request;  
  12.   
  13.     public Response() {}  
  14.   
  15.     public Response(OutputStream out) {  
  16.         this.out = out;  
  17.     }  
  18.   
  19.     public void sendStaticResource() throws IOException {  
  20.         String uri = request.getUri();  
  21.         FileInputStream fis = null;  
  22.         File file = new File(HttpServer.WEB_ROOT, uri);  
  23.         if (file.exists()) {  
  24.             fis = new FileInputStream(file);  
  25.             byte[] buf = new byte[BUFFER_SIZE];  
  26.             int len = fis.read(buf);  
  27.             while (len != -1) {  
  28.                 out.write(buf, 0, len);  
  29.                 len = fis.read(buf);  
  30.             }  
  31.         } else {  
  32.             // file not found  
  33.             String errorMessage = "HTTP/1.1 404 File Not Found\r\n"  
  34.                     + "Content-Type: text/html\r\n" + "Content-Length: 23\r\n"  
  35.                     + "\r\n" +  
  36.   
  37.                     "<h1>File Not Found</h1>";  
  38.             out.write(errorMessage.getBytes());  
  39.         }  
  40.         if(fis != null){  
  41.             fis.close();  
  42.         }  
  43.     }  
  44.   
  45.     public void setRequest(Request request) {  
  46.         this.request = request;  
  47.     }  
  48. }  

上面的程序比较简单,但也有一定的问题: 
1. 比如获取URI的过程,感觉就很不好,没有一定的容错性 
2. 异常的处理比较混乱等 
虽然这些tomcat都得到了很好的处理,并且有很多值得借鉴的地方,但是思考一下到后面估计和它有一定的共鸣。 

源码在google这里面有 http://www.google.com/codesearch/p?hl=en&sa=N&ct=rx&cd=9#X8Q3DKkF7lI/HowTomcatWorks/:java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值