我正在 VSCode 上使用 JDK 17、Tomcat 9.0 和 Maven 运行 Java servlet。我面临的问题是,当我创建一个新类来使用新路由时,Tomcat 返回一个 Error 404 表示 “源服务器没有找到目标资源的当前表示或不愿意披露即使我添加了带有指定路线 @WebServlet
的
servlet
注释(在上下文中,我应该访问
http://localhost:8080/server/servlet
)。这是项目文件夹结构:
src
└── main
├── java
│ └── com
│ └── example
│ └── servlet
│ └── MyServlet.java
└── webapp
├── index.jsp
└── WEB-INF
├── views
│ └── view.jsp
└── web.xml
编译到/target
文件夹时,是这样呈现的:
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
我尝试在 web.xml
上映射 servlet 路由,但是 Tomcat 无法识别该类(即使我在那里映射了该类)。所以我不知道到底是什么问题。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="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
metadata-complete="false"
version="5.0"
>
<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
}
}
为了使 servlet 新路由正常工作,我缺少什么?
步骤2 停止服务器 使用带有名称和 url 模式的注释,例如 @WebServlet(name="giveServletClassName",urlPatterns={/url}) 并启动服务器。