简体   繁体   中英

Cannot access servlet using Eclipse and Tomcat 9

I created a new dynamic web project called TestWeb in Eclipse. I added a single index.html to the WebContent folder and created a single web servlet, making the servlet available at /Test.

I can access the index.html file at http://localhost:8080/TestWeb , however, I cannot access the servlet at http://localhost:8080/TestWeb/Test . Please assist.

Here is the code for the 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());
    }

}

Here is my 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(“ / Test / *”)更改注释

I soled the issue by renaming the project. My project name consisted of two words and one space. "Password Services". Once I renamed the project to "Server" I could reach the servlet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM