【发布时间】:2021-03-21 23:11:33
【问题描述】:
我正在尝试使用 Eclipse+Tomcat 使用单个 servlet 和 JSP 设置一个非常基本的 JEE 项目。但是,我在尝试访问其 URL http://localhost:8080/test/toto 时收到 HTTP 500 内部服务器错误。正在显示的异常消息是:
无法调用“javax.servlet.RequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)”,因为“javax.servlet.ServletContext.getRequestDispatcher(String)”的返回值为空。
这是我的测试 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
这是项目树形
编辑:我最近尝试的其他事情:在另一台电脑上重新安装 Eclipse+Tomcat 并重新启动,将 test.jsp 直接移动到 WebContent 并访问 http://localhost:8080/test/test.jsp: 导致 HTTP 404 ressource not found 错误。
我刚刚注意到,在我创建我的 servlet 之前,我可以在 http://localhost:8080/test/whatever.jsp 访问我在 WebContent 下创建的任何文件,但是一旦我添加了 servlet 并在 web.xml 中建立映射,我就不能再这样做了
【问题讨论】:
-
我把这里的格式弄乱了,现在应该可以了,我删除了多余的'>'
标签: eclipse jsp tomcat servlets