Quantcast
Channel: Active questions tagged servlets - Stack Overflow
Viewing all articles
Browse latest Browse all 714

Java Servlet does not recognize my route/class

$
0
0

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 startInternalINFO: 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 deployDirectoryINFO: 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 deployDirectoryINFO: 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 deployDirectoryINFO: 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 deployDirectoryINFO: 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 startINFO: Starting ProtocolHandler ["http-nio-8080"][apache-tomcat-9.0.88]: May 05, 2024 10:15:56 AM org.apache.catalina.startup.Catalina startINFO: Server startup in [223] milliseconds

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


Viewing all articles
Browse latest Browse all 714

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>