当前位置:网站首页>模拟Servlet的本质

模拟Servlet的本质

2022-07-07 14:15:00 Vayne的胖虎子

项目的目录结构

---src
	---web.properties
	---javax.servlet
		---Servlet
	---web
		---Tomcat
	---service
	    ---UserListServlet
	    ---UserLoginServlet
  • web.properties
/aaa=service.UserListServlet
/bbb=service.UserLoginServlet
  • Servlet
package javax.servlet;

/** * Author:LinYue * Time:2022-06-27 10:29:54 */
public interface Servlet {
    
    void service();
}
  • Tomcat
package web;

import javax.servlet.Servlet;
import java.util.ResourceBundle;
import java.util.Scanner;

/** * Author:LinYue * Time:2022-06-27 10:32:13 */
public class Tomcat {
    
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    
        System.out.println("Tomcat启动了....");
        Scanner s = new Scanner(System.in);
        System.out.print("请输入访问路径:");
        String path = s.nextLine();
        ResourceBundle bundle = ResourceBundle.getBundle("web");
        String resource = bundle.getString(path);
        Class clazz = Class.forName(resource);
        Object obj = clazz.newInstance();
        Servlet servlet = (Servlet) obj;
        servlet.service();
    }
}

  • UserListServlet
package service;

import javax.servlet.Servlet;

/** * Author:LinYue * Time:2022-06-27 10:31:06 */
public class UserListServlet implements Servlet {
    
    @Override
    public void service() {
    
        System.out.println("UserListServlet的service方法调用了!");
    }
}

  • UserLoginServlet
package service;

import javax.servlet.Servlet;

/** * Author:LinYue * Time:2022-06-27 10:31:06 */
public class UserLoginServlet implements Servlet {
    
    @Override
    public void service() {
    
        System.out.println("UserLoginServlet的service方法调用了!");
    }
}

原网站

版权声明
本文为[Vayne的胖虎子]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Limenrence/article/details/125479838