Tomcat 应用程序使用完整的路径和资源,而不是其他方式 [英] Tomcat Application works with complete Path and Resource, not otherwise

查看:23
本文介绍了Tomcat 应用程序使用完整的路径和资源,而不是其他方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照

这是我用 http://localhost:8080/helloworld/hello

得到的输出

Tomcat 显然在 8080 端口上运行.这是我的 netstat 命令的输出:

<小时>

Java 代码如下:

package apress.helloworld;导入 java.io.IOException;导入 java.io.PrintWriter;导入 javax.servlet.http.HttpServlet;导入 javax.servlet.http.HttpServletRequest;导入 javax.servlet.http.HttpServletResponse;公共类 HelloWorld 扩展了 HttpServlet{protected void doGet(HttpServletRequest request,HttpServletResponse 响应){尝试{response.setContentType("text/html");PrintWriter printWriter = response.getWriter();printWriter.println("

");printWriter.println("Hello World");printWriter.println("</h2>");}捕获(IOException ioException){ioException.printStackTrace();}}}

这里是 web.xml 部署描述符:

<display-name>helloworld</display-name><小服务程序><servlet-name>HelloWorld</servlet-name><servlet-class>apress.helloworld.HelloWorld</servlet-class><servlet-mapping><servlet-name>HelloWorld</servlet-name><url-pattern>/hello</url-pattern></servlet-mapping><欢迎文件列表><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>

解决方案

为了在 / 看到一些东西(例如 http://localhost:8080/)你必须有一个名为 ROOT 的应用程序(因为名称不能为空).这可以是 eclipse 中的应用程序,tomcat 的 webapps 目录中的文件 ROOT.war,或者包含名为 ROOT 的 web 应用程序的目录.>

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

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

I have created a Tomcat based web application, as per the instructions given in Learn Java for Web Development (Apress). The web application has been developed as a Dynamic Web Project in Eclipse (exactly as specified in the book). I am using Eclipse version 2019-03 (4.11.0).

The complete URL for the application is http://localhost:8080/helloworld/hello. I am able to run this application, with the complete URL both from Eclipse and from the browser. However, when I give only the localhost and port number (i.e. http://localhost:8080), I get the 404 error. I was expecting to see the Tomcat Server "If you're seeing this, you've successfully installed Tomcat. Congratulations!" page.

This behavior is consistent between Eclipse and the browser.

Here is the error I get with http://localhost:8080

and here is the output I get with http://localhost:8080/helloworld/hello

Tomcat is obviously running on port 8080. Here is the output of my netstat command:


Here is the Java code:

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();
        }
    }

}

And here is the web.xml Deployment Descriptor:

<?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>

解决方案

In order to see something at / (e.g. http://localhost:8080/) you'll have to have an application named ROOT (as the name can't be empty). This can be an application in eclipse, a file ROOT.war in tomcat's webapps directory, or a directory with a webapplication named ROOT.

If you start a tomcat server in eclipse, it might not have that standard web application deployed - in fact, you should be happy that it doesn't, because this enables you to develop/deploy such an application yourself.

If you start tomcat outside of eclipse, and use the default unzipped download from tomcat.apache.org, you'll see the default ROOT webapplication. In other words, everything works as expected, you didn't make any mistake in your code, you'll just have to change your expectations of what the eclipse-started tomcat will deploy out of the box

这篇关于Tomcat 应用程序使用完整的路径和资源,而不是其他方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆