mavenservletshttp-status-code-404java-11tomcat10

Servlets problem with accessing subpages HTTP 404 Tomcat 10.1 java 11


I have just started to learn servlet technology, and I'm currently trying to create a very basic web application. Just some pages, trying this thing. I can reach the start page /index.html but I get 404 HTTP status code for other part:

Message: The requested resource [/first] is not available

Description: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

My web.xml under WEB-INF is this:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="4.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
</web-app>

My FirstServlet.java is this:

package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/first")
public class FirstServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        pw.println("<html>");
        pw.println("<body>");
        pw.println("Welcome to servlet");
        pw.println("</body>");
        pw.println("</html>");
        pw.close();
    }
}

The context path is /. I tried urlPatterns at WebServlet annotation. I'm trying it under IntelliJ IDEA Community, with Smart Tomcat plugin, with Tomcat 10.1.24 installed, and with Java 11. I ran mvn clean package.

After starting the application I can reach index.html with http://localhost:8080/ but I can't reach http://localhost:8080/first .

I'm trying to find the solution for some time but I wasn't able to do that. I read through the threads here (at least tried), too, but didn't find it. From a Dockerfile image I tried it with Tomcat 9.0.89 but I get the same / 404 http status code.

I would like to ask for your help in this question. If I can provide any useful additional relevant information, please let me know.

Thank you very much for your help in advance.


Solution

  • This code is very old servlet technology and it doesn't work with Tomcat 10.x. You can use it with Tomcat 9 or lower version.