16

While starting tomcat server I am getting an exception

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

I am using spring3 but there is jar spring2-5-6 in my lib folder, I removed it from pom.xml but still appears in lib folder - though I am not sure if that is an issue. I am using Eclipse IDE. Thanks!!

<dependencies>
    <dependency>
      <groupId>org.hibernate.javax.persistence</groupId>
      <artifactId>hibernate-jpa-2.1-api</artifactId>
      <version>1.0.0.Draft-6</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-annotations</artifactId>
      <version>3.5.6-Final</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>3.1.2.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>3.1.2.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>3.1.2.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>3.1.2.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>3.1.2.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1-b01</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webflow</artifactId>
      <version>1.0.6</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
Manth
  • 358
  • 3
  • 4
  • 14
  • In my case, i removed javax.servlet entry from pom.xml and rather added the tomcat library in my classpath. – JavaTec Feb 15 '16 at 17:49

2 Answers2

29

You shouldn't be using multiple versions of Spring JARs in one project, but this is not the issue.

The problem is most likely caused by servlet API classes loaded by two different class-loaders. Probably you have servlet*.jar or some other container-specific JARs in your WAR. Remove them by setting their <scope> to provided in pom.xml.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • I just have javax.servlet-api for servlet in my pom.xml. rest all are org.springframework's artifacts for spring-webmvc, spring-webflow, spring-tx, spring-web, spring-beans and hibernate-annotations. I posted dependacies from my pom.xml above. thankks. – Manth Jul 28 '12 at 22:38
  • 1
    @Manth: as I said, just change the scope of `javax.servlet-api` to `provided`. Did it help? – Tomasz Nurkiewicz Jul 29 '12 at 08:11
  • 1
    Nurkiewiczit: didn't work. I did change the javax.servlet-api to provided as you said. After compiling i still see the javax.servlet-api-3.1-b01.jar in my target/web-inf/lib folder. – Manth Jul 30 '12 at 22:58
  • Care to explain as to why not use multiple versions of spring jars? I agree that it is a very good idea to prevent such situations but if you want to use websockets and spring security you must use spring4 and the spring security package uses spring 3.2 – Cu7l4ss Apr 10 '14 at 14:03
  • In my case I had to make my tomcat-api scope provided – pawelccb Aug 05 '14 at 06:49
  • I had added `servlet-api` dependency to my POM to prevent a bunch of warnings in Eclipse, but I forgot to specify the scope. When I changed it to `provided`, the error at runtime went away. Thanks! – Jim Tough Jun 23 '16 at 12:33
  • In a weird way, I deleted the javax.servlet-api dependency and worked. I don't understand, because in other computer works with it as 'provided'. – Alfonso Nishikawa Jul 11 '16 at 22:05
0

In my case it wasn't a problem with the libraries. I was changing a Standard Servlet to be implemented with Spring, so I followed these instructions, that I paraphrase here just in case the page goes down later:

  1. Implement org.springframework.web.HttpRequestHandler instead of extending javax.Servlet

    public class MyServlet implements HttpRequestHandler {

  2. Created the bean in the applicationContext.xml (I did it in the dispatcher-servlet.xml)

    <bean id="MyServlet" class="com.package.to.MyServlet"/>

  3. Specify the servlet in the Web.xml, changing the old class (com.package.to.MyServlet) to Spring HttpRequestHandlerServlet.

    <servlet> <servlet-name>MyServlet</servlet-name> <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/MyServlet</url-pattern> </servlet-mapping>

I had to do an additional step to avoid a FileNotFoundException about applicationContext.xml doing the following in the web.xml

<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

Christian Vielma
  • 15,263
  • 12
  • 53
  • 60