Tomcat Application works with complete Path and Resource, not otherwise [duplicate]

感情迁移 提交于 2020-01-25 07:32:25

问题


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:


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



来源:https://stackoverflow.com/questions/59603006/tomcat-application-works-with-complete-path-and-resource-not-otherwise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!