Discuss / Java / 使用嵌入式Tomcat运行Servlet

使用嵌入式Tomcat运行Servlet

Topic source

净净一隅

#1 Created at ... [Delete] [Delete and Lock User]

servlet是project项目下的一个module**, 没有这种结构的就不需要写进来**

不知道有没有获取当前模块路径的办法

public class Application {
public static void main(String[] args) throws Exception {
// 启动Tomcat:
Tomcat tomcat = new Tomcat();
tomcat.setPort(Integer.getInteger("port", 8080));
tomcat.getConnector();
// 创建webapp:
System.out.println(new File("servlet/src/main/webapp").getAbsolutePath());

Context ctx = tomcat.addWebapp("", new File("servlet/src/main/webapp").getAbsolutePath());
WebResourceRoot resources = new StandardRoot(ctx);
resources.addPreResources(
new DirResourceSet(resources, "/WEB-INF/classes", new File("servlet/target/classes").getAbsolutePath(), "/"));
ctx.setResources(resources);
tomcat.start();
tomcat.getServer().await();
}
}


  • 1

Reply