Solve the problem of cannot be cast to javax.servlet.Filter error _jsp programming

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.