Tomcat应用程序需要完整路径和资源才能正常工作,否则无法正常运行。

huangapple go评论62阅读模式
英文:

Tomcat Application works with complete Path and Resource, not otherwise

问题

我已根据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来运行应用程序。然而,当我只提供localhost和端口号(即http://localhost:8080)时,我收到404错误。我期望看到Tomcat服务器的页面,上面写着"If you're seeing this, you've successfully installed Tomcat. Congratulations!"。

这个行为在Eclipse和浏览器之间是一致的。

这是我在http://localhost:8080上收到的错误:

Tomcat应用程序需要完整路径和资源才能正常工作,否则无法正常运行。

这是我在http://localhost:8080/helloworld/hello上收到的输出:

Tomcat应用程序需要完整路径和资源才能正常工作,否则无法正常运行。

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

Tomcat应用程序需要完整路径和资源才能正常工作,否则无法正常运行。

以下是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>
英文:

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

Tomcat应用程序需要完整路径和资源才能正常工作,否则无法正常运行。

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

Tomcat应用程序需要完整路径和资源才能正常工作,否则无法正常运行。

Tomcat is obviously running on port 8080. Here is the output of my netstat command:
Tomcat应用程序需要完整路径和资源才能正常工作,否则无法正常运行。


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(&quot;text/html&quot;);
			PrintWriter printWriter = response.getWriter();
			printWriter.println(&quot;&lt;h2&gt;&quot;);
			printWriter.println(&quot;Hello World&quot;);
			printWriter.println(&quot;&lt;/h2&gt;&quot;);
		}
		catch (IOException ioException)
		{
			ioException.printStackTrace();
		}
	}

}

And here is the web.xml Deployment Descriptor:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;
xmlns:web=&quot;http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;
xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id=&quot;WebApp_ID&quot; version=&quot;3.0&quot;&gt;
	&lt;display-name&gt;helloworld&lt;/display-name&gt;
	&lt;servlet&gt;
		&lt;servlet-name&gt;HelloWorld&lt;/servlet-name&gt;
		&lt;servlet-class&gt;apress.helloworld.HelloWorld&lt;/servlet-class&gt;
	&lt;/servlet&gt;
	&lt;servlet-mapping&gt;
		&lt;servlet-name&gt;HelloWorld&lt;/servlet-name&gt;
		&lt;url-pattern&gt;/hello&lt;/url-pattern&gt;
	&lt;/servlet-mapping&gt;
	&lt;welcome-file-list&gt;
		&lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
		&lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;
		&lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
		&lt;welcome-file&gt;default.html&lt;/welcome-file&gt;
		&lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;
		&lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;
	&lt;/welcome-file-list&gt;
&lt;/web-app&gt;

答案1

得分: 2

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

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

如果您在eclipse之外启动tomcat,并使用来自tomcat.apache.org的默认解压缩下载,您将看到默认的ROOT Web应用程序。换句话说,一切都按预期进行,您没有在代码中犯任何错误,只是需要调整您对由eclipse启动的tomcat默认部署的期望。

英文:

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

huangapple
  • 本文由 发表于 2020年1月6日 02:34:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/59603006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定