1. 程式人生 > >HttpServletRequest物件方法的用法

HttpServletRequest物件方法的用法

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方法


例子程式:

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
         System.out.println("getRequestURL: "+request.getRequestURL());
         System.out.println("getRequestURI: "+request.getRequestURI());
         System.out.println("getQueryString: "+request.getQueryString());
         System.out.println("getRemoteAddr: "+request.getRemoteAddr());
         System.out.println("getRemoteHost: "+request.getRemoteHost());
         System.out.println("getRemotePort: "+request.getRemotePort());
         System.out.println("getRemoteUser: "+request.getRemoteUser());
         System.out.println("getLocalAddr: "+request.getLocalAddr());
         System.out.println("getLocalName: "+request.getLocalName());
         System.out.println("getLocalPort: "+request.getLocalPort());
         System.out.println("getMethod: "+request.getMethod());
         System.out.println("-------request.getParamterMap()-------");
         //得到請求的引數Map,注意map的value是String陣列型別
         Map map = request.getParameterMap();
         Set<String> keySet = map.keySet();
         for (String key : keySet) {
			String[] values = (String[]) map.get(key);
			for (String value : values) {
				System.out.println(key+"="+value);
			}
		 }
         System.out.println("--------request.getHeader()--------");
         //得到請求頭的name集合
         Enumeration<String> em = request.getHeaderNames();
         while (em.hasMoreElements()) {
			String name = (String) em.nextElement();
			String value = request.getHeader(name);
			System.out.println(name+"="+value);
		}
         
	}
瀏覽器上位址列:http://localhost:8080/RequestAndResponse/requestmethod?name=sunjob&password=123456&password=haha

控制檯輸出:

getRequestURL: http://localhost:8080/RequestAndResponse/requestmethod
getRequestURI: /RequestAndResponse/requestmethod
getQueryString: name=sunjob&password=123456&password=haha
getRemoteAddr: 127.0.0.1
getRemoteHost: 127.0.0.1
getRemotePort: 2374
getRemoteUser: null
getLocalAddr: 127.0.0.1
getLocalName: localhost
getLocalPort: 8080
getMethod: GET
-------request.getParamterMap()-------
name=sunjob
password=123456
password=haha
--------request.getHeader()--------
host=localhost:8080
user-agent=Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20100101 Firefox/17.0
accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
accept-language=zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3
accept-encoding=gzip, deflate
connection=keep-alive
cache-control=max-age=0