I generated a standard project of servlet in IntelliJ IDEA.
Problem:
When I open localhost:8080/javaee in the browser it works.
But when I try to open servlet page: localhost:8080/javaee/hello-servlet I get 404 error.
Project:
I use Maven, here is 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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>java-ee</artifactId> <version>1.0-SNAPSHOT</version> <name>java-ee</name> <packaging>war</packaging> <properties> <maven.compiler.target>11</maven.compiler.target> <maven.compiler.source>11</maven.compiler.source> <junit.version>5.7.1</junit.version> </properties> <dependencies> <dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <version>5.0.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.1</version> </plugin> </plugins> </build> </project> Here is the structure of the project:
index.jsp has next content:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html> <html> <head> <title>JSP - Hello World</title> </head> <body> <h1><%= "Hello World!" %> </h1> <br/> <a href="hello-servlet">Hello Servlet</a> </body> </html> HelloServlet.java
package com.example; import java.io.*; import jakarta.servlet.http.*; import jakarta.servlet.annotation.*; @WebServlet(name = "helloServlet", value = "/hello-servlet") public class HelloServlet extends HttpServlet { private String message; public void init() { message = "Hello World!"; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html"); // Hello PrintWriter out = response.getWriter(); out.println("<html><body>"); out.println("<h1>" + message + "</h1>"); out.println("</body></html>"); } public void destroy() { } } And here is my configuration for a running of Tomcat server from Intellij Idea:





/javaee/?javax.*package naming of Java EE. But you are specifying the Jakarta Servlet 5 API in your POM, which is aimed at thejakarata.*package naming of Jakarta EE, and works with Tomcat 10 (not 9). Are you aware of the package-naming transition as part of the transfer of Java EE by Oracle to Jakarta EE by Eclipse Foundation? See Tomcat page: Which version?.javax.*package naming with Java EE, and one later version number for the cutting-edgejakarta.*package naming of Jakarta EE. As with Tomcat, the two versions are functionally equivalent, same features & performance. So, your choice: (a) mainstreamjavax.*with lower version number of Tomcat or Jetty, or (b) cutting-edgejakarta.*with higher number but functionally-equivalent version of Tomcat or Jetty.