HTML中调用servlet的问题(?)

最近在学习servlet。刚开始时,按照案例一行一行的敲代码。不过,出问题了。

hello1.html

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5     </head>
 6     <body style="font-size:24px">
 7         <form action="hello1" method="get">
 8             Name:<input name="name"><br />
 9             Content Me:<br />
10             QQ<input type="checkbox" name="contact" value="qq" />
11             Tel<input type="checkbox" name="contact" value="tel" />
12             WeChat<input type="checkbox" name="contact" value="wechat" />
13             <br />
14             <input type="submit" value="OK" />
15         </form>
16     </body>
17 </html>

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 4     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
 5     http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 6     id="WebApp_ID" version="3.1">
 7     <servlet>
 8         <servlet-name>hello1Servlet</servlet-name>
 9         <servlet-class>unitTwo.Hello1Servlet</servlet-class>
10     </servlet>
11     <servlet-mapping>
12         <servlet-name>hello1Servlet</servlet-name>
13         <url-pattern>/hello1</url-pattern>
14     </servlet-mapping>
15 </web-app>

Hello1Servlet.java

 1 package unitTwo;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 public class Hello1Servlet extends HttpServlet {
12     /**
13      * 
14      */
15     private static final long serialVersionUID = 1L;
16 
17     protected void service(HttpServletRequest request, HttpServletResponse response)
18             throws ServletException, IOException {
19         PrintWriter out = response.getWriter();
20         String name = request.getParameter("name");
21         out.println("<h1>Hello, " + name + "</h1>");
22         String[] contacts = request.getParameterValues("contact");
23         if (contacts != null) {
24             out.print("<h1>Contact Information: </h1>");
25             for (String info : contacts) {
26                 out.print("<h1>" + info + "</h1>");
27             }
28         }
29         out.close();
30     }
31 }

运行在TOMCAT v8.0上,结果是,在Eclipse EE上运行时,正常显示,但是到了浏览器上,就出问题了,显示为未找到该网页,应该是错误404。

至于原因,首先归结于在Eclipse EE上服务开启受到了限制,然后关了Eclipse EE,用命令重启服务,结果还是错误。

纠结了一天了,决定先将问题写下来,待深入些再来解决。

或者希望有大神能降临,轻轻松松的将这个问题秒杀。

以上

原文地址:https://www.cnblogs.com/SummerinShire/p/4631163.html