Tomcat java servlet 上的 404

Tomcat 404 on java servlet

我正在尝试按照此处的教程 https://tcserver.docs.pivotal.io/3x/docs-tcserver/topics/tutwebapp.html#tutwebapp-ant-buildfile 构建我的第一个 tomcat 应用程序。

虽然 jsp 部分工作正常,但我在 java servlet 部分得到了 404。这是我的相关文件和目录结构。有人可以指出我做错了什么吗?

(base) $ tree -r
.
└── helloworld
    ├── work
    │   ├── index.html
    │   ├── images
    │   │   └── Pivotal_Logo.png
    │   ├── hello_world_jsp.jsp
    │   └── WEB-INF
    │       ├── web.xml
    │       └── classes
    │           └── examples
    │               └── Hello_World_Java.class
    ├── web
    │   ├── index.html
    │   ├── images
    │   │   └── Pivotal_Logo.png
    │   ├── hello_world_jsp.jsp
    │   └── WEB-INF
    │       └── web.xml
    ├── src
    │   └── examples
    │       └── Hello_World_Java.java
    ├── helloworld.iml
    ├── dist
    │   └── hello_custom_build.war
    └── build.xml

12 directories, 13 files

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

    <display-name>HelloWorld Application</display-name>
    <description>
        This is a simple web application with a source code organization
        based on the recommendations of the Application Developer's Guide.
    </description>

    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>examples.Hello_World_Java</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/Hello_World_Java</url-pattern>
    </servlet-mapping>

</web-app>

index.html

<html>
<head>
    <title>Sample "Hello, World" Application</title>
</head>
<body bgcolor=white>

<table border="0" cellpadding="10">
    <tr>
        <td>
            <img src="images/Pivotal_Logo.png">
        </td>
        <td>
            <h1>Sample "Hello, World" Application</h1>
        </td>
    </tr>
</table>

<p>This is the home page for the HelloWorld Web application. </p>
<p>To prove that they work, you can execute either of the following links:
<ul>
    <li>To a JSP page: <a href="hello_world_jsp.jsp">Hello JSP Page</a>.
    <li>To a servlet: <a href="/Hello_World_Java">Hello via Java app</a>.
</ul>

</body>
</html>

Hello_World_Java 封装示例;

进口java.io.IOException; 导入 java.io.PrintWriter; 导入 javax.servlet.ServletException; 导入 javax.servlet.http.HttpServlet; 导入 javax.servlet.http.HttpServlet请求; 导入 javax.servlet.http.HttpServlet响应;

public final class Hello_World_Java extends HttpServlet {


    /**
     * Respond to a GET request for the content produced by
     * this servlet.
     *
     * @param request The servlet request we are processing
     * @param response The servlet response we are producing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
            throws IOException, ServletException {

        response.setContentType("text/html");
        PrintWriter writer = response.getWriter();
        writer.println("<html>");
        writer.println("<head>");
        writer.println("<title>Sample Application Servlet Page</title>");
        writer.println("</head>");
        writer.println("<body bgcolor=white>");

        writer.println("<table border=\"0\" cellpadding=\"10\">");
        writer.println("<tr>");
        writer.println("<td>");
        writer.println("<img src=\"images/Pivotal_Logo.png\">");
        writer.println("</td>");
        writer.println("<td>");
        writer.println("<h1>Sample Application Servlet</h1>");
        writer.println("</td>");
        writer.println("</tr>");
        writer.println("</table>");

        writer.println("This is the output of a servlet that is part of");
        writer.println("the Hello, World application.");

        writer.println("</body>");
        writer.println("</html>");
    }
}

根据您的评论,您似乎遗漏了 url 中的应用程序名称。应该是这样的:

localhost:8080/[应用名称]/Hello_World_Java