英文:
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
上收到的错误:
这是我在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>
英文:
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>
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论