最近在学习servlet。刚开始时,按照案例一行一行的敲代码。不过,出问题了。
hello1.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body style="font-size:24px">
<form action="hello1" method="get">
Name:<input name="name"><br />
Content Me:<br />
QQ<input type="checkbox" name="contact" value="qq" />
Tel<input type="checkbox" name="contact" value="tel" />
WeChat<input type="checkbox" name="contact" value="wechat" />
<br />
<input type="submit" value="OK" />
</form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>hello1Servlet</servlet-name>
<servlet-class>unitTwo.Hello1Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello1Servlet</servlet-name>
<url-pattern>/hello1</url-pattern>
</servlet-mapping>
</web-app>
Hello1Servlet.java
package unitTwo; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class Hello1Servlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L; protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
out.println("<h1>Hello, " + name + "</h1>");
String[] contacts = request.getParameterValues("contact");
if (contacts != null) {
out.print("<h1>Contact Information: </h1>");
for (String info : contacts) {
out.print("<h1>" + info + "</h1>");
}
}
out.close();
}
}
运行在TOMCAT v8.0上,结果是,在Eclipse EE上运行时,正常显示,但是到了浏览器上,就出问题了,显示为未找到该网页,应该是错误404。
至于原因,首先归结于在Eclipse EE上服务开启受到了限制,然后关了Eclipse EE,用命令重启服务,结果还是错误。
纠结了一天了,决定先将问题写下来,待深入些再来解决。
或者希望有大神能降临,轻轻松松的将这个问题秒杀。
以上