一、queryString就是附加在URL中帶的參數,這部分參數會被URLEncode。post
二、url中帶的參數能夠和body中帶的參數相同,body中的多個參數也能夠相同。request.getParameter(name)只能返回一個值,若是url中包含該參數則返回url中的值,不然返回body中所帶的參數(若是body存在多個相同的,則取第一個)。編碼
三、GET 不能經過Body傳輸數據。post採起x-www-form-urlencoded時,body編碼爲:paramA=valueA¶mB=valueB&......url
四、只有使用的API須要從body讀流(如:request.getParameter()),則會把body中全部的流讀完,讀完以後,後續從流中再也讀不出數據。spa
五、若是先從流中讀取數據,則後續的可能利用流中數據的API(如:request.getParameter())得不到數據。code
六、post的body編碼爲form-data時,具體的body輸出爲:orm
Content-Disposition: form-data; name="ddd"; filename="test.txt"blog
Content-Type: text/plainutf-8
hello world!get
----------------------------517080537389190054308391it
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+"|"); }
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:|
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")); }
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
body:name=bodyname&name=duplicateName&address=bodyaddressaddress| queryString:null urI:/Web URL:http://localhost:8080/Web getParameter-name:null getParameter-address:null