1

I'm a beginner in Servlets and Tomcat, and I'm making a simple Hello World program

I have read lots of posts about this and I am still clueless as to why my approach to Tomcat doesn't work. Here's the full directory / project of mine:

WebTest:

package classes;

import java.io.IOException;
import java.io.PrintWriter;

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

@WebServlet(name = "WebTest", urlPatterns = {"/fing"})
public class WebTest extends HttpServlet  {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            PrintWriter out = response.getWriter();

            out.println("<html>");
            out.println("<head>");
            out.println("<title> Home Page </title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1> Hello there </h1>");
            out.println("<html>");
            out.println("</html>");

    
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

index.html:

<!DOCTYPE html>
<html lang="en">

    <head>
        <title> Home Page </title>
    </head>

    <body>
        <h1>Hello world</h1>
        <form  action= "./fing" method = "post" >
            <label>Enter name:</label> <input type="text" name = "user">
            <input type = "submit" value="Click Here">
        </form>
    </body>

</html>

web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  
</web-app>

Project Set up:

enter image description here

The error I'm getting: enter image description here

Note: I'm using Tomcat 10.0.8 and jakarta.servlet 6.0.0

I tried to change the web.xml file instead of inserting the Annotation but the issue still persists. I expect the web to have a different text on it

Shadow504
  • 33
  • 1
  • 5

1 Answers1

0

The main issue is the web.xml it follows an old version of tomcat and is incompatible. Try to use this snippet:

<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
                      https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
         version="5.0">

  <display-name>Archetype Created Web Application</display-name>
  <request-character-encoding>UTF-8</request-character-encoding>
  <response-character-encoding>UTF-8</response-character-encoding>
</web-app>

For the correct version check your servlet api version in the pom.xml:

<dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId>
    <version>5.0.0</version>
</dependency>

As the servlet is reacting on POST requests you can curl:

 curl -d "param1=value1" -X POST http://localhost:8080/webtest/fing 

Alternatively add a doGet method and call it from browser. See more info here and here

luckyluke
  • 491
  • 7
  • 16
  • I tried exactly what you have specified, I can confirm I have the correct version of the servlet api, the issue still persists however (Tomcat still cannot find the servlet) – Shadow504 Nov 21 '22 at 00:36