无法通过 URL 访问 Java servlet

Can't access a Java servlet through its URL

我正在尝试使用 Eclipse+Tomcat 使用单个 servlet 和 JSP 建立一个非常基本的 JEE 项目。但是,我在尝试访问其 URL http://localhost:8080/test/toto 时遇到 HTTP 500 内部服务器错误。显示的异常消息是:

Cannot invoke "javax.servlet.RequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)" because the return value of "javax.servlet.ServletContext.getRequestDispatcher(String)" is null.

这是我的测试 servlet 代码:


import java.io.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Test extends HttpServlet {
    public void doGet( HttpServletRequest request, HttpServletResponse response )
               throws ServletException, IOException{
        this.getServletContext().getRequestDispatcher( "/WEB-INF/test.jsp" ).forward( request, response );
    }
}

test.jsp 文件应该只显示一条消息:“由 JSP 生成” 以下是web.xml的内容供参考:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0">
    <servlet>
        <servlet-name>Test</servlet-name>
        <servlet-class>com.sdzee.servlets.Test</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Test</servlet-name>
        <url-pattern>/toto</url-pattern>
    </servlet-mapping>
</web-app> 

对应的Tomcat日志为:

0:0:0:0:0:0:0:1 - - [10/Dec/2020:14:29:47 +0100] "GET /test/toto HTTP/1.1" 500 1606

这是项目arborescence

编辑:我最近尝试的其他事情:在另一台电脑上重新安装 Eclipse+Tomcat 并重新开始,将 test.jsp 直接移动到 WebContent 并访问 http://localhost:8080/test/test.jsp: 导致 HTTP 404 资源没有发现错误。 我刚刚注意到,在我创建我的 servlet 之前,我可以访问我在 http://localhost:8080/test/whatever.jsp 的 WebContent 下创建的任何文件,但是一旦我添加了 servlet 并在 web.xml[ 中建立了映射,我就不能再这样做了=22=]

我正在编辑 Tomcat 服务器的 web.xml 文件,而不是项目文件,这就是为什么没有按预期工作的原因。