JAVA手写tomcat,带你了解tomcat的原理,附代码

1 创建一个简单的servlet
代码示例:

package springmvc;
import JAVA.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet{
/**
* 一个最简单的servlet
*/
private static final long serialVersionUID = 7683475760018217521L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String data=https://www.isolves.com/it/wlyx/fwq/2019-11-20/"这是我的第一个servlet!";
System.out.println(data);
}
}
web.xml中的标签确定了servlet的访问路径
<!DOCTYPE web-App PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>littleServlet</servlet-name>
<servlet-class>springmvc.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>littleServlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
</web-app>
servlet完成之后需要需要将他打包成项目(war/jar) , 并解压完成 , 形成完整的项目目录 , 供后续自己写的Tomcat使用 。通常情况下 , 所有的项目都会放入tomcat的webapps目录下 。
开始手动编写tomcat代码代码示例
package springboot.tomcat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.Principal;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.servlet.AsyncContext;
import javax.servlet.DispatcherType;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpUpgradeHandler;
import javax.servlet.http.Part;
import springboot.util.ProjectUtil;
/**
* @author liuhongya328
*
*/
public class TomcatServer {
static int threads = 10;
private static ExecutorService pool = Executors.newCachedThreadPool();
/**
* @param args
*/
public static void main(String[] args) throws Exception{
//加载项目,获取tomcat需要发布的所有项目及项目下的所有servlet信息
final Map<String, ProjectUtil.WebXml> projectIno = ProjectUtil.load();
ServerSocket serverSocket = new ServerSocket(8080);
System.out.println("Tomcat 启动成功");
while(!serverSocket.isClosed()) {
//阻塞获取新连接
Socket socket = serverSocket.accept();
pool.submit(()->{
//接收数据
InputStream inputStream = socket.getInputStream();;
//请求响应结果
OutputStream outputStream = socket.getOutputStream();
try {
System.out.println("收到请求:-------");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
String msg = null;
StringBuilder requestInfo = new StringBuilder();
while((msg = reader.readLine())!=null) {
if(msg.length()==0) {
break;
}
requestInfo.append(msg).append("rn");
}
System.out.println(requestInfo.toString()+"--------");
//请求需要访问的servlet,这个servlet相当于业务代码的controller , 是需要放入tomcat中启动的项目 , 用原生的servlet便于从底层代码理解
//1.获取请求方式 及请求路径 GET /SpringMVC/servlet/littleServlet HTTP/1.1
String firstLine = requestInfo.toString().split("rn")[0];
String projectName = firstLine.split(" ")[1].split("/")[1];


推荐阅读