HttpServletRequest

總結:

一、queryString就是附加在URL中帶的參數,這部分參數會被URLEncode。post

二、url中帶的參數能夠和body中帶的參數相同,body中的多個參數也能夠相同。request.getParameter(name)只能返回一個值,若是url中包含該參數則返回url中的值,不然返回body中所帶的參數(若是body存在多個相同的,則取第一個)。編碼

三、GET 不能經過Body傳輸數據。post採起x-www-form-urlencoded時,body編碼爲:paramA=valueA&paramB=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--

 

1、示例一

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:|

 

2、示例二

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
相關文章
相關標籤/搜索