我的系统有 Tomcat 7,所有文件都在 webapps 下。文件结构为
webapps/ WelcomeForm/ web/ WelcomeForm.html
WEB-INF/web.xml
lib/ 类/ hello/ HelloWorldServlet.java HelloWorldServlet.class
Web 文件夹包含 WelcomeForm.html。
WEB-INF 保存 web.xml。
lib 包含 servlet-api.jar 和
类包含 HelloWorldServlet.java。
html 文件运行良好,但我无法运行 Java 文件,因为它返回消息:
HTTP Status 404 - /hello
type Status report
message /hello
description The requested resource (/hello) is not available.
文件的代码如下:
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="/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);
}
}
我究竟做错了什么?