【问题标题】:Cannot access servlet using Eclipse and Tomcat 9无法使用 Eclipse 和 Tomcat 9 访问 servlet
【发布时间】:2019-06-08 07:55:21
【问题描述】:

我在 Eclipse 中创建了一个名为 TestWeb 的新动态 Web 项目。我在 WebContent 文件夹中添加了一个 index.html 并创建了一个 Web servlet,使该 servlet 在 /Test 中可用。

我可以通过http://localhost:8080/TestWeb 访问 index.html 文件,但是我无法通过http://localhost:8080/TestWeb/Test 访问 servlet。请帮忙。

这是 Servlet 的代码:

@WebServlet("/Test")
public class Test extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Test() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

}

这是我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="4.0">
  <display-name>TestWeb</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

【问题讨论】:

  • 请分享您的 web.xml
  • 我分享了 web.xml 文件。
  • 我不是 JEE 开发人员,但可能包含您获得的信息而不是您期望的信息会对那些可以帮助您的人有所帮助。那么您是否收到任何 404 错误?是否有与之相关的日志/堆栈跟踪?
  • @Hendré 尝试将@WebServlet("/Test") 更改为@WebServlet("TestWeb/Test")
  • @Pshemo 我收到一个没有堆栈跟踪的 404 错误。

标签: java eclipse tomcat


【解决方案1】:

首先检查您的 web.xml(部署描述符)文件,确保其中定义了正确的 xml 架构,如果那里一切正常,然后像这样更改您的注释 @WebServlet("/Test/*")

【讨论】:

  • 我将 web.xml 文件添加到问题中。更改为“/Test/*”没有帮助,我仍然收到 404 错误。
【解决方案2】:

我通过重命名项目解决了这个问题。我的项目名称由两个单词和一个空格组成。 “密码服务”。一旦我将项目重命名为“服务器”,我就可以访问 servlet。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多