ServletDispatcher cannot be cast to Javax.servlet.Servlet exception in my spring project

前端 未结 2 1619
-上瘾入骨i
-上瘾入骨i 2020-12-06 05:36

While starting tomcat server I am getting an exception

SEVERE: Servlet /MavenWeb threw load() exception
java.lang.ClassCastException: org.springframework.we         


        
相关标签:
2条回答
  • 2020-12-06 05:50

    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>

    0 讨论(0)
  • 2020-12-06 06:11

    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.

    0 讨论(0)
提交回复
热议问题