Home > Blockchain >  Tomcat - Error instantiating servlet class - intelij
Tomcat - Error instantiating servlet class - intelij

Time:09-26

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?

CodePudding user response:

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.

  • Related