Solve the problem that cannot be cast to javax. servlet. Filter reports an error. javax. servlet. filter

Source: Internet
Author: User

Solve the problem that cannot be cast to javax. servlet. Filter reports an error. javax. servlet. filter

Cannot be cast to javax. servlet. Filter error due to servlet-api.jar conflict

When you use maven to develop a web application, an error is reported during startup:

Jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet. class

Then an error is output:

Severe: Exception starting filter encodingFilterjava. lang. ClassCastException: org. springframework. web. filter. CharacterEncodingFilter cannot be cast to javax. servlet. Filter

The cause is that after tomcat is started, all the jar packages in the tomcat/lib directory are read into the memory, if the WEB-INF/lib directory in the application in the webapps directory has the same package, it will not be loaded,

Similar problems may occur between packages of different versions.

The solution to this problem is to use the <scope> label for the servlet-ap.jar, servlet-api and jsp-api are used during compilation, but these two dependencies are not needed during packaging.

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 five values:

* Compile, default value, applicable to all stages and will be released along with the project.
* Provided, similar to compile, expects JDK, container, or user to provide this dependency. For example, servlet. jar.
* Runtime, used only at runtime, such as the JDBC driver, applicable to the running and testing stages.
* Test, which is only used for testing and is used to compile and run the test code. Will not be released along with the project.
* System, similar to provided, must explicitly provide jar containing dependencies. Maven will not find it in Repository.

The above is the solution for cannot be cast to javax. servlet. Filter to report errors. We will continue to organize relevant knowledge in the future. Thank you for your support for 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.