Tomcat 10.0.4 doesn’t load servlets (@WebServlet classes) with 404 error [duplicate]

For copyright reasons the Servlet 5.0 API (implemented by Tomcat 10) and the Servlet 4.0 API (implemented by Tomcat 9) are incompatible: the API namespace changed from javax.* to jakarta.*. This can manifest in many ways:

  1. Software written for Servlet 4.0 does not compile against the API jars from Tomcat 10: cf. Servlet 5.0 JAR throws compile error on javax.servlet.* but Servlet 4.0 JAR does not,
  2. Servlet 4.0 applications which use a web.xml descriptor throw a lot of ClassNotFoundExceptions and don’t start: cf. Tomcat 10.x throws java.lang.NoClassDefFoundError on javax/servlet/ServletRequestListener.
  3. Servlet 4.0 applications which use a web.xml descriptor log a “X is not a jakarta.servlet.Servlet” error: cf. Servlet class org.restlet.ext.servlet.ServerServlet is not a jakarta.servlet.Servlet.
  4. Servlet 4.0 applications which use annotations to declare servlets stop working, as in your case,
  5. Servlet 4.0 applications which rely on a ServletContainerInitializer (like Spring and Spring Boot applications) don’t start: cf. Deploying Spring MVC 5 on Tomcat 10 … deployment problems

The last one is the hardest to diagnose: no errors are written to the log files, but the application doesn’t work. The reason behind this behavior is that @javax.servlet.WebServlet annotations are ignored: the server is scanning for @jakarta.servlet.WebServlet.

Since all three problems have the same cause, the solutions provided to the aforementioned questions all work. In this specific case I would advise to use the Tomcat Migration Tool for Jakarta EE.

Remark: The Tomcat download site features a warning, that unfortunately many people don’t notice:

Users of Tomcat 10 onwards should be aware that, as a result of the move from Java EE to Jakarta EE as part of the transfer of Java EE to the Eclipse Foundation, the primary package for all implemented APIs has changed from javax.* to jakarta.*. This will almost certainly require code changes to enable applications to migrate from Tomcat 9 and earlier to Tomcat 10 and later.

Leave a Comment