0

I am trying to create a web application, and want to open up a java servlet when an HTML form is submitted. The servlet is continually giving errors (mostly 404 errors), and I have tried every solution I have seen, but none of them work.

package com.example.kahootwebapp;

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

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

@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter writer = response.getWriter();

        writer.println("<html><body>");
        writer.println("<h1>This is a test</h1>");
        writer.println("</html></body>");

    }

}
<form name="HelloServlet" method="post" action="HelloServlet">
   <label id="label" for="input">Enter kahoot account email: </label><input type="text" id="input" name="username"/>
</form>

Code organization

I am running the server on Apache Tomcat, version 10.0.16. At current time, I am just trying to get the servlet to open and run without error, and then planning on adding code to it. Thanks.

Malware
  • 1
  • 1

2 Answers2

0

Jakarta EE transition

Your Servlet code is using javax.* package name. That works only on Tomcat 9 and earlier. Deploying such a Servlet to Tomcat 10+ will fail.

Tomcat 10+ uses the jakarta.* packages.

This package name change is related to the transfer of Java EE technology from Oracle to the Eclipse Foundation. There the technology is being renamed to Jakarta EE. A quick search on the web will provide many articles and videos about the transition.

You need to choose between using the old-school naming or the modern naming. For larger complex projects, you may want to continue old-school while deferring the transition to a later time. If starting a new project, I recommend considering the modern naming. But research if the important libraries you need have been released in an edition using the new naming.

If you choose the modern naming, adjust your POM to match. You are writing a Servlet class that extends from a class in the Servlet API. So your IDE and the compiler needs a copy of that Servlet API in order to compile. Be sure to mark your POM with a scope of provided so as to not bundle a copy within your final Servlet artifact (likely a WAR file). Your Servlet container such as Tomcat or Jetty come with their own copy of the Servlet API. See how to add the servlet api to my pom.xml, but adjust those answers to use the appropriate Jakarta naming. I added an Answer there with that adjustment.

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

Tomcat versions

Know that Tomcat 9 and 10.0.* are equivalent products, developed in parallel. Expect the same features and performance. The package naming is the main difference. These two versions of Tomcat implement the Servlet spec versions 4.3 and 5, respectively. Servlet 5 spec is identical to 4.3 except for the legal name changes from Oracle and Java to Eclipse Foundation and Jakarta.

Tomcat 10.1.x is currently in development to support Jakarta Servlet 6 spec. Tomcat 10.1.x is available now as an alpha pre-release. The Jakarta Servlet 6 specification will be finalized later this year 2022.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • When I change my imports from `javax.*` to `jakarta.*`, I just get errors on the entire servlet code, and suggestions to "Find JAR on Web". Is there something I need to download to implement `jakarta.*` vs. implementing `javax.*`? – Malware Feb 19 '22 at 16:18
  • Is there something I also need to change in my pom file? – Malware Feb 19 '22 at 16:25
  • @Malware Yes, you must adjust your POM to match. You are writing a Servlet class that extends from a class in the Servlet API. So your IDE and the compiler needs a copy of that Servlet API in order to compile. Be sure to mark your POM with a `scope` of `provided` so as to *not* bundle a copy within your final Servlet artifact (likely a WAR file). Your Servlet container such as Tomcat or Jetty come with their own copy of the Servlet API. See [*how to add the servlet api to my pom.xml*](https://stackoverflow.com/q/1370414/642706), but adjust those answers to use the appropriate Jakarta naming. – Basil Bourque Feb 19 '22 at 16:45
0

I searched it a lot but finally noticed that the Basil Bourque's explanation is the correct one and since you are using java jakarta you require to use tomcat 10+. Thats All! :)

Med
  • 1
  • 1
  • Please don't add "thank you" as an answer. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation), you will be able to [vote up questions and answers](https://stackoverflow.com/help/privileges/vote-up) that you found helpful. - [From Review](/review/late-answers/33091941) – Aaron Meese Nov 08 '22 at 01:32