Dubbo 3.0.0 正式釋出,王者歸來!
阿新 • • 發佈:2021-06-19
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();
}
}
}