首页 > 代码库 > 借助form表单向web服务器发送消息

借助form表单向web服务器发送消息

form表单是常用的,在网页浏览器中 用户点击的请求经htto协议发送回web容器,请求处理

建立用户的页面

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8 <h3>beer select</h3>
 9     <form method="post" action="SelectBeer.do">
10     Selection beer charectar <p>
11     Color:
12     <select name="color" size="1">
13     <option>red
14     <option>blue
15     <option>gray
16     <option>yellow
17     </select>
18     <br>
19     <br>
20     <center>
21     <input type="submit">
22     </center>
23     </form>
24 </body>
25 </html>

servlet处理的内容,此处用doPost()方法,只展示覆写的doPost()内容

1 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
2         // TODO Auto-generated method stub
3         //doGet(request, response);
4         response.setContentType("text/html");
5         PrintWriter out = response.getWriter();
6         out.println("beer selection Adivce<br>");
7         String c= request.getParameter("color");
8         out.print("<br>got beer "+c);
9     }

配置web.xml文件,这里是将.do的逻辑符配置给服务器

 1 <servlet>
 2     <description></description>
 3     <display-name>BeerSelect</display-name>
 4     <servlet-name>BeerSelect</servlet-name>
 5     <servlet-class>BeerSelect</servlet-class>
 6   </servlet>
 7   <servlet-mapping>
 8     <servlet-name>BeerSelect</servlet-name>
 9     <url-pattern>/SelectBeer.do</url-pattern>
10   </servlet-mapping>

 编译文件,重启tomcat服务器,得到允许结果

技术分享

技术分享

 

借助form表单向web服务器发送消息