Search code examples
javamaventomcatservlets

Java Servlet does not recognize my route/class


I am running a Java servlet using JDK 17, Tomcat 9.0 and Maven on VSCode. I am facing the problem that, when I create a new class to use a new route, Tomcat returns me an Error 404 saying that "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists" even though I added the @WebServlet annotation with the specified route servlet (in context, I should access http://localhost:8080/server/servlet).

This is the project folder structure:

src
└── main
    ├── java
    │   └── com
    │       └── example
    │           └── servlet
    │               └── MyServlet.java
    └── webapp
        ├── index.jsp
        └── WEB-INF
            ├── views
            │   └── view.jsp
            └── web.xml

When it is compiled to the /target folder, it is presented this way:

target
├── classes
│   └── com
│       └── example
│           └── servlet
│               └── MyServlet.class
├── generated-sources
│   └── annotations
├── maven-archiver
│   └── pom.properties
├── maven-status
│   └── maven-compiler-plugin
│       └── compile
│           └── default-compile
│               ├── createdFiles.lst
│               └── inputFiles.lst
├── server
│   ├── index.jsp
│   ├── META-INF
│   └── WEB-INF
│       ├── classes
│       │   └── com
│       │       └── example
│       │           └── servlet
│       │               └── MyServlet.class
│       ├── views
│       │   └── view.jsp
│       └── web.xml
└── server.war

I tried to map the servlet route on web.xml, but then Tomcat does not recognize the class (even though I mapped the class right there). So I don't know exactly what is the issue.

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>server</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>server Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>17</maven.compiler.release>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>

  <dependencies>
    <!-- https://mvnrepository.com/artifact/jakarta.servlet/jakarta.servlet-api -->
    <dependency>
      <groupId>jakarta.servlet</groupId>
      <artifactId>jakarta.servlet-api</artifactId>
      <version>5.0.0</version>
      <scope>provided</scope>
    </dependency>



    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>server</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

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"
  metadata-complete="true"
>
  <display-name> Welcome to Tomcat 9</display-name>
</web-app>

MyServlet.java:

package com.example.servlet;

import java.io.IOException;

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("/servlet")
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(
      HttpServletRequest request, 
      HttpServletResponse response) throws ServletException, IOException {
 
        request.getRequestDispatcher("/views/view.jsp").forward(request, response);

        // even when it is tested individually (not sending to a JSP file, it does not work
    }
}
INFO: Starting service [Catalina]
[apache-tomcat-9.0.88]: May 05, 2024 10:15:56 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.88]
[apache-tomcat-9.0.88]: May 05, 2024 10:15:56 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Web application installation directory [/home/santiago/.config/Code/User/workspaceStorage/d8ec3ec1ee17b60fa552c7bbc55f6194/adashen.vscode-tomcat/tomcat/apache-tomcat-9.0.88/webapps/server]
[apache-tomcat-9.0.88]: May 05, 2024 10:15:56 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [/home/santiago/.config/Code/User/workspaceStorage/d8ec3ec1ee17b60fa552c7bbc55f6194/adashen.vscode-tomcat/tomcat/apache-tomcat-9.0.88/webapps/server] has finished in [167] ms
[apache-tomcat-9.0.88]: May 05, 2024 10:15:56 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Web application installation directory [/home/santiago/.config/Code/User/workspaceStorage/d8ec3ec1ee17b60fa552c7bbc55f6194/adashen.vscode-tomcat/tomcat/apache-tomcat-9.0.88/webapps/ROOT]
[apache-tomcat-9.0.88]: May 05, 2024 10:15:56 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [/home/santiago/.config/Code/User/workspaceStorage/d8ec3ec1ee17b60fa552c7bbc55f6194/adashen.vscode-tomcat/tomcat/apache-tomcat-9.0.88/webapps/ROOT] has finished in [12] ms
[apache-tomcat-9.0.88]: May 05, 2024 10:15:56 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
[apache-tomcat-9.0.88]: May 05, 2024 10:15:56 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in [223] milliseconds

What I am missing to make the servlet new route work properly?


Solution

  • As @BasilBourque pointed out, some packages, specifications and tools were not in their correct versions to be ran together.

    Because I am using Tomcat 9.0, I had to change jakarta.servlet for javax.servlet instead on pom.xml file.

    Also I had to change the Servlet version on web.xml to version 4 instead of version 5, which is probably compatible only with Tomcat 10.0 and above, hence why it was not working properly and finding my route.

    The web-app tag on web.xml now is this:

    <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"
      metadata-complete="true">
    ...
    

    So I removed the @WebServlet annotation on MyServlet.java file and added this to web.xml:

    <servlet>
      <servlet-name>MyServlet</servlet-name>
      <servlet-class>com.example.servlet.MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
      <servlet-name>MyServlet</servlet-name>
      <url-pattern>/servlet</url-pattern>
    </servlet-mapping>
    

    And now MyServlet class looks like this, inside src/main/java/com/example/servlet:

    package com.example.servlet;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    // removed @WebServlet annotation, as there's no import of javax.servlet.annotations on this version (probably)
    
    public class MyServlet extends HttpServlet {
        @Override
        protected void doGet(
          HttpServletRequest request, 
          HttpServletResponse response) throws ServletException, IOException {
     
            response.setContentType("text/html");
            response.getWriter().write("Hello, World!");
        }
    }
    

    As I am using javax.servlet.servlet-api on version 3.0-alpha-1, I guess it has not added the @WebServlet annotation yet on this version, but the 4.0.1 version is here and maybe it already has the annotation.

    Now I can access my /servlet route correctly. It was hard to achieve it, because the compatibility of Tomcat with some versions of packages and other stuff is quite esoteric.

    UPDATE:

    Found out that the @WebServlet annotation was not available because I've set metadata-complete as true on web.xml file. As they are incompatible to coexist, I had to set it to false and the annotation works now:

    <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"
      metadata-complete="false">
    

    Now I can use @WebServlet(urlPatterns = "/servlet") on my class normally.