HttpServletRequest對象方法的用法 (轉)

來源:互聯網
上載者:User

標籤:print   remote   name   ref   flat   dog   htm   資訊   control   

 出處:http://blog.csdn.net/xh16319/article/details/8450715

 

 

1. 獲得客戶機資訊
    getRequestURL方法返回用戶端發出請求時的完整URL。
    getRequestURI方法返回請求行中的資源名部分。
    getQueryString 方法返回請求行中的參數部分。
    getRemoteAddr方法返回傳出請求的客戶機的IP地址 
    getRemoteHost方法返回傳出請求的客戶機的完整主機名稱
    getRemotePort方法返回客戶機所使用的網路連接埠號碼
    getLocalAddr方法返回WEB伺服器的IP地址。
    getLocalName方法返回WEB伺服器的主機名稱 
    getMethod得到客戶機請求方式
 2.獲得客戶機要求標頭 

    getHeader(string name)方法 
    getHeaders(String name)方法 
    getHeaderNames方法 

 3. 獲得客戶機請求參數(用戶端提交的資料)
    getParameter(name)方法
    getParameterValues(String name)方法
    getParameterNames方法 
    getParameterMap方法


例子程式:

 

[java] view plain copy 
  1. public void doGet(HttpServletRequest request, HttpServletResponse response)  
  2.         throws ServletException, IOException {  
  3.         System.out.println("getRequestURL: "+request.getRequestURL());  
  4.         System.out.println("getRequestURI: "+request.getRequestURI());  
  5.         System.out.println("getQueryString: "+request.getQueryString());  
  6.         System.out.println("getRemoteAddr: "+request.getRemoteAddr());  
  7.         System.out.println("getRemoteHost: "+request.getRemoteHost());  
  8.         System.out.println("getRemotePort: "+request.getRemotePort());  
  9.         System.out.println("getRemoteUser: "+request.getRemoteUser());  
  10.         System.out.println("getLocalAddr: "+request.getLocalAddr());  
  11.         System.out.println("getLocalName: "+request.getLocalName());  
  12.         System.out.println("getLocalPort: "+request.getLocalPort());  
  13.         System.out.println("getMethod: "+request.getMethod());  
  14.         System.out.println("-------request.getParamterMap()-------");  
  15.         //得到請求的參數Map,注意map的value是String數群組類型  
  16.         Map map = request.getParameterMap();  
  17.         Set<String> keySet = map.keySet();  
  18.         for (String key : keySet) {  
  19.         String[] values = (String[]) map.get(key);  
  20.         for (String value : values) {  
  21.             System.out.println(key+"="+value);  
  22.         }  
  23.      }  
  24.         System.out.println("--------request.getHeader()--------");  
  25.         //得到要求標頭的name集合  
  26.         Enumeration<String> em = request.getHeaderNames();  
  27.         while (em.hasMoreElements()) {  
  28.         String name = (String) em.nextElement();  
  29.         String value = request.getHeader(name);  
  30.         System.out.println(name+"="+value);  
  31.     }  
  32.           
  33. }  

瀏覽器上地址欄:http://localhost:8080/RequestAndResponse/requestmethod?name=sunjob&password=123456&password=haha

 

控制台輸出:

 

[plain] view plain copy 
  1. getRequestURL: http://localhost:8080/RequestAndResponse/requestmethod  
  2. getRequestURI: /RequestAndResponse/requestmethod  
  3. getQueryString: name=sunjob&password=123456&password=haha  
  4. getRemoteAddr: 127.0.0.1  
  5. getRemoteHost: 127.0.0.1  
  6. getRemotePort: 2374  
  7. getRemoteUser: null  
  8. getLocalAddr: 127.0.0.1  
  9. getLocalName: localhost  
  10. getLocalPort: 8080  
  11. getMethod: GET  
  12. -------request.getParamterMap()-------  
  13. name=sunjob  
  14. password=123456  
  15. password=haha  
  16. --------request.getHeader()--------  
  17. host=localhost:8080  
  18. user-agent=Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20100101 Firefox/17.0  
  19. accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8  
  20. accept-language=zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3  
  21. accept-encoding=gzip, deflate  
  22. connection=keep-alive  
  23. cache-control=max-age=0  

 

HttpServletRequest對象方法的用法 (轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.