【发布时间】:2020-01-05 18:34:35
【问题描述】:
按照Learn Java for Web Development (Apress) 中的说明,我创建了一个基于Tomcat 的Web 应用程序。 Web 应用程序已在 Eclipse 中开发为 Dynamic Web Project(完全按照书中的说明)。我使用的是 Eclipse 版本 2019-03 (4.11.0)。
应用程序的完整 URL 是 http://localhost:8080/helloworld/hello。我能够从 Eclipse 和浏览器运行此应用程序,使用完整的 URL。但是,当我只提供本地主机和端口号(即http://localhost:8080)时,我会收到 404 错误。我期待看到 Tomcat 服务器 “如果你看到这个,你已经成功安装了 Tomcat。恭喜!”页面。
这种行为在 Eclipse 和浏览器之间是一致的。
这是我在使用http://localhost:8080 时遇到的错误
这是我通过http://localhost:8080/helloworld/hello得到的输出
Tomcat 显然在 8080 端口上运行。这是我的netstat 命令的输出:
这是Java代码:
package apress.helloworld;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld extends HttpServlet{
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
{
try
{
response.setContentType("text/html");
PrintWriter printWriter = response.getWriter();
printWriter.println("<h2>");
printWriter.println("Hello World");
printWriter.println("</h2>");
}
catch (IOException ioException)
{
ioException.printStackTrace();
}
}
}
这里是web.xml 部署描述符:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>helloworld</display-name>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>apress.helloworld.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
【问题讨论】: