【问题标题】:Tomcat Application works with complete Path and Resource, not otherwise [duplicate]Tomcat应用程序使用完整的路径和资源,否则[重复]
【发布时间】: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>

【问题讨论】:

    标签: eclipse tomcat servlets


    【解决方案1】:

    为了在/(例如http://localhost:8080/)上看到某些内容,您必须有一个名为ROOT 的应用程序(因为名称不能为空)。这可以是 eclipse 中的应用程序,tomcat 的webapps 目录中的文件ROOT.war,或者具有名为ROOT 的Web 应用程序的目录。

    如果您在 Eclipse 中启动一个 tomcat 服务器,它可能没有部署标准的 Web 应用程序 - 事实上,您应该很高兴它没有部署,因为这使您能够自己开发/部署这样的应用程序。

    如果你在eclipse外部 启动tomcat,并使用从tomcat.apache.org 下载的默认解压缩,你会看到默认的ROOT web 应用程序。换句话说,一切都按预期工作,你的代码没有犯任何错误,你只需要改变你对 eclipse 启动的 tomcat 开箱即用部署的期望

    【讨论】:

      猜你喜欢
      • 2017-02-12
      • 1970-01-01
      • 2022-11-04
      • 2016-09-11
      • 1970-01-01
      • 2022-12-09
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      相关资源
      最近更新 更多