Spring MVC - Getting 404 error and not able to configure Dispatcher Servlet
I am new to Spring MVC and trying to create my first project in Spring MVC. Please help me to resolve the following error: -
Getting following error in console: -
Allocate exception for servlet [dispatcher]
java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet
Browser showing 404 error for the URL - http://localhost:9492/MvcProject/home
web.xml
<!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>
<!-- Configure Dispatcher Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern> <!-- handles all requests starting with '/' -->
</servlet-mapping>
</web-app>
dispatcher-servlet.xml
<?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"
xmlns:p="http://www.springframework.org/schema/p"
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="mvc.controller"/>
<context:annotation-config/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" name="viewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Controller Class
package mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/home")
public String home() {
System.out.println("This is home page");
return "index1";
}
}
index1.jsp
<%@ 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 our home page - index1</h1>
</body>
</html>
Eclipse Project hierarchy
Try to add javax.servlet-api.jar to your WEB-INF/lib directory. Maybe it is not in tomcat's lib folder. To download the jar go to
https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api/3.1.0
You have to add javax.servlet-api dependency in the pom.xml file. After adding, apply Maven update and it will download the jar file.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
Allocate exception for servlet [dispatcher] java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet
Means your application is trying to use the class javax.servlet.http.HttpServlet
but it can't find in your POJO. You have to add HttpServlet
dependency in your project.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>