You're basically physically including Tomcat 9.x (Servlet 4.0) specific JAR file in WAR and then writing/compiling code against Tomcat 9.x (Servlet 4.0) or older and then then deploying the WAR to Tomcat 10.x (Servlet 5.0) or newer. This is not the correct approach at all.
Since Tomcat 10.x (Servlet 5.0) the javax.*
package has been renamed to jakarta.*
package.
In other words, please make sure that you don't randomly put JAR files of a different server in your WAR such as tomcat-servlet-api-9.0.4.jar
. This will only cause trouble. Remove it altogether and edit the imports of your servlet class from
import javax.servlet.*;
import javax.servlet.http.*;
to
import jakarta.servlet.*;
import jakarta.servlet.http.*;
While at it, please also make sure that the root element of the web.xml
is declared conform the Servlet API version of the target server, which is in case of Tomcat 10.x thus Servlet 5.0 (and thus not Servlet 3.1 which basically matches Tomcat 8.0).
<web-app
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0"
>
<!-- Config here. -->
</web-app>
See also:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…