Cannot is cast to javax.servlet.Filter error, cause Servlet-api.jar conflict
Use MAVEN to develop a Web application that complains when it starts:
Jar not loaded. For a Servlet Spec 2.3, section 9.7.2. Offending Class:javax/servlet/servlet.class
And then output the error:
Serious: Exception starting filter Encodingfilter
java.lang.ClassCastException: Org.springframework.web.filter.CharacterEncodingFilter cannot be cast to
Javax.servlet.Filter
The cause of the problem is that after Server-:tomcat startup, the jar packets in the Tomcat/lib directory are read into memory, and if the WebApps directory has the same package in the Web-inf/lib directory, it will fail to load.
Similar problems can occur between different versions of packages
The way to solve this problem is to use the <scope> tag for Servlet-ap.jar, compile it with Servlet-api and JSP-API, but without these two dependencies when packing
As follows:
<dependency>
<groupId>javax.servlet</groupId>
<artifactid>servlet-api</ artifactid>
<version>2.4</version>
<scope>provided</scope>
</ Dependency>
<dependency> <scope>, which mainly manages the deployment of dependencies. Currently <scope> can use 5 values:
* Compile, default, applies to all phases, and is published along with the project.
* provided, similar to compile, expects JDK, containers, or users to provide this dependency. such as Servlet.jar.
* Runtime, used only at run time, such as JDBC driver, applicable to run and test phase.
* Test, used only during testing, for compiling and running test code. Will not be published with the project.
* System, similar to provided, needs to explicitly provide jar,maven that contains dependencies and does not find it in repository.
The above is cannot is cast to javax.servlet.Filter the solution of the error, follow-up to continue to collate relevant knowledge, thank you for your support of this site!