1.封装请求对象,通过输入流,对http协议进行解析,获取http请求头的方法和url地址
import java.io.IOException;
import java.io.InputStream;
/**
* @Author Lxq
* @Date 2020/2/29 14:35
* @Version 1.0
* 封装请求对象
* 通过输入流,对http协议进行解析,获取http请求头的方法和url地址
*/
public class MyRequest {
// 请求url
private String url;
// 请求方法
private String method;
public MyRequest(InputStream inputStream) throws IOException {
String httpRequest = "";
byte[] bytes = new byte[1024];
int lenth = 0;
if ((lenth = inputStream.read(bytes)) > 0) {
httpRequest = new String(bytes, 0, lenth);
}
String httpHead = httpRequest.split("\\n")[0];
url = httpHead.split("\\s")[1];
method = httpHead.split("\\s")[0];
System.out.println(this);
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
}
2.封装响应对象,基于HTTP协议的格式进行输出写入
import com.sun.deploy.net.HttpResponse;
import java.io.IOException;
import java.io.OutputStream;
/**
* @Author Lxq
* @Date 2020/2/29 14:44
* @Version 1.0
* 封装响应对象
* 基于HTTP协议的格式进行输出写入。
*/
public class MyResponse {
private OutputStream outputStream;
public MyResponse(OutputStream outputStream) {
this.outputStream = outputStream;
}
public void write(String content) throws IOException {
StringBuffer httpResponse = new StringBuffer();
httpResponse.append("HTTP/1.1 200 OK\n")
.append("Content-Type: text/html\n")
.append("\r\n")
.append("<html><body>")
.append(content)
.append("</body></html>");
outputStream.write(httpResponse.toString().getBytes());
outputStream.close();
}
}
3.实现自己一个Servlet请求处理基类
/**
* @Author Lxq
* @Date 2020/2/29 14:45
* @Version 1.0
* Servlet请求处理基类
*/
public abstract class MyServlet {
public abstract void doGet(MyRequest request, MyResponse response);
public abstract void doPost(MyRequest request, MyResponse response);
public void service(MyRequest request, MyResponse response) {
if (request.getMethod().equalsIgnoreCase("get")) {
doGet(request, response);
} else if (request.getMethod().equalsIgnoreCase("post")) {
doPost(request, response);
}
}
}
4.实现自己特定servlet
import java.io.IOException;
/**
* @Author Lxq
* @Date 2020/2/29 14:53
* @Version 1.0
*/
public class GirlServlet extends MyServlet {
@Override
public void doGet(MyRequest request, MyResponse response) {
try {
response.write("get----------girl");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void doPost(MyRequest request, MyResponse response) {
try {
response.write("post----------girl");
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.IOException;
/**
* @Author Lxq
* @Date 2020/2/29 14:51
* @Version 1.0
*/
public class BoyServlet extends MyServlet {
@Override
public void doGet(MyRequest request, MyResponse response) {
try {
response.write("get--------boy");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void doPost(MyRequest request, MyResponse response) {
try {
response.write("post-----boy");
} catch (IOException e) {
e.printStackTrace();
}
}
}
5.servlet配置类
/**
* @Author Lxq
* @Date 2020/2/29 14:54
* @Version 1.0
* servlet配置
*/
public class ServletMapping {
private String servletName;
private String url;
private String clazz;
public ServletMapping(String servletName, String url, String clazz) {
this.servletName = servletName;
this.url = url;
this.clazz = clazz;
}
public String getServletName() {
return servletName;
}
public void setServletName(String servletName) {
this.servletName = servletName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
}
6.映射配置类
import java.util.ArrayList;
import java.util.List;
/**
* @Author Lxq
* @Date 2020/2/29 14:55
* @Version 1.0
* 映射配置类
*/
public class ServletMappingConfig {
public static List<ServletMapping> mappings = new ArrayList<>();
static {
mappings.add(new ServletMapping("findBoy","/boy","BoyServlet"));
mappings.add(new ServletMapping("findgirl","/girl","GirlServlet"));
}
}
6. 启动测试
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
/**
* @Author Lxq
* @Date 2020/2/29 14:59
* @Version 1.0
*/
public class MyTomcat {
private int port = 8088;
private Map<String, String> urlServletMap = new HashMap<>();
public MyTomcat(int port) {
this.port = port;
}
public void start() {
initServletMapping();
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
System.out.println("tomcat 启动");
while (true) {
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
MyRequest request = new MyRequest(inputStream);
OutputStream outputStream = socket.getOutputStream();
MyResponse response = new MyResponse(outputStream);
// 请求分发
dispath(request, response);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void dispath(MyRequest request, MyResponse response) {
String clazz = urlServletMap.get(request.getUrl());
try {
Class<MyServlet> aClass = (Class<MyServlet>) Class.forName(clazz);
MyServlet myServlet = aClass.newInstance();
myServlet.service(request, response);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private void initServletMapping() {
for (ServletMapping mapping : ServletMappingConfig.mappings) {
urlServletMap.put(mapping.getUrl(), mapping.getClazz());
}
}
public static void main(String[] args) {
new MyTomcat(8088).start();
}
}