挺好玩的,有利于初学对容器和servlet接口规范的理解
具体代码
package org.apache;
import javax.servlet.Servlet;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Scanner;
/**
* @author hrui
* @date 2023/6/20 21:35
*/
public class Tomcat {
public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
System.out.println("Tomcat启动.......");
System.out.println("模拟浏览器输入请求地址:");
Scanner scan=new Scanner(System.in);
while(true) {
String str = scan.nextLine();
//用ResourceBundle也可以
FileReader fileReader = new FileReader("web.properties");
Properties p = new Properties();
p.load(fileReader);
fileReader.close();
String property = p.getProperty("/aaa");
Class aClass = Class.forName(property);
Object o = aClass.newInstance();
Servlet s = (Servlet) o;
s.service();
}
}
}
package javax.servlet;
/**
* @author hrui
* @date 2023/6/20 21:42
*/
public interface Servlet {
void service();
}
package com.project;
import javax.servlet.Servlet;
/**
* @author hrui
* @date 2023/6/20 21:42
*/
public class ProjectList implements Servlet {
@Override
public void service() {
System.out.println("正在处理商品信息");
}
}
package com.project;
import javax.servlet.Servlet;
/**
* @author hrui
* @date 2023/6/20 21:41
*/
public class UserList implements Servlet {
@Override
public void service() {
System.out.println("正在处理用户信息");
}
}
web.properties的配置