Tomcat原理
Tomcat解析
Tomcat定位为webServer
Tomcat嵌入,servlet
手动实现单机版Tomcat
基本流程图:
首先实现请求对象:
package com.wp;/*
*@author wupeng
*@time 2021/6/21-7:58
请求方法
*/
import java.io.IOException;
import java.io.InputStream;
public class MyRequest {
// 请求方法
private String requestMethod;
// 请求地址
private String requestUrl;
public MyRequest(InputStream inputStream) throws IOException {
//缓冲区空间
byte[] buffer = new byte[1024];
//读取数据的长度
int len = 0;
//定义请求变量
String str = null;
if((len = inputStream.read(buffer))> 0) {
str = new String(buffer,0,len);
}
//GET /HTTP1.1
String data = str.split("\n")[0];
String[] params = data.split(" ");
this.requestMethod = params[0];
this.requestUrl = params[1];
}
public String getRequestMethod() {
return requestMethod;
}
public void setRequestMethod(String requestMethod) {
this.requestMethod = requestMethod;
}
public String getRequestUrl() {
return requestUrl;
}
public void setRequestUrl(String requestUrl) {
this.requestUrl = requestUrl;
}
}
实现响应对象:
package com.wp;/*
*@author wupeng
*@time 2021/6/21-8:26
*/
import java.io.IOException;
import java.io.OutputStream;
public class MyResponse {
private OutputStream outputStream;
public MyResponse(OutputStream outputStream) {
this.outputStream = outputStream;
}
//将数据写到客户端
public void write(String str) throws IOException {
StringBuilder builder = new StringBuilder();
builder.append("HTTP/1.1 200 OK\n")
.append("Content-Type:text/html\n")
.append("\r\n")
.append("<html>\n")
.append("<body>")
.append("<h1>"+str+"</h1>")
.append("</body>")
.append("</html>");
this.outputStream.write(builder.toString().getBytes());
this.outputStream.flush();
this.outputStream.close();
}
}
实现映射关系:
package com.wp;/*
*@author wupeng
*@time 2021/6/21-8:35
*/
import java.util.HashMap;
public class MyMapping {
public static HashMap<String,String> map = new HashMap<>();
static {
map.put("/mytomcat","com.wp.MyServlet");
}
public HashMap<String,String> getMapping() {
return map;
}
}
实现接口:
package com.wp;/*
*@author wupeng
*@time 2021/6/21-8:38
*/
import java.io.IOException;
public abstract class MyHttpServlet {
// 定义常量
public static final String METHOD_GET = "GET";
public static final String METHOD_POST = "POST";
//判断什么方法
public abstract void doGet(MyRequest myRequest,MyResponse myResponse) throws IOException;
public abstract void doPost(MyRequest myRequest,MyResponse myResponse) throws IOException;
//根据请求方式来判断调用哪种处理方法
public void service(MyRequest myRequest,MyResponse myResponse) throws IOException {
if(METHOD_GET.equals(myRequest.getRequestMethod())) {
doGet(myRequest,myResponse);
} else if (METHOD_POST.equals(myRequest.getRequestMethod())){
doPost(myRequest,myResponse);
}
}
}
实现Servlet
package com.wp;/*
*@author wupeng
*@time 2021/6/21-8:57
*/
import java.io.IOException;
public class MyServlet extends MyHttpServlet {
@Override
public void doGet(MyRequest myRequest, MyResponse myResponse) throws IOException {
myResponse.write("mytomcat get");
}
@Override
public void doPost(MyRequest myRequest, MyResponse myResponse) throws IOException {
myResponse.write("post tomcat");
}
}
实现一个服务:
package com.wp;/*
*@author wupeng
*@time 2021/6/21-9:05
*/
import com.sun.corba.se.impl.orbutil.ObjectUtility;
import com.sun.deploy.nativesandbox.comm.Response;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
/*
定义服务端的接受程序,接受Socket请求
*/
public static void startServer(int port) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
//定义服务端套接字
ServerSocket serverSocket = new ServerSocket(port);
//定义客户端套接字
Socket socket = null;
while(true) {
socket = serverSocket.accept();
//获取输入流输出流
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
//定义请求对象
MyRequest request = new MyRequest(inputStream);
//定义响应对象
MyResponse response = new MyResponse(outputStream);
String clazz= new MyMapping().getMapping().get(request.getRequestUrl());
if(clazz != null) {
Class<MyServlet> myServletClass = (Class<MyServlet>) Class.forName(clazz);
//根据MyservletClass创建对象
MyServlet myServlet = myServletClass.newInstance();
myServlet.service(request,response);
}
}
}
public static void main(String[] args) {
try {
startServer(10086);
} catch (Exception e) {
e.printStackTrace();
}
}
}