我有一个简单的hello world应用程序,嵌入了tomcat v.8.5.28
当我显式添加servlet和映射时,它工作正常:
public static void main(String[] args) throws Exception {
Tomcat server = new Tomcat();
server.setPort(8080);
Context context = server.addContext("", Paths.get(".").toAbsolutePath().toString());
Tomcat.addServlet(context, "hello", new HelloServlet());
context.addServletMappingDecoded("/", "hello");
server.start();
server.getServer().await();
}
但是,如果我删除addServlet
和addServletMappingDecoded
并使用@WebServlet(name = "HelloServlet", urlPatterns = "/hello")
注释我的servlet,它就不起作用。 http://localhost:8080/hello/
返回HTTP Status 404 – Not Found
我有这个文件层次结构:
这是jar中的文件层次结构:
尝试启动应用时,我有此输出
>java -jar servlet-app-1.0-SNAPSHOT-jar-with-dependencies.jar
Mar 09, 2018 3:57:36 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Mar 09, 2018 3:57:36 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Mar 09, 2018 3:57:36 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Tomcat]
Mar 09, 2018 3:57:36 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.5.28
Mar 09, 2018 3:57:36 AM org.apache.catalina.startup.ContextConfig getDefaultWebXmlFragment
INFO: No global web.xml found
Mar 09, 2018 3:57:36 AM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Mar 09, 2018 3:57:36 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
我应该将哪些路径传递给addWebapp
,以便tomcat可以找到我的web.xml
?