MyRequest请求类
package com.hailu; 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 / HTTP/1.1 String data = str.split(" ")[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; } }
MyResponse响应类
package com.hailu; import java.io.OutputStream; public class MyResponse { private OutputStream outputStream; public MyResponse(OutputStream outputStream){ this.outputStream=outputStream; } public void write(String string) throws Exception{ StringBuilder stringBuilder=new StringBuilder(); stringBuilder.append("HTTP/1.1 200 OK ") .append("Content-Type:text/html ") .append(" ") .append("<html><body><h1>") .append(string) .append("</h1></body></html>"); this.outputStream.write(stringBuilder.toString().getBytes()); this.outputStream.flush(); this.outputStream.close(); } }
MyMapping用于映射哪个Servlet处理我们的请求
package com.hailu; import java.util.HashMap; public class MyMapping { public static HashMap<String,String> mapping=new HashMap<>(); static { mapping.put("/mytomcat","com.hailu.MyServlet"); } public HashMap<String,String> getMapping(){ return mapping; } }
MyHttpServlet抽象类
package com.hailu; 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; /** * 根据请求方式来判断用哪种处理方法 * @param myRequest * @param 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); } } }
MyHttpServlet
package com.hailu; public class MyServlet extends MyHttpServlet { @Override public void doGet(MyRequest myRequest, MyResponse myResponse) throws Exception { myResponse.write("GetMyTomcat"); } @Override public void doPost(MyRequest myRequest, MyResponse myResponse)throws Exception { myResponse.write("PostMyTomcat"); } }
MyServer
package com.hailu; import java.io.IOException; 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(); MyRequest myRequest = new MyRequest(socket.getInputStream()); MyResponse myResponse = new MyResponse(socket.getOutputStream()); 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(10086); } catch (Exception e) { e.printStackTrace(); } } }