本节重点
1. 总结1 整理下servlet传递参数方法,根据HTTP Method主要分为两种:
(1)第一种get方法
get方法的参数放置在HTT报文的请求行的URL字段,格式为key-value键值对。
(2)第二种POST方法
有了get为什么还要引入post方法, 当我们想要传递秘密的数据时,使用get会将这些数据显示到地址栏,显然不合适,所以,引入了post方法。POST方法的参数都放置在HTTP报文的中。根据不同内容格式,分为:
Form表单,参数是key-value,对于post form数据和get参数,HttpServletRequest 中存放,如
h1=zh-CN&source=hp&1=domety
1
h1=zh-CN&source=hp&1=domety
Json格式,参数类型为json形式。如
{
id:1,
name:zhonghuwu
}
1
2
3
4
{
id:1,
name:zhonghuwu
}
其他格式,如参数类型还可以为xml类型,此时内容为
1
2
3
4
2.总结2从服务器端和客户端两种角度进行编程
1服务器端角度
服务器端接受用户端传来参数和返回给客户端结果。
1.1 Request参数在HTTP报文中位置
HTTP请求报文由请求行、请求头部、空行和请求数据4个部分组成。如下图
有了get为什么还要引入post方法, 当我们想要传递秘密的数据时,使用get会将这些数据显示到地址栏,显然不合适,所以,引入了post方法。
1. get方法的参数
是放置在报文Header的URL中
2. POST 表单参数
POST的参数都是放置在Request Body中。如下图
POST方法,根据在RequestBody中数据格式分为:
(1)POST表单的数据,
对应表单参数都是以的键值对方式。且获取的都是字符串格式为键值对,如上图中参数的格式为:
h1=zh-CN&source=hp&1=domety
(2)POST的JSON数据参数
数据格式为json格式字符串。
(3)其他
如xml,此时RequestBody中的参数格式就是XML格式。
1.2获取请求中参数
1. 获取post方法的json参数代码为
//获取请求报文Body中查询参数
String requestBody = IOUtils.toString(request.getInputStream(),
Charsets.UTF_8.name());
1
2
3
//获取请求报文Body中查询参数
StringrequestBody=IOUtils.toString(request.getInputStream(),
Charsets.UTF_8.name());
然后,可以通过JSONOBject中来解析。
2. get和post方法非json数据公用的方法
getParameter(“key”)
1
getParameter(“key”)
返回值都是String字符串,如果想要的是数字型、布尔型,自己进行转换。
3. get方法
String queryString = request.getQueryString();
1
StringqueryString=request.getQueryString();
4. post 表单方法
request.getParameterMap()
1
request.getParameterMap()
1.3 使用HttpServletResponse返回数据
以json格式为例:
protected void doPost(HttpServletRequest req, HttpServletResponse resp){
JSONObject json=new JSONObject();
json.put("name", name);
json.put("pwd", pwd);
//设置response类型
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
//将数据写到response body中
PrintWriter out=resp.getWriter();
out.write(json.toString());
out.flush();
out.close()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
protectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp){
JSONObjectjson=newJSONObject();
json.put("name",name);
json.put("pwd",pwd);
//设置response类型
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
//将数据写到response body中
PrintWriterout=resp.getWriter();
out.write(json.toString());
out.flush();
out.close()
}
2从客户端进行操作
看成是一个浏览器客户端,发送请求和接受请求
2.1构造一个请求
本小节内容为:使用HttpPost和HttpGet对象
1.使用HttpPost构造一个Post方法的Json类型参数的请求的代码如下
//设置内容为UTF-8编码,否则会出现中文乱码
StringEntity stringEntity = new StringEntity(requestBody, Charsets.UTF_8.name());
//设置content-type属性
stringEntity.setContentType(APPLICATION_JSON);
httpPost.setEntity(stringEntity);
1
2
3
4
5
//设置内容为UTF-8编码,否则会出现中文乱码
StringEntitystringEntity=newStringEntity(requestBody,Charsets.UTF_8.name());
//设置content-type属性
stringEntity.setContentType(APPLICATION_JSON);
httpPost.setEntity(stringEntity);
2. 使用HttpPost构造一个Post方法的非Json类型参数
对于form表单形式的参数,代码如下
List parametes = Lists.newLinkedList();
paramerters.add(new BasicNameValuePair("key", "value"));
httpPost.setEntity(new UrlEncodedFormEntity(parameters, Charsets.UTF_8));
1
2
3
Listparametes=Lists.newLinkedList();
paramerters.add(newBasicNameValuePair("key","value"));
httpPost.setEntity(newUrlEncodedFormEntity(parameters,Charsets.UTF_8));
3. 使用HttpGet构造一个Get方法请求
如上的都是针对HttpPost,即都是Post的请求方法,如果此时方法是Get类型的就需要构造一个HttpGet,设置参数方法是在HttpGet的URI中将参数以”key=value&key=value”拼接到此uri后面
2.2 使用HttpResponse接受一个请求的数据
HttpResponse response = HttpClient.execute(request);
httpEntity = response.getEntity();
String responseString = EntityUtils.toString(httpEntity, Charsets.UTF_8);
1
2
3
HttpResponseresponse=HttpClient.execute(request);
httpEntity=response.getEntity();
StringresponseString=EntityUtils.toString(httpEntity,Charsets.UTF_8);
(全文完)