java: cannot access javax.servlet.ServletException class file for javax.servlet.ServletException not found

Solution 1:

Why do I have to include Java Servlet API dependency when I migrate to Tomcat 10 ?

Presumably because your application has a dependency on something that conforms to the JSP 4.0.x specs.

I did include Java Servlet API dependency, but is that a proper fix?

No it is not the proper fix. While you have succeeded in building your app, you will most get classloader errors when you deploy to Tomcat 10. That's because the Tomcat 10 runtime libraries don't provide the javax.servlet.* classes.

The correct approach is to identify why your application or its dependencies are trying to use classes in javax.servlet ... and eliminate this.

  1. Remove the javax.servlet / javax.servlet-api / 4.0.1 dependency.

  2. Search your application codebase for any remaining references to javax.servlet ... and change them.

  3. Use the Maven dependency tree plugin to try to identify which of your applications dependencies has a dependency on the old javax.servlet API. Look for newer versions of those dependencies that use jakarta.servlet instead.

If 2 and 3 don't identify the culprit, it gets rather difficult. You might need to unpack all of the dependent JAR files and "analyze" them using javap and grep. Or there might be better way.

If you are able to identify the dependencies, but you can't find a Jakarta-compatible version, then you have another kind of problem. You have to choose between putting in the effort to port the dependency, finding another library (or whatever) to provide the functionality, or remove the functionality from your web app.