总结:
1、queryString就是附加在URL中带的参数,这部分参数会被URLEncode。
2、url中带的参数可以和body中带的参数相同,body中的多个参数也可以相同。request.getParameter(name)只能返回一个值,如果url中包含该参数则返回url中的值,否则返回body中所带的参数(如果body存在多个相同的,则取第一个)。
3、GET 不能通过Body传输数据。post采取x-www-form-urlencoded时,body编码为:paramA=valueA¶mB=valueB&......
4、只有使用的API需要从body读流(如:request.getParameter()),则会把body中所有的流读完,读完之后,后续从流中再也读不出数据。
5、如果先从流中读取数据,则后续的可能利用流中数据的API(如:request.getParameter())得不到数据。
6、post的body编码为form-data时,具体的body输出为:
Content-Disposition: form-data; name="ddd"; filename="test.txt"
Content-Type: text/plain
hello world!
----------------------------517080537389190054308391
Content-Disposition: form-data; name="dscsd"
违法而发weee
----------------------------517080537389190054308391--
一、示例一
doPost()如下:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //后从body读流 System.out.println("queryString:"+request.getQueryString()); System.out.println("urI:"+request.getRequestURI()); System.out.println("URL:"+request.getRequestURL()); Map<String, String[]> params = request.getParameterMap(); for (String key : params.keySet()) { String[] values = params.get(key); for (int i = 0; i < values.length; i++) { String value = values[i]; System.out.println("["+key+","+value+"]"); } } System.out.println("getParameter-name:"+request.getParameter("name")); System.out.println("getParameter-address:"+request.getParameter("address")); String body = getStreamAsString(request.getInputStream(), "utf-8"); System.out.println("body:"+body+"|"); }
A:请求如下
A:执行结果如下:
queryString:name=urlName urI:/Web URL:http://localhost:8080/Web [name,urlName] [name,bodyname] [name,duplicateName] [address,bodyaddressaddress] getParameter-name:urlName getParameter-address:bodyaddressaddress body:|
B:请求如下
B:执行结果如下:
queryString:null urI:/Web URL:http://localhost:8080/Web [name,bodyname] [name,duplicateName] [address,bodyaddressaddress] getParameter-name:bodyname getParameter-address:bodyaddressaddress body:|
二、示例二
doPost()代码如下:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 先从body读流 String body = getStreamAsString(request.getInputStream(), "utf-8"); System.out.println("body:" + body + "|"); System.out.println("queryString:" + request.getQueryString()); System.out.println("urI:" + request.getRequestURI()); System.out.println("URL:" + request.getRequestURL()); Map<String, String[]> params = request.getParameterMap(); for (String key : params.keySet()) { String[] values = params.get(key); for (int i = 0; i < values.length; i++) { String value = values[i]; System.out.println("[" + key + "," + value + "]"); } } System.out.println("getParameter-name:" + request.getParameter("name")); System.out.println("getParameter-address:" + request.getParameter("address")); }
A:请求如下:
A:执行结果如下:
body:name=bodyname&name=duplicateName&address=bodyaddressaddress| queryString:name=urlName urI:/Web URL:http://localhost:8080/Web [name,urlName] getParameter-name:urlName getParameter-address:null
B:请求如下:
B:执行结果如下:
body:name=bodyname&name=duplicateName&address=bodyaddressaddress| queryString:null urI:/Web URL:http://localhost:8080/Web getParameter-name:null getParameter-address:null