我在 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/