Java servlet 上的 Tomcat 404

问题描述 投票:0回答:2

我正在尝试按照此处的教程

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.HttpServletRequest; 导入 javax.servlet.http.HttpServletResponse;

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>");
    }
}
java tomcat tomcat9
2个回答
1
投票

根据您的评论,您似乎缺少网址中的应用程序名称。应该是这样的:

本地主机:8080/[应用程序名称]/Hello_World_Java


0
投票

您必须提供 Hello_World_Java.java 包中缺少一个组件

@WebServlet(“/Hello_World_Java”)

公开课声明上方

这是因为您在 <a href> 标记中请求

/Hello_World_Java
但您没有在 java 文件中给出 servlet 名称。

© www.soinside.com 2019 - 2024. All rights reserved.