| 11 | import java.io.File; |
| 12 | |
| 13 | public class Main { |
| 14 | public static void main(String[] args) throws LifecycleException { |
| 15 | Tomcat tomcat = new Tomcat(); |
| 16 | tomcat.setPort(9976); |
| 17 | tomcat.setBaseDir("temp"); |
| 18 | |
| 19 | // 指定Web应用程序的根目录 |
| 20 | String webappDirLocation = "src/main/webapp/"; |
| 21 | Context ctx = tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath()); |
| 22 | // 关键:设置WebResourceRoot,让Tomcat扫描到你的类路径下的Servlet类 |
| 23 | File classesDir = new File("target/classes"); |
| 24 | WebResourceRoot resources = new StandardRoot(ctx); |
| 25 | resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes", |
| 26 | classesDir.getAbsolutePath(), "/")); |
| 27 | ctx.setResources(resources); |
| 28 | // 配置Jar扫描器以扫描Servlet注解 |
| 29 | StandardJarScanner jarScanner = new StandardJarScanner(); |
| 30 | jarScanner.setScanAllDirectories(true); |
| 31 | jarScanner.setScanClassPath(true); |
| 32 | ctx.setJarScanner(jarScanner); |
| 33 | |
| 34 | // 启动Tomcat服务器 |
| 35 | tomcat.start(); |
| 36 | System.out.println("Embedded Tomcat started at http://localhost:9976"); |
| 37 | |
| 38 | // 让服务器一直运行 |
| 39 | tomcat.getServer().await(); |
| 40 | } |
| 41 | } |
nothing calls this directly
no outgoing calls
no test coverage detected