- ⚠️ Tomcat 11 enforces stricter servlet specifications, requiring adjustments for older Spring MVC applications.
- 🔍 A 404 error in Spring MVC often results from incorrect servlet mappings, missing dependencies, or deployment misconfigurations.
- 🛠️ Proper
web.xmlandDispatcherServletconfigurations are crucial for handling requests correctly. - 📂 Ensuring the correct WAR deployment structure inside Tomcat’s
webapps/directory prevents loading issues. - 🏗️ Using modern Spring MVC versions compatible with Servlet 5.0+ can mitigate incompatibility problems.
Why Am I Getting a 404 Error in Spring MVC on Tomcat 11?
Spring MVC is one of the most widely used frameworks for building Java web applications, and Apache Tomcat remains a top choice as a deployment server. However, if you are running a Spring MVC application on Tomcat 11 and encountering a 404 error, you’re not alone. This guide explores common causes and provides practical solutions, covering configuration issues, dependency mismatches, and deployment mistakes that prevent your application from reaching the intended resources.
Understanding the 404 Error in Spring MVC on Tomcat
A 404 error signifies that the server could not find the requested resource. In a Spring MVC application deployed on Tomcat 11, common reasons include:
- Faulty servlet mappings or missing
web.xmlconfigurations. - Incorrect
DispatcherServletsetup, which prevents proper request routing. - Problems with WAR file placement or application deployment in Tomcat.
- Incompatible servlet versions between the Spring MVC version and Tomcat 11.
By systematically analyzing these potential causes, you can resolve issues quickly and get your application running correctly.
How Tomcat 11 Handles Spring MVC Applications
Tomcat 11 comes with significant updates that impact how applications are deployed and executed. Developers upgrading from older versions must be aware of the following changes:
🔹 Stricter Servlet API Enforcement
Tomcat 11 adopts Jakarta EE 10, which introduces breaking changes from previous Java EE specifications, requiring adjustments in Spring MVC applications relying on older servlet features.
🔹 Deprecation of Legacy Servlet Behavior
Some older methods in web.xml configurations and servlet mappings may no longer be compatible without modifications.
🔹 Tighter Security Policies
New security policies might block improperly configured applications from running smoothly, requiring explicit permissions in deployment descriptors.
Ensuring that your application complies with Tomcat 11’s new rules will prevent deployment failures and unexpected behavior.
Common Causes of 404 Errors in Spring MVC on Tomcat
1. Incorrect web.xml Configuration
The web.xml file defines servlet mappings and request routing. Incorrect configurations can prevent Tomcat from locating the dispatch servlet. Ensure your web.xml follows this template:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
version="3.0">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Common mistakes:
❌ Misspelling DispatcherServlet in the servlet-class.
❌ Using an incorrect url-pattern that does not match incoming requests.
2. Issue with WAR Deployment in Tomcat
For Spring MVC applications to run, Tomcat must correctly deploy the WAR file. Verify the following:
✅ The myapp.war file exists inside the webapps/ directory.
✅ Accessing http://localhost:8080/myapp/ does not return a 404.
✅ Log files (catalina.out) do not show deployment errors.
You can manually trigger a reload using:
touch webapps/myapp.war
This forces Tomcat to redeploy the application.
3. Misconfigured Context Path
Your application's context.xml file defines its web path. If it's misconfigured, Tomcat won’t recognize the application URL:
<Context path="/myapp"/>
Verify that the path matches how you intend to access the application (e.g., http://localhost:8080/myapp/).
4. Missing Dependencies in WEB-INF/lib
Spring MVC requires several JAR files to function correctly. Ensure the following are present in WEB-INF/lib:
spring-webmvc.jarspring-core.jarspring-context.jarjavax.servlet-api.jar
If using Maven, include:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.23</version>
</dependency>
</dependencies>
Tip: Run mvn clean package to verify that dependencies are bundled in the WAR.
5. Incompatibilities Between Spring MVC and Tomcat 11
Tomcat 11 requires at least Servlet 5.0, while older Spring MVC versions may not be compatible. Ensure compatibility by using:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>5.0.0</version>
</dependency>
6. Incorrectly Mapped Controllers in Spring MVC
Spring MVC controllers must be correctly mapped to return expected views. A valid HomeController:
@Controller
@RequestMapping("/home")
public class HomeController {
@GetMapping
public String home() {
return "index";
}
}
Common issues:
❌ The @RequestMapping path does not align with requested URLs.
❌ No valid view resolver exists to interpret "index".
Step-by-Step Troubleshooting Guide
✅ Step 1: Check Tomcat’s Status
Ensure Tomcat is running:
systemctl status tomcat
If stopped, restart:
systemctl start tomcat
✅ Step 2: Validate Deployment Structure
Spring MVC projects must follow this format:
webapps/
├── myapp/
│ ├── WEB-INF/
│ │ ├── web.xml
│ │ ├── classes/
│ │ ├── lib/
│ │ ├── views/
If WEB-INF is missing, Tomcat won't serve the application.
✅ Step 3: Check Tomcat’s Logs
Inspect deployment logs:
tail -f logs/catalina.out
Look for missing dependency or servlet mapping errors.
✅ Step 4: Test with curl or Postman
Check endpoint responses:
curl -v http://localhost:8080/myapp/home
If you receive 404, debug controller mappings and servlet configurations.
✅ Step 5: Enable Debugging Logs
Modify log4j.properties:
log4j.logger.org.springframework=DEBUG
Logs offer detailed insights into request handling.
Best Practices for Running Spring MVC on Tomcat 11
🎯 Use a modern IDE – IntelliJ IDEA, Eclipse, or NetBeans simplify deployment.
🚀 Automate deployments with CI/CD – Prevents misconfigurations in WAR packaging.
🐞 Enable verbose logging – Helps diagnose hidden issues.
🔄 Regularly update dependencies – Avoid outdated Spring MVC versions.
By following these best practices, you can ensure a robust and error-free deployment on Tomcat 11.
Citations
- Oracle. (2023). Java EE and Servlet API Updates. Retrieved from Oracle Docs
- Apache Tomcat. (2023). Tomcat 11 Migration Guide. Retrieved from Tomcat Docs
For further help, refer to the official Spring MVC documentation.