java - Servlet 错误 HTTP 状态 404 – 未找到 Java Tomcat

标签 java tomcat

我知道已经有类似的问题,但没有一个解决方案适合我。我检查了目录并编辑了我的 `web.xml 文件几次,但它仍然不起作用。

我正在用 Java 编写一个在 Tomcat 上运行的简单 servlet,但出现错误:

HTTP Status 404 – Not Found

Type Status Report

Message /WorkshopForm/MainWorkshopForm

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

我的 servlet 类是:

package workshop;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(description = "This registration form", urlPatterns = { 
"/WorkshopForm" })


public class WorkshopForm {
public class MyServlet extends HttpServlet implements Servlet {
    private static final long serialVersionUID = 13425L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String participantName = request.getParameter("participantName");
    String participantSurname = request.getParameter("participantSurname");
    String participantEmail = request.getParameter("participantEmail");

    PrintWriter writer = response.getWriter();
    writer.println("Welcome" + participantName + " " + participantSurname + " " + participantEmail); 
    }       
}

我的web.xml:

screenshot web file

Eclipse 中的树:

screenshot directories

web.xml文件有问题吗?我在想,也许我的名称或路径不匹配,但我已经尝试解决它,但不知道为什么它不起作用。

最佳答案

URL 模式 /WorkshopForm 仅匹配确切的 URL 路径 /WorkshopForm。如果您希望 servlet 也处理更长的路径,例如 /WorkshopForm/MainWorkshopForm,您需要将 URL 模式更改为 /WorkshopForm/*。然后您可以在您的 servlet 代码中调用 request.getPathInfo() 来获取路径的可变部分。

另一种方法是使用一些 JAX-RS 框架来处理从 URL 路径到处理单个路径的 Java 方法的映射。

关于java - Servlet 错误 HTTP 状态 404 – 未找到 Java Tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50278013/

相关文章:

java - 在 IntelliJ for Java 中自动将大括号添加到单行 if/for/while 等语句中

java - 无法在三星智能电视上使用 Flash 加载 HTTPS 验证码

tomcat - 在 Web 上哪里可以找到 Tomcat 托管的 WAR

java - 使用 web.xml 文件从 Tomcat 中的 URL 中删除 "default.jsp"

linux - 亚马逊 AMI tomcat7 tomcat-users.xml : command not found

java.io.StreamCorruptedException : invalid stream header:

java - 为什么Java变量名不能以数字开头?

java - Tomcat 似乎无法释放线程

apache - 如何生成和配置 ETag?

java - 问题: Java. util.date在 hibernate 中不允许重复的日期值