【发布时间】:2018-10-21 05:57:25
【问题描述】:
我是 tomcat 的新手,我只是想制作一个 hello world 应用程序来习惯 tomcat,但是当我启动我的应用程序并转到 http://localhost:8080/helloweb/index.html 时,它给了我一个 404 错误。如果我转到http://localhost:8080/,那么它会将我带到 tomcat 的默认网页。请帮忙,我一直在尝试调试这个,但似乎找不到其他人遇到这个问题。
index.html
<!DOCTYPE html>
<html>
<head>
<title>Index Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Entry Form</h1>
<form name="Name Input Form" action="response.jsp">
Enter your name:
<input type="text" name="Name" value="" />
<input type="submit" value="OK" />
</form>
</body>
</html>
NameHandler.java
package org.mypackage.hello;
public class NameHandler {
private String name;
public NameHandler() {
name = null;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/helloweb"/>
response.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<jsp:useBean id="mybean" scope="session" class="org.mypackage.hello.NameHandler" />
<jsp:setProperty name="mybean" property="name" />
<h1>Hello, <jsp:getProperty name="mybean" property="name" />!</h1>
</body>
</html>
项目结构
My_Project_Root
|-- pom.xml
|-- nb-configuration.xml
`-- src
`-- main
|-- java
| `-- com
| `-- example
| `-- my_project
| `-- sample_class.java
|-- resources
`-- webapp
|-- META-INF
`-- context.xml
|-- WEB-INF
|-- index.html
【问题讨论】:
-
由于我们无法访问您的本地主机,因此需要源代码。
-
当然,我的错。刚刚更新以包含我的源代码。
-
你是如何部署它的?确保您使用的上下文根与文件夹或文件名完全匹配。项目中的 context.xml 经常被忽略。
-
我在netbeans中做了这个项目,context.xml是自动生成的。我也在通过 netbeans 部署它。
-
当你说上下文根时,你是指context.html中的
<Context path="" />吗?