7

I have a similar problem to the one here DispatcherServlet cannot be cast to Servlet which is when running a new Spring MVC project using Eclipse. However, all the answers suggest a solution using Maven and I'm not using Maven.

I searched for a day for this problem but no solution.

INFO: Starting Servlet engine: [Apache Tomcat/10.0.4]
Mar 22, 2021 9:59:10 AM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
WARNING: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took 
[447] milliseconds.

Mar 22, 2021 9:59:10 AM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for 
this logger for a complete list of JARs that were scanned but no TLDs were found in them. 
Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.


 Mar 22, 2021 9:59:11 AM org.apache.catalina.core.ApplicationContext log
 INFO: Marking servlet [dispatcher] as unavailable

 Mar 22, 2021 9:59:11 AM org.apache.catalina.core.StandardContext loadOnStartup
 SEVERE: Servlet [dispatcher] in web application [/spring-mvc-demo] threw load() exception
 java.lang.ClassCastException: class org.springframework.web.servlet.DispatcherServlet cannot 
 be cast to class jakarta.servlet.Servlet (org.springframework.web.servlet.DispatcherServlet 
 is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader 
 @62e70ea3; jakarta.servlet.Servlet is in unnamed module of loader java.net.URLClassLoader 
 @8bd1b6a)
 
  at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1043)
  at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:984)
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
7HROSHI
  • 95
  • 1
  • 1
  • 5
  • 2
    Looks like you’re running Tomcat 10 with a version of Spring that doesn’t support Tomcat 10. All the JavaEE classes were renamed when it became JakartaEE. It looks like no version of Spring yet supports the new namespace - https://github.com/spring-projects/spring-framework/issues/25354. Use Tomcat 9. – Boris the Spider Mar 22 '21 at 07:17
  • I use Tomcat 10, Spring 5.29 and Eclipse Enterprise 2021-03 (4.19.0) – 7HROSHI Mar 22 '21 at 07:20
  • So do you suggest downgrading Spring or Tomcat? – 7HROSHI Mar 22 '21 at 07:21
  • and by the way I'm using a code from 2017 – 7HROSHI Mar 22 '21 at 07:22
  • 1
    How would downgrading Spring help? Use Tomcat 9. – Boris the Spider Mar 22 '21 at 07:22
  • sorry I don't use Maven – 7HROSHI Mar 22 '21 at 07:23
  • 2
    Spring only supports, at the moment, JavaEE. You are using Tomcat10 which is using JakartaEE and thus isn't going to work. YOu will need to use tomcat9 (which is JavaEE and not JakartaEE). – M. Deinum Mar 22 '21 at 07:26
  • 1
    Does this answer your question? [Servlet 5.0 JAR throws compile error on javax.servlet.\* but Servlet 4.0 JAR does not](https://stackoverflow.com/questions/64387472/servlet-5-0-jar-throws-compile-error-on-javax-servlet-but-servlet-4-0-jar-does) – Piotr P. Karwasz Mar 22 '21 at 07:47

1 Answers1

21

Spring (and Spring Boot) currently only support JavaEE and not JakartaEE. Which means that it will only run on/with JavaEE compatible servers. Tomcat10 is an implementation of the JakartaEE specification which currently isn't supported.

The only solution is to downgrade to Tomcat 9.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224