了解tomcat
- tomcat由apache开源组织使用java开发的一款web容器
tomcat简单原理图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a2813ebe7c14b97fe2f719b3596c3cbe.png)
自定义实现基础tomcat功能
- 定义request类(用来接收客户端发送来的请求)
package com.shanlei;
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);
}
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.shanlei;
import jdk.internal.util.xml.impl.Input;
import javax.swing.text.html.HTML;
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>")
.append("<body>")
.append("<h1>" + str +"</h1>")
.append("</body>")
.append("</html>");
this.outputStream.write(builder.toString().getBytes());
this.outputStream.flush();
this.outputStream.close();
}
}
- 定义Mapping类(用来映射请求与applet)
package com.shanlei;
import java.util.HashMap;
public class MyMapping {
public static HashMap<String, String> mapping = new HashMap<>();
static{
mapping.put("/mytomcat", "com.shanlei.MyServlet");
}
public static HashMap<String, String> getMapping() {
return mapping;
}
}
- 定义HttpServlet抽象类(定义doGet与doPost与service方法)
package com.shanlei;
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 Exception;
public abstract void doPost(MyRequest myRequest, MyResponse myResponse) throws Exception;
public void Service(MyRequest myRequest,MyResponse myResponse) throws Exception{
if(METHOD_GET.equals(myRequest.getRequestMethod())){
doGet(myRequest, myResponse);
}else if(METHOD_POST.equals(myRequest.getRequestMethod())){
doPost(myRequest, myResponse);
}
}
}
- 定义Servlet类(继承HttpServlet,进行具体业务逻辑)
package com.shanlei;
import java.io.IOException;
public class MyServlet extends MyHttpServlet {
@Override
public void doGet(MyRequest myRequest, MyResponse myResponse) throws Exception {
myResponse.write("myTomcat");
}
@Override
public void doPost(MyRequest myRequest, MyResponse myResponse) throws Exception {
myResponse.write("POST Tomcat");
}
}
package com.shanlei;
import org.omg.CORBA.Request;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void startServer(int port) throws Exception {
ServerSocket serverSocket = new ServerSocket(port);
Socket socket = null;
while(true){
socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
MyRequest myRequest = new MyRequest(inputStream);
MyResponse myResponse = new MyResponse(outputStream);
String clazz = new MyMapping().getMapping().get(myRequest.getRequestUrl());
if(clazz!=null){
Class<MyServlet> myServletClass = (Class<MyServlet>)Class.forName(clazz);
MyServlet myServlet = myServletClass.newInstance();
myServlet.Service(myRequest, myResponse);
}
}
}
public static void main(String[] args) {
try {
startServer(8888);
} catch (Exception e) {
e.printStackTrace();
}
}
}