java - 无法使用 Eclipse 和 Tomcat 9 访问 servlet

标签 java eclipse tomcat

我在 Eclipse 中创建了一个名为 TestWeb 的新动态 Web 项目。我将一个 index.html 添加到 WebContent 文件夹并创建了一个 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(部署描述符)文件,确保其中定义了正确的 xml 模式,如果一切正常,则像这样更改您的注释 @WebServlet("/测试/*")

关于java - 无法使用 Eclipse 和 Tomcat 9 访问 servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54179630/

相关文章:

即使数据库在启动时不可用,Spring 应用程序也应该启动

java - 我可以将 Java Swing 应用程序部署为 Glassfish 中的企业应用程序吗

java - 用Java读取CSV文件

java - 需要更简单的 Java/SQL 数据传输

eclipse - 在 Eclipse 中调试 Tomcat Web 应用程序

java - 比较文件内容 Eclipse

eclipse - 对没有上下文根的 Java 应用程序进行本地测试

java - 无法在 Eclipse 中启动 Tomcat 7.0

java - ASM COMPUTE_FRAMES If + 赋值错误

java - 使用 Eclipse 启动 Web Service Explorer 时出现错误 500