美文网首页
基于Tomcat思想的简单web服务器(3)

基于Tomcat思想的简单web服务器(3)

作者: 初省 | 来源:发表于2019-05-08 14:55 被阅读0次

在Spring中,只需要加上注解就可以自动寻找到匹配的方法,不需要web.xml配置路径,这虽然不算是web服务器的功能,但是还是进行了简单的原理模拟

我们现在不需要servlet,只需要写出Controller类,然后给类加上路径注解即可

WebServer,ServerHandler,HTTPResponse均不变
删除ServletMapping,ServletMappingConfig,Servlet ,HelloServlet 类

注解类,value就是路径,路径必须是“/test”这种结构的

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE })
public @interface RequestMapping {

    String value() default "";

}

测试的Controller层

@RequestMapping(value = "/hello")
public class HelloController {

    @RequestMapping(value = "/say")
    public String sayHello() {
        return "hello";
    }


    @RequestMapping(value = "/error")
    public String error() {
        return "error 404";
    }

}

注解配置类,扫描包下所有的类,查看是否有注解,如果使用了注解就添加进list中
这里只是做了一个模拟,所以只扫描了当前文件夹下

public class MappingConfig {

    // 扫描当前包下的所有的注解类
    public static List<Class> searchClass() throws ClassNotFoundException {
        File file = new File("./target/classes/com/example/fall/WebServer");
        File[] files = file.listFiles();
        List<Class> list = new ArrayList<>();
        if(files != null) {
            for (File i : files) {
                String className = ("com.example.fall.WebServer." + i.getName());
                className = className.substring(0, className.length() - 6);
                Class clazz = Class.forName(className);
                Annotation annotation = clazz.getAnnotation(RequestMapping.class);
                if (annotation != null) {
                    list.add(clazz);
                }
            }
        }
        return list;
    }

}

Dispatcher 类,加载的时候先获得被注解的类列表,然后每次都中列表中匹配路径

public class Dispatcher {

    private static List<Class> classes;

    static {
        try {
            classes = MappingConfig.searchClass();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    public static void dispatcher(HttpRequest httpRequest, HTTPResponse httpResponse) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
        String uri = httpRequest.getUri();
        String clazzUri = "";
        String methodUri = "";
        int index1 = uri.indexOf("/");
        int index2 = uri.indexOf("/", index1 + 1);
        if (index2 != -1) {
            clazzUri = uri.substring(0, index2);
            int index3 = uri.indexOf("?", index2);
            if (index3 != -1) {
                methodUri = uri.substring(index2, index3);
            } else {
                methodUri = uri.substring(index2);
            }
        } else {
            clazzUri = "/hello";
            methodUri = "/error";
        }
        Object result = "Error 404";
        for (Class i : classes) {
            RequestMapping clazzAnno = (RequestMapping) i.getAnnotation(RequestMapping.class);
            if (clazzAnno.value().equals(clazzUri)) {
                Method[] methods = i.getDeclaredMethods();
                for (Method j : methods) {
                    RequestMapping methodAnno = (RequestMapping) j.getAnnotation(RequestMapping.class);
                    if (methodAnno.value().equals(methodUri)) {
                        Object obj = i.newInstance();
                        result = j.invoke(obj);
                    }
                }
            }
        }
        httpResponse.write((String) result);
    }

}

结果

访问http://localhost:8888/hello/say
出现hello
访问http://localhost:8888/hello/error
error 404

相关文章

  • 基于Tomcat思想的简单web服务器(3)

    在Spring中,只需要加上注解就可以自动寻找到匹配的方法,不需要web.xml配置路径,这虽然不算是web服务器...

  • 基于Tomcat思想的简单web服务器(1)

    本文是为了学习Tomcat源码之前,先理解web服务器的思想而写的 Tomcat作为web服务器,其实就是和客户端...

  • 基于Tomcat思想的简单web服务器(2)

    现在我们把原来的BIO改成基于netty的NIO 因为netty中自带了HTTPRequest,所以我们就不在需要...

  • Tomcat

    Apache-Tomcat 关系 web服务器工作原理 Tomcat Web 3大件 Tomcat 处理 http...

  • 1.tomcat服务器的使用

    1.web开发入门的了解 2.常用的web服务器的厂商 3.Tomcat服务器的认识 4.Tomcat服务器目录结...

  • tomcat安全加固和规范

    tomcat是一个开源Web服务器,基于Tomcat的Web运行效率高,可以在一般的硬件平台上流畅运行,因此,颇受...

  • Nginx实战

    主流服务器比较 Tomcat:开源、运行servlet和jsp web应用软件基于java的web应用软件容器。对...

  • Tomcat结构及处理请求过程

    Tomcat是一个基于组件形式的的Web容器,由Server(服务器)、Service(服务)、Connector...

  • Tomcat&

    Tomcat的学习 1.tomcat的解释 tomcat作用是发布网页中的. tomcat是web服务器 web容...

  • tomcat 如何做一个自己的个人网站

    使用tomcat发布 tomcat 网站的服务器,专业叫法:web服务器、web容器、web中间件 暂时可理解为指...

网友评论

      本文标题:基于Tomcat思想的简单web服务器(3)

      本文链接:https://www.haomeiwen.com/subject/bkcuoqtx.html