本文主要是介绍How Tomcat Works 1,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一直以来都在使用Tomcat作为servlet容器,很好奇tomcat的工作原理和结构。正好How tomcat works这本书讲解的比较清晰,虽然很多东西不懂,但也要慢慢来研究一下,这样才能有提高。
tomcat中最核心的部分是catalina,我们可以将其简单的看做由两个主要模块构成的,连接器connector和容器container,他们之间的对应关系如下
连接器负责为接收到得每个http请求构造一个resquest和response对象,然后传递给container,容器负责调用servlet的service方法。
下面就创建一个最简单的基于java的web服务器来响应请求。该服务器只能处理静态的资源请求。
一个基于java的web服务器要涉及到两个非常重要的类,socket和server socket,通过http协议进行通讯。
下面是一个HttpServer类
package server1;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;
import java.net.UnknownHostException;public class HttpServer {private boolean shutdown = false;//服务器是否停止public static String WEB_ROOT = System.getProperty("user.dir") + File.separator + "webroot";//服务器资源目录public static void main(String[] args) {System.out.println(WEB_ROOT);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 (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}Socket socket = null;InputStream input = null;OutputStream output = null;while(!shutdown){//不停的接受请求try {socket = serverSocket.accept();input = socket.getInputStream();output = socket.getOutputStream();} catch (IOException e) {e.printStackTrace();}Request request = new Request(input);request.parse();//解析urlResponse response = new Response(output);response.setRequest(request);try {response.sendStaticResource();//返回客户端socket.close();} catch (IOException e) {e.printStackTrace();}}}
}
下面是Request类
package server1;import java.io.IOException;
import java.io.InputStream;public class Request {private InputStream input;private String uri;public String getUri() {return uri;}public void setUri(String uri) {this.uri = uri;}public Request(InputStream input){this.input = input;}//读取客户端的请求,并解析出uripublic void parse(){StringBuffer request = new StringBuffer(2048);int i = 0;byte[] buffer = new byte[2048];try {i = input.read(buffer);} catch (IOException e) {e.printStackTrace();}for(int j=0;j<i;j++){request.append((char)buffer[j]);}System.out.println(request.toString());uri = this.parseUri(request.toString());System.out.println(uri);}//传入的requestString类似以GET /favicon.ico HTTP/1.1开头//该方法负责将uri截取出来public String parseUri(String requestString){int index1,index2;index1 = requestString.indexOf(" ");if(index1!=-1){index2 = requestString.indexOf(" ", index1+1);if(index2!=-1){return requestString.substring(index1+1, index2);}}return null;}}
下面是Response类
package server1;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;public class Response {private OutputStream output;Request request;private static final int BUFFER_SIZE = 1024;public void setRequest(Request request) {this.request = request;}public Response(OutputStream output){this.output = output;}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 i = fis.read(bytes);while(i!=-1){output.write(bytes, 0, i);i = fis.read(bytes);}}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>";output.write(errorMessage.getBytes());}} catch (Exception e) {e.printStackTrace();}finally{if(fis!=null)fis.close();}}
}
在浏览器中访问
http://localhost:8080/index.html即可。在工程的根目录建立webroot文件夹添加index.html即可访问到
这篇关于How Tomcat Works 1的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!