Tomcat 应用程序使用完整的路径和资源,否则不行
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 。但是,当我只提供本地主机和端口号(即 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 显然是 运行ning 在端口 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>
要在 /
(例如 http://localhost:8080/
)看到某些内容,您必须有一个名为 ROOT
的应用程序(因为名称不能为空)。这可以是 eclipse 中的应用程序、tomcat 的 webapps
目录中的文件 ROOT.war
,或者包含名为 ROOT
.
的 Web 应用程序的目录
如果您在 eclipse 中启动 tomcat 服务器,它可能没有部署标准的 Web 应用程序 - 事实上,您应该很高兴它没有,因为这使您能够 develop/deploy 这样的应用自己做。
如果您在 tomcat 外部 启动 eclipse,并使用从 tomcat.apache.org 下载的默认解压缩,您将看到默认的 ROOT web 应用程序。换句话说,一切都按预期工作,你没有在代码中犯任何错误,你只需要改变你对 eclipse-started tomcat 将立即部署的内容的期望
我已经按照 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 服务器 "If you're seeing this, you've successfully installed Tomcat. Congratulations!" 页面。
此行为在 Eclipse 和浏览器之间是一致的。
这是我用 http://localhost:8080
这是我用 http://localhost:8080/helloworld/hello
Tomcat 显然是 运行ning 在端口 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>
要在 /
(例如 http://localhost:8080/
)看到某些内容,您必须有一个名为 ROOT
的应用程序(因为名称不能为空)。这可以是 eclipse 中的应用程序、tomcat 的 webapps
目录中的文件 ROOT.war
,或者包含名为 ROOT
.
如果您在 eclipse 中启动 tomcat 服务器,它可能没有部署标准的 Web 应用程序 - 事实上,您应该很高兴它没有,因为这使您能够 develop/deploy 这样的应用自己做。
如果您在 tomcat 外部 启动 eclipse,并使用从 tomcat.apache.org 下载的默认解压缩,您将看到默认的 ROOT web 应用程序。换句话说,一切都按预期工作,你没有在代码中犯任何错误,你只需要改变你对 eclipse-started tomcat 将立即部署的内容的期望