JavaWeb核心编程之(三.5)HTTP请求和接受表单数据

HTTP简介
WEB浏览器与WEB拂去其之间的一问一答的交互过程, 必须遵循一定的规则,这个规则就是HTTP协议
HTTP是hypertext transfer protocol(超文本传输协议)的简写, 它是TCP/IP协议集中的一个应用层协议, 用于定义WEB浏览器与WEB服务器之间交换数据的过程以及数据本身的格式
HTTP协议的版本 HTTP/1.0 HTTP/1.1 HTTP-NG

HTTP的会话方式
四个步骤
1> 建立连接
2> 发出请求信息
3> 回送响应信息
4> 关闭连接

浏览器与WEB服务器之间的链接过程式短暂的, 每次链接只处理一个请求和响应. 对每一个页面的访问, 浏览器与WEB服务器都要建立一次单独的连接.
浏览器到WEB服务器之间的所有通讯都是完全独立分开的请求和响应对儿

 

创建项目testHttpForm  ---> package(com.xiaoan.test)  --> class(TestForm.java)

 1 package com.xiaoan.test;
 2 
 3 import java.io.IOException;
 4 import java.util.Arrays;
 5 import java.util.Enumeration;
 6 import java.util.Map;
 7 import java.util.Map.Entry;
 8 
 9 import javax.servlet.Servlet;
10 import javax.servlet.ServletConfig;
11 import javax.servlet.ServletException;
12 import javax.servlet.ServletRequest;
13 import javax.servlet.ServletResponse;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.swing.plaf.synth.SynthSeparatorUI;
16 
17 public class TestForm implements Servlet{
18 
19     @Override
20     public void destroy() {
21         // TODO Auto-generated method stub
22         
23     }
24 
25     @Override
26     public ServletConfig getServletConfig() {
27         // TODO Auto-generated method stub
28         return null;
29     }
30 
31     @Override
32     public String getServletInfo() {
33         // TODO Auto-generated method stub
34         return null;
35     }
36 
37     @Override
38     public void init(ServletConfig arg0) throws ServletException {
39         // TODO Auto-generated method stub
40         
41     }
42 
43     @Override
44     public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
45         System.out.println("this is Servlet service function");
46         //1 获取请求参数 k--v
47         String username = request.getParameter("username");
48         String password = request.getParameter("password");
49         System.out.println("username: " + username);
50         System.out.println("password: " + password);
51         //获取请求参数 k -- v1,v2,v3   -  表单中checkbox类型数据
52         String[] reads = request.getParameterValues("read");
53         for(String read: reads){
54             System.out.println("-->read: " + read);
55         }
56         
57         //获取所有的参数名
58         Enumeration<String> keys = request.getParameterNames();
59         
60         //获取所有的请求参数 <k,v>
61         Map<String, String[]> params = request.getParameterMap();
62         for(Map.Entry<String, String[]> param: params.entrySet()){
63             System.out.println("****" + param.getKey() + ": " + Arrays.asList(param.getValue()));
64         }
65         
66         /*
67          * HttpServletRequest: 是 SerlvetRequest 的子接口. 针对于 HTTP 请求所定义. 里边包含了大量获取 HTTP 请求相关的方法.
68          */
69         
70         //获取请求的URI
71         HttpServletRequest httpServletRequest = (HttpServletRequest) request;
72         String uri = httpServletRequest.getRequestURI();
73         System.out.println("request uri: " + uri);
74         
75         //获得请求方式
76         String method = httpServletRequest.getMethod();
77         System.out.println("request method: " + method);
78         
79         //如果是get请求, 则获得问号传参后面的部分, 如果是post请求该项返回null
80         String getstr = httpServletRequest.getQueryString();
81         System.out.println("getstr: " + getstr);
82         
83         //获取请求的 Serlvet 的映射路径
84         String servletPath = httpServletRequest.getServletPath();
85         System.out.println(servletPath);
86     }
87 
88 }

--> 配置web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3   <display-name>testHttpForm</display-name>
 4   
 5   
 6   <servlet>
 7       <servlet-name>formServlet</servlet-name>
 8       <servlet-class>com.xiaoan.test.TestForm</servlet-class>
 9   </servlet>
10   <servlet-mapping>
11       <servlet-name>formServlet</servlet-name>
12       <url-pattern>/formServlet</url-pattern>
13   </servle

写表单(WebContent目录下)  新建testform.html

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8     <form action="/testHttpForm/formServlet" method="get">
 9     username: <input type="text" name="username"><br>
10     password: <input type="password" name="password"><br>
11     多选测试:<input type="checkbox" name="read" value=1>1 &nbsp;&nbsp;&nbsp;
12     <input type="checkbox" name="read" value=2>2 &nbsp;&nbsp;&nbsp;
13     <input type="checkbox" name="read" value=3>3 &nbsp;&nbsp;&nbsp;
14     <input type="checkbox" name="read" value=4>4 &nbsp;&nbsp;&nbsp;
15     <input type="checkbox" name="read" value=5>5 &nbsp;&nbsp;&nbsp;
16     <input type="submit" name="submit" value="提交">
17     </form>
18 </body>
19 </html>

提交方式 get和post切换一下, 看看有什么不同

 

posted @ 2016-07-24 22:28  安仲炜  阅读(3609)  评论(0编辑  收藏  举报