0

After creating a project in intelij and starting Tomcat Server I successfully get my index.jsp page.

But when I try to open HelloServlet.java I get: "Error instantiating servlet class" error.

And then when I reload this page I get the 404 error: "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists."

This code definitely works on another PC with Tomcat, but doesn't on mine.

HelloServlet.java

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

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() {
    }
} 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
         version="4.0">

    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>HelloServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/HelloServlet</url-pattern>
    </servlet-mapping>
</web-app>

index.jsp

<%@ 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="HelloServlet">Hello Servlet</a>
  </body>
</html>

Project Structure

|____resources
|____webapp
| |____index.jsp
| |____WEB-INF
| | |____web.xml
|____java
| |____HelloServlet.java

What should I do to fix my problem?

1 Answers1

0

I fixed my problem installing Tomcat 8.5.70

I don't know what was wrong with Tomcat 10, but 8.5.70 works fine with the same project.


UPD:

The problem was in imports. Tomcat 10 uses the jakarta.* package rather than javax.*.

Thanks every one for your advises.

  • 1
    Nothing is “wrong” with Tomcat 10. Tomcat 10 uses the `jakarta.*` package rather than `javax.*`. In all other regards, Tomcat 10 is the same as Tomcat 9, both in parallel development, same features, same performance. Read [*Tomcat Versions*](http://tomcat.apache.org/whichversion.html), and [*Transition from Java EE to Jakarta EE*](https://blogs.oracle.com/javamagazine/post/transition-from-java-ee-to-jakarta-ee) by Arjan Tijms, and [many other articles](https://duckduckgo.com/?q=jakarta+javax.*+jakarta.*+package+name+change+ee&t=iphone&ia=web). – Basil Bourque Sep 21 '21 at 16:51
  • Remark that _"`javax.servlet.*` doesn't work anymore in Servlet 5.0 or newer"_ is the fourth paragraph of [BalusC's answer](https://stackoverflow.com/a/11731512/11748454) to the question I already cited. – Piotr P. Karwasz Sep 22 '21 at 05:35