1

I am creating a basic demo project for servlet but I keep getting this error whenever I run the project or index.html file. I have mentioned the servlet configurations in the web.xml. Also, I have added servlet jar files into my build path.

I cannot figure out why I am getting this error. I tried many solutions on the internet but none seems to be working.

Here are files. and the directory structure is this.

package Main;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Login extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    Login(){
        super();
    }

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException 
    {
        
        System.out.println("servlet called");
        
        
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
  <display-name>FirstServlet</display-name>
  
  <servlet>
   <servlet-name>Login</servlet-name>
   <servlet-class>Main.Login</servlet-class>
</servlet>

<servlet-mapping>
   <servlet-name>Login</servlet-name>
   <url-pattern>/Login</url-pattern>
</servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
<!DOCTYPE html>
<html>

<body>
    <form action="Login" method="get">
        <input type="text" name="username"/><br>
         <input type="text" name="password"/>
         <input type="submit" value="Login"/>
    </form>
</body>
</html>
Olivier
  • 13,283
  • 1
  • 8
  • 24
amstudent
  • 11
  • 1

1 Answers1

0

This is a common issue on Tomcat 10 because the change from Java EE to Jakarta EE, so you can change your project, using Jakarta EE 9

    <dependency>
        <groupId>jakarta.platform</groupId>
        <artifactId>jakarta.jakartaee-api</artifactId>
        <version>9.0.0</version>
        <scope>provided</scope>
    </dependency>

and importing this packages

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

If you don´t want to do that, you can downgrade to Tomcat 9.

For related documentation follow this link

Tomcat 10.0.4 doesn't load servlets (@WebServlet classes) with 404 error

CaptainPyscho
  • 338
  • 1
  • 7