java ClassCastException:DispatcherServlet 无法转换为 Servlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17799295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 15:01:38  来源:igfitidea点击:

ClassCastException: DispatcherServlet cannot be cast to Servlet

javaspringmavenservletspom.xml

提问by Arun

I have created a new Spring MVC project using maven-eclipse and the following error is thrown:

我使用 maven-eclipse 创建了一个新的 Spring MVC 项目,并抛出以下错误:

(I tried some solutions from the stackoverflow which is not working in my case. Some problem with the pom.xml which I could not find. I added provided scope for the servlet-api and tried which was not working either.)

(我尝试了 stackoverflow 中的一些解决方案,这在我的情况下不起作用。我找不到 pom.xml 的一些问题。我为 servlet-api 添加了提供的范围,并尝试了哪个也不起作用。)

SEVERE: Servlet /Remindem threw load() exception
java.lang.ClassCastException: org.springframework.web.servlet.DispatcherServlet 
  cannot be cast to javax.servlet.Servlet

My pom.xml is as follows:

我的 pom.xml 如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.org</groupId>
<artifactId>Remindem</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>Remindem</name>
<url>http://maven.apache.org</url>

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.8.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.0.0.Beta2</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.12.1.GA</version>
    </dependency>
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4</version>
    </dependency>

    <!-- Spring framework -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        <version>2.5.6</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>2.5.6</version>
        <scope>compile</scope>

    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.2.3.RELEASE</version>
        <scope>compile</scope>
    </dependency>


    <!-- Spring MVC framework -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>2.5.6</version>
        <scope>compile</scope>
    </dependency>

    <!-- JSTL -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.1.2</version>
    </dependency>

    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-api</artifactId>
        <version>7.0.19</version>
    </dependency>
</dependencies>

<build>
    <plugins>
<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.0</version>
    <configuration>
      <url>http://localhost:8080/Remindem</url>
    </configuration>
  </plugin>
    </plugins>
</build>

My web.xml is as follows:

我的 web.xml 如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Remindem</display-name>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

     <servlet>
        <servlet-name>svn</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/SpringAppServlet.xml               
            </param-value>
        </init-param>
    <load-on-startup>1</load-on-startup>
</servlet> 

 <servlet-mapping>
    <servlet-name>svn</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping> 

 <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

</web-app>

回答by Sotirios Delimanolis

Change

改变

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>compile</scope>
</dependency>

to

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

providedhas this description:

provided有这样的描述:

This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

这很像 compile,但表明您希望 JDK 或容器在运行时提供依赖项。例如,在为 Java Enterprise Edition 构建 Web 应用程序时,您可以将 Servlet API 和相关 Java EE API 的依赖项设置为提供的范围,因为 Web 容器提供这些类。此范围仅在编译和测试类路径上可用,并且不可传递。

from the mavendocs. You're basically telling Maven to provided the jar for compilation, but, at runtime, use some other, the servlet container's, jar.

来自maven文档。您基本上是在告诉 Maven 提供用于编译的 jar,但是在运行时,使用其他一些 servlet 容器的 jar。

回答by Rajarajan Pudupatti Sundari Je

In your case, ClassCastException seems to be because of classes being loaded by different classloaders. Say, you have the servlet-api.jar included by mistake in your WEB-INF/lib and you had set PARENT_LAST true for your webapp class loader. If you are running on an application server like WAS, this would mean you are loading interface with one class loader (some class loader at the top) and implementation (servlet-api jar just an e.g.) with another class loader at the bottom.

在您的情况下, ClassCastException 似乎是因为不同的类加载器加载了类。假设您错误地将 servlet-api.jar 包含在您的 WEB-INF/lib 中,并且您已将 PARENT_LAST 设置为您的 webapp 类加载器。如果您在像 WAS 这样的应用程序服务器上运行,这意味着您正在使用一个类加载器(顶部的某个类加载器)加载接口,并使用底部的另一个类加载器实现(servlet-api jar 只是一个例子)。

回答by Philip Puthenvila

Which application server you are using? I think Servlet jars files should be provide by your application srver.

您使用的是哪个应用程序服务器?我认为 Servlet jars 文件应该由您的应用程序服务器提供。

回答by User 4.5.5

Keep scope as 'provided' and try keeping that jar in tomcat/lib folders. This is because of each class loader of respective war tries to load its own Servlet-api classes separately,compared to application class loader which loads the Servlet-api classes at container level.

保持范围为“提供”,并尝试将该 jar 保存在 tomcat/lib 文件夹中。这是因为与在容器级别加载 Servlet-api 类的应用程序类加载器相比,各个 war 的每个类加载器都尝试单独加载自己的 Servlet-api 类。

So if u can make the jar moved to CATALINA_HOME/lib.you have required set of classes loaded by application class loader only and same version will be referred across by all wars.

因此,如果您可以将 jar 移动到 CATALINA_HOME/lib。您只需要应用程序类加载器加载的一组类,并且所有War都将引用相同的版本。

scope 'provided' conveys to class loader at war level that the classes required for this is already loaded by application class loader and insists class loader at its war level may not required to create separate instances or version for it that cause class cast exception

范围“提供”传达给War级别的类加载器,应用程序类加载器已经加载了为此所需的类,并坚持认为War级别的类加载器可能不需要为其创建单独的实例或版本,从而导致类转换异常