实现Tomcat
- 定义请求对象
将字节流转成String 切割获取请求 和 请求方法
-
定义响应对象
将响应协议输出出去 -
写一个自定义注解 作用于类上面
在类前面加上该注解 判断是否是 servlet
该类上面有我们自己定义的注解 就可以吧他放到请求map中
该类继承了AbstractServlet 抽象类 -
该抽象类用于处理请求
package com.smile.tomcat.utils;
import com.smile.tomcat.annotion.RequestUrl;
import com.smile.tomcat.servlet.ServletMapping;
import javax.naming.Context;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
/**
* @author: Smile
* @description:
* @create: 2020-04-27 16:07
*/
public class FindFileHaveAnnotation {
/**
* 返回带注解的类
* @return
* @throws UnsupportedEncodingException
*/
public static List<ServletMapping> allServletMapping() throws UnsupportedEncodingException {
System.out.println("开始扫描包!"+ LocalDateTime.now());
List<ServletMapping> servletMappingList = new ArrayList<>();
String url = getClassPath();
List<String> classes = getClassesList(url);
classes.stream()
.filter(e -> e.startsWith("com"))
.forEach(e -> {
try {
RequestUrl annotation = Class.forName(e).getAnnotation(RequestUrl.class);
if (annotation != null) {
servletMappingList.add(new ServletMapping("1", annotation.value(), e));
}
} catch (ClassNotFoundException classNotFoundException) {
classNotFoundException.printStackTrace();
}
});
System.out.println("扫描结束!"+LocalDateTime.now());
return servletMappingList;
}
private static String getClassPath() throws UnsupportedEncodingException {
String url = URLDecoder.decode(Context.class.getResource("/").getPath(),
String.valueOf(Charset.defaultCharset()));
if (url.startsWith("/")) {
url = url.replaceFirst("/", "");
}
return url.replaceAll("/", "\\\\");
}
private static List<String> getClassesList(String url) {
File file = new File(url);
List<String> classes = getAllClass(file);
for (int i = 0; i < classes.size(); i++) {
classes.set(i, classes.get(i).replace(url, "").replace(".class", "").replace("\\", "."));
}
return classes;
}
private static List<String> getAllClass(File file) {
List<String> ret = new ArrayList<>();
if (file.isDirectory()) {
File[] list = file.listFiles();
for (File fileList : list) {
ret.addAll(getAllClass(fileList));
}
} else {
ret.add(file.getAbsolutePath());
}
return ret;
}
}
该utlis用于 扫描项目中所有包 判断类上面有没有 我们自己定义的注解 如有存在 就返回一个集合
package com.smile.tomcat;
import com.smile.tomcat.domain.MyRequest;
import com.smile.tomcat.domain.MyResponse;
import com.smile.tomcat.http.AbstractServlet;
import com.smile.tomcat.servlet.ServletMappingConfig;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
/**
* @author: Smile
* @description: \
* @create: 2020-04-27 14:35
*/
public class TomcatMain {
private int port = 8080;
private final Map<String, AbstractServlet> urlServletMap = new HashMap<>();
public TomcatMain(int port) {
this.port = port;
}
public TomcatMain() {
}
public void start() throws IOException {
initServletMapping();
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Tomcat started on port(s): " + port);
while (true) {
Socket accept = serverSocket.accept();
dispatch(accept);
}
}
private void initServletMapping() {
ServletMappingConfig.servletMappingList
.forEach(e -> {
try {
urlServletMap.put(e.getUrl(),
(AbstractServlet) Class.forName(e.getClazz()).newInstance());
} catch (Exception x) {
x.printStackTrace();
}
});
System.out.println("初始化完成!"+ LocalDateTime.now());
}
private void dispatch(Socket socket) throws IOException {
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
MyRequest myRequest = new MyRequest(inputStream);
MyResponse myResponse = new MyResponse(outputStream);
String url = myRequest.getUrl();
System.out.println("请求地址:"+url);
if (urlServletMap.containsKey(url)) {
urlServletMap.get(url).service(myRequest, myResponse);
} else {
myResponse.write("404 NOT FOUNT");
}
socket.close();
}
public static void main(String[] args) throws IOException {
new TomcatMain().start();
}
}