1. servlet容器是如何工作的?
- 创建一个Request对象,填充一些信息比如参数、headers、cookies、查询字符串、URI等。一个Request对象是javax.servlet.ServletRequest或javax.servlet.http.ServletRequest接口的实例。
- 创建一个Response对象,用于调用的servlet向客户端传递响应信息。其是javax.servlet.ServletResponse或javax.servlet.http.ServletResponse的实例。
- 调用servlet的service方法,传递request和response对象。servlet从request对象中读取值,向response对象中写入值。
2.Catalina主要模块
- Connector,连接器主要是连接请求到容器。它的工作是为每一个接收到http请求构建一个request和response对象,接下来传递给待处理的容器。
- Container,容器从连接器接收到request和response对象,负责调用servlet的service方法。
连接器和容器是多对1的关系(*对1)
3.tomcat4和5对比
- tomcat5支持servlet2.4和jsp2.0规范,tomcat4支持servlet2.3和jsp1.2
- tomcat5拥有比tomcat4更高效的默认连接器
- tomcat5使用更少的资源。因为tomcat5共享一个后台处理的线程,而tomcat4的每个模块都有自己的后台处理线程。
- tomcat5代码更简洁。因为不需要一个mapper组件来找到一个子组件。
一个简单的HttpServer代码示例:
流程图:
package com.flyer.tomcat.first; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; /** * 一个简单的http服务器 * <p /> * * @author Administrator */ public class HttpServer { private final static String SHUTDOWN_COMMAND = "/shutdown"; public final static String WEB_ROOT = System.getProperty("user.dir") + File.separator + "webroot"; private boolean shutdown = false; public static void main(String[] args) { System.out.println("server start"); System.out.println(WEB_ROOT); HttpServer server = new HttpServer(); try { server.await(); } catch (IOException e) { System.out.println(e.getMessage()); } } private void await() throws IOException { ServerSocket serverSocket = new ServerSocket(9090,1,InetAddress.getByName("127.0.0.1")); InputStream input = null; OutputStream output = null; while (!shutdown) { Socket socket = null; try { socket = serverSocket.accept(); input = socket.getInputStream(); output = socket.getOutputStream(); Request request = new Request(input); request.parse(); Response response = new Response(output); response.setRequest(request); response.sendStaticResource(); shutdown = request.getUri().equals(SHUTDOWN_COMMAND); } catch (Exception e) { e.printStackTrace(); } finally { socket.close(); input.close(); output.close(); } } } }
Request类
package com.flyer.tomcat.first; import java.io.IOException; import java.io.InputStream; public class Request { private byte[] buffer = new byte[1024]; private InputStream input; private String uri; public Request(InputStream input) { this.setInput(input); } /** * @return */ public InputStream getInput() { return input; } /** * @param */ public void setInput(InputStream input) { this.input = input; } public void parse() { int count = 0; StringBuilder sb = new StringBuilder(); try { count = input.read(buffer); } catch (IOException e) { count = -1; } if (count != -1) { for (int i = 0; i < count; i++) { sb.append((char) buffer[i]); } } uri = parseUri(sb.toString()); } private String parseUri(String requestString) { int index1, index2; String uri = requestString; index1 = uri.indexOf(" "); if (index1 != -1) { index2 = uri.indexOf(" ", index1 + 1); if (index1 < index2) { uri = uri.substring(index1 + 1, index2); return uri; } } return ""; } /** * @return */ public String getUri() { return uri; } // public static void main(String[] args) { // Request request = new Request(new InputStream() { // // @Override // public int read() throws IOException { // // TODO Auto-generated method stub // return 0; // } // // }); // String test = "GET /index.jsp HTTP/1.1"; // System.out.println(request.parseUri(test)); // } }
Response类
package com.flyer.tomcat.first; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Response { public final static int BUFFER_SIZE = 1024; private Request request; private OutputStream output; public Response(OutputStream output) { this.setOutput(output); } /** * @return */ public Request getRequest() { return request; } /** * @param */ public void setRequest(Request request) { this.request = request; } public void sendStaticResource() { byte[] buffer = new byte[BUFFER_SIZE]; InputStream inputStream = null; try { File file = new File(HttpServer.WEB_ROOT + request.getUri()); if (file.exists()) { inputStream = new FileInputStream(file); int count = 0; while ((count = inputStream.read(buffer)) != -1) { output.write(buffer, 0, count); } } else { 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) { // TODO: handle exception } finally { try { if (inputStream != null) { inputStream.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * @return */ public OutputStream getOutput() { return output; } /** * @param */ public void setOutput(OutputStream output) { this.output = output; } }