2

I generated a standard project of servlet in IntelliJ IDEA.

Problem:

When I open localhost:8080/javaee in the browser it works.

enter image description here

But when I try to open servlet page: localhost:8080/javaee/hello-servlet I get 404 error.

enter image description here

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:

enter image description here

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:

enter image description here

enter image description here

14
  • Why are you including the /javaee/? Commented Aug 18, 2021 at 4:14
  • 2
    You are using Tomcat 9 which is aimed at the javax.* package naming of Java EE. But you are specifying the Jakarta Servlet 5 API in your POM, which is aimed at the jakarata.* 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?. Commented Aug 18, 2021 at 4:29
  • 2
    Or, vice-versa, change from Tomcat 9 to Tomcat 10. The two products are equivalent, in parallel development during this time of transition. Commented Aug 18, 2021 at 4:34
  • 1
    Jetty is doing the same as Tomcat: two versions in parallel development, one version number for the mainstream but legacy javax.* package naming with Java EE, and one later version number for the cutting-edge jakarta.* package naming of Jakarta EE. As with Tomcat, the two versions are functionally equivalent, same features & performance. So, your choice: (a) mainstream javax.* with lower version number of Tomcat or Jetty, or (b) cutting-edge jakarta.* with higher number but functionally-equivalent version of Tomcat or Jetty. Commented Aug 18, 2021 at 4:53
  • 1
    Since your POM says you want to use Java 11, I suggest you use Jakarta EE 9.1 which is the same as Jakarta EE 9.0 but guaranteeing support for Java 11 beyond the previous limit of Java 8. We have all kinds of funky no-new-feature functionally-equivalent version releases during this transition time! The future Jakarta 10 will be the release aimed at new features and innovation, marking the end of this transition period. Commented Aug 18, 2021 at 5:03

1 Answer 1

4

You either need to update your tomcat version to 10 or downgrade your servlet api to use javax.servlet-api instead of jakarta.servlet-api (what's specified in your current pom.xml file)

Up until tomcat version 10, tomcat supported javax as the servlet API but after tomcat 10 they only support jakarta.

Migration docs: https://tomcat.apache.org/migration-10.html#Migrating_from_9.0.x_to_10.0.x

Let me answer in a pseudo code cuz why not xD,

if (tomcat.version >= 10) use jakarta.servlet-api else use javax.servlet-api 
Sign up to request clarification or add additional context in comments.

1 Comment

The issue was. the version of tomcat which was using tomcat 9. i just download tomcat 11. it works fine

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.