Request獲取url資訊以及url帶的引數
阿新 • • 發佈:2019-01-31
通過url訪問伺服器可以得出連線本伺服器所有機器的ip以及埠等資訊
在瀏覽器上輸入:
http://192.168.191.1:8080/HttpServletRequestTest/servlet/HttpServletRequestTest?a=1&b=2
可以獲得訪問伺服器所有裝置的ip以及埠
以下是輸出的日誌:
Java程式碼如下:
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); }