addContext()与addWebapp()这两种方法都是向嵌入式tomcat添加web应用程序。
addContext()需要配置相关所有内容,比如配置默认的Servlet,否则将无法访问静态资源。
addWebapp()可以理解为拥有自动配置功能,同时我们也可以通过次方法添加Servlet,指定静态资源路径。如下:
Context context = tomcat.addWebapp(tomcatConfiguration.getContextPath(), baseDir + File.separator + "webapp");
tomcat.addWebapp("/img","D:\\img");
Wrapper hello = tomcat.addServlet(context, "hello", new HelloServlet());
hello.addMapping("/hello");
Wrapper hello2 = tomcat.addServlet(context, "hello2", new HelloServlet2());
hello2.addMapping("/hello2");
tomcat8API:http://tomcat.apache.org/tomcat-8.0-doc/api/index.html