Reputation: 582
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:
Upvotes: 2
Views: 1110
Reputation: 75
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
Upvotes: 4