我的系统有tomcat 7,所有的填充物都在webapp下。文件结构为
webapps/WelcomeForm/web/WelcomeForm.html WEB-INF/web.xml lib/classes/hello/HelloWorldServlet.java HelloWorldServlet.class
Web 文件夹包含 WelcomeForm.html。WEB-INF 保存 web.xml。lib 包含 servlet-api.jar 和 classes 包含 HelloWorldServlet.java。hmtl 文件运行良好,但我无法运行 java 文件,因为它返回消息:
HTTP 状态 404 - /你好
类型状态报告
留言/你好
描述 请求的资源 (/hello) 不可用。
文件的代码如下:
WelcomeForm.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Welcome Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form method="POST" action="<%=request.getContextPath()%>/hello">
<font size="10" color="red">Hello World!</font><BR>
Type your first name and click submit button <input TYPE=TEXT NAME="username" SIZE=20>
<P><input type="submit" value="Submit">
</form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>hello.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
HelloWorldServlet.java
package hello;
import java.io.IOException;
import javax.servlet.ServletException;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class HelloWorldServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("username");
PrintWriter out = response.getWriter();
out.write("Your Name is :" );
out.print(name);
}
}
我究竟做错了什么?