1

I am creating a web application using Spring MVC and Eclipse IDE. Spring Version- 6.0.3
To configure the project, I followed the following steps-

  1. Added dependencies in pom.xml-
 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!-- Spring MVC Dependency -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>6.0.3</version>
        </dependency>
  </dependencies>
  1. Added Tomcat Server Runtime to the build path- enter image description here

  2. Added maven dependencies to deployment assembly- enter image description here

  3. web.xml- (in WEB-INF folder)

<web-app>
    <display-name>Spring MVC Demo</display-name>

    <!-- Configure dispatcher servlet -->
    <servlet>
        <servlet-name>dispatcherservlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherservlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- / means handle all the requests from all urls -->
</web-app>
  1. dispatcherservlet-servlet.xml (in WEB-INF folder)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context     
    http://www.springframework.org/schema/context/spring-context.xsd"

    xmlns:p="http://springframework.org/schema/p">
<!-- Enable annotations -->
<context:component-scan base-package="spring-mvc-demo.src.main.java.controller"></context:component-scan>
    <!-- View Resolver bean -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        name="viewResolver">
        <!-- Inject two properties -->
        <!-- Location for pages is given to prefix -->
        <property name="prefix" value="/WEB-INF/views/" />
        <!-- ending of page is .jsp -->
        <property name="suffix" value=".jsp" />
        <!-- Example name /WEB-INF/views/hello.jsp (here the name hello will be 
            given by controller) -->
    </bean>

</beans>


  1. Placed index.jsp under views folder in WEB-INF-
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="ISO-8859-1">
        <title>Home Page</title>
    </head>
    <body>
        <h1>This is home page</h1>
        <h1>Called by home controller</h1>
        <h1>fired for /</h1>
    </body>
</html>
  1. Created HomeController.java class in src/main/java/controller
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/home")
public class HomeController {
    
    @GetMapping("/current")
    public String home() {
        //return the name of the page
        System.out.println("Hello this is home URL");
        return "index";
    }
}

I have created a controller with the url /home/current. On visiting this url I expect to see the desired index.jsp.
Problem- When I "Run on Server" I get following error-

SEVERE: Allocate exception for servlet [dispatcherservlet]
java.lang.ClassNotFoundException: jakarta.servlet.http.HttpServlet
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1412)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1220)
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)

Please help me to find the error in my configuration and the reason I get this error. I saw several other posts, re-checked my steps but still getting the same error- java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ayush
  • 464
  • 5
  • 17

2 Answers2

5

Based on the first screenshot in your question, you're using Tomcat 9. But for Spring 6 you need a minimum of Tomcat 10. Tomcat 10 is the first to use jakarta.* namespace whereas older versions used javax.* namespace.

Spring 6 (and Spring Boot 3) is the first version to use jakarta.* namespace whereas older versions used javax.* namespace.

So you have 2 options:

  1. Upgrade Tomcat to a minimum of 10.
  2. Or, downgrade Spring to a maximum of 5.

Clearly, option 1 is the recommended way to go in long term.

By the way, Spring 6 requires Java 17 not Java 1.7 as seen in the first screenshot in your question.

See also:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I changed my tomcat version to 10.0. Now, I am getting a different error- Jan 22, 2023 11:24:29 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound WARNING: No mapping for GET /spring-mvc-demo/ Jan 22, 2023 11:24:34 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound WARNING: No mapping for GET /spring-mvc-demo/home/current – ayush Jan 22 '23 at 17:57
  • 1
    Yup. That's a different problem. Continue here https://stackoverflow.com/q/41577234 or ask a new question. – BalusC Jan 22 '23 at 18:13
  • Thank You. I resolved it, had a problem with component scanning. – ayush Jan 22 '23 at 18:24
  • Although my project is working, but I am still getting a error in the WEB-INF/views/index.jsp (highlighted in red as an error)- Multiple annotations found at this line: - The superclass "javax.servlet.http.HttpServlet", determined from the Dynamic Web Module facet version (3.1), was not found on the Java Build Path - The superclass "javax.servlet.http.HttpServlet", determined from the Dynamic Web Module facet version (3.1), was not found on the Java Build Path – ayush Jan 23 '23 at 11:56
  • That is a false negative. Technically speaking, Eclipse needs to be updated to the version which recognizes and supports `jakarta.*`. This is unrelated to the problem asked in the current question. – BalusC Jan 23 '23 at 12:36
  • You need to add the namespace/schema location to web-app tag in your deployment descriptor (ie., web.xml) to get rid errors in your JSP files, also update the Dynamic Web Module facet version to v5.0. Refer to this link for more detailed answer https://www.eclipse.org/forums/index.php/t/1112198/ – Vijay Kumar Mar 04 '23 at 17:54
0

While working with spring 6.0 and jakartgha servelet api(6.0.0) and jsp api (3.1.1) we have use tomcat 10.1 or 10.0 at least

General Grievance
  • 4,555
  • 31
  • 31
  • 45