Issue
Here is my Eclipse-wide JRE definition clearly showing the servlet-api.jar has been added:
And here are the same old import errors that just never seem to be able to be resolved. Isn't Java awesome? Any chance someone has a simple, factual answer as to why this still doesn't work?
Solution
jakarta.servlet
versus javax.servlet
Any chance someone has a simple, factual answer as to why this still doesn't work?
Because:
- You are building against the Tomcat 10.0.10 servlet-api JAR file.
- Tomcat 10.0.x implements version 5.0 of the Servlet spec; see http://tomcat.apache.org/whichversion.html
- Servlet 5.0 is Jakarta EE, not Java EE; see https://en.wikipedia.org/wiki/Jakarta_Servlet#History
- In later versions of Jakarta EE, the package names for the servlet classes have changed from
javax.servlet
tojakarta.servlet
.
And your code is trying to use the old package name.
Solutions:
- Change your webapp code to import from the new
jakarta.servlet
package, OR - Roll back to a version of Tomcat that supports the older version of the Servlet spec; i.e. Tomcat 9.0.x or earlier.
Answered By - Stephen C
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)