2

I have a simple hello world application with embedded tomcat v.8.5.28

It works fine when I explicitly add servlet and mapping:

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();
}

But if I delete addServlet and addServletMappingDecoded and annotate my servlet with @WebServlet(name = "HelloServlet", urlPatterns = "/hello") it doesn't work. http://localhost:8080/hello/ returns HTTP Status 404 – Not Found

I have this files hierarchy:

enter image description here

And this is file hierarchy in jar:

enter image description here

I have this output when trying to start my app

>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"]

What paths should I pass to addWebapp so tomcat can find my web.xml?

Kirill
  • 1,540
  • 4
  • 18
  • 41

0 Answers0