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-
- 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>
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>
- 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
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>
- 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>
- 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
CodePudding user response:
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:
- Upgrade Tomcat to a minimum of 10.
- 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.