Issue
I'm trying to solve this error for the last 3 weeks. I tried some solutions from StackOverflow but they didn't help me also tried watching youtube videos but the result is still the same.
This is my Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springMVC</groupId>
<artifactId>com.springMVC</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>com.springMVC Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.21.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>com.springMVC</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
This is my web.xml code
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--Configuring DispatcherServlet-->
<servlet>
<servlet-name>ser</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ser</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
This is my Servlet code
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="Controller" />
<!-- <context:annotation-config />-->
<!-- Configuring View Resolver-->
<bean name="viewRes" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
This is my Controller code
package Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class AllControllers {
@RequestMapping("/home")
public String index(){
return "index";
}
@RequestMapping("/about")
public String about(){
return "about";
}
}
And here are my index.jsp and about.jsp Code
about.jsp
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>about</title>
</head>
<body>
<h1>ABOUT</h1>
</body>
</html>
index.jsp
<html>
<body>
<h2>Hello World!</h2>
<h2>Hello Sanan!</h2>
<h2>Hello Shaikh!</h2>
</body>
</html>
I am stuck on this error from the last three weeks
Solution
You've been using the Tomcat 10.x
. and this version of tomcat is using Servlet API version 5.0. The Servlet API 5 is part of Jakarta EE version 9. The javax.*
package has been renamed to jakarta.*
package since Jakarta EE version 9. Therefore, you might be getting the following issue while starting the tomcat server.
java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet
You can either downgrade the Tomcat version to 9.x or you can also modify the context.xml
file of tomcat server 10 as mentioned below.
<Context>
...
<Loader jakartaConverter="TOMCAT" />
</Context>
Restart the server and you can access the resources by hitting the following endpoints.
http://localhost:8080/com.springMVC/home
http://localhost:8080/com.springMVC/about
Answered By - harry
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)