我们经常需要在servlet(j2ee13.jar javax.servlet.http.HttpServletRequest)中,获取请求request的各种数据信息。
请求的URL: http://10.63.205.120:7001/undwrt/GetRealIP?param=a
可通过下面代码:
/** * 通过servlet获取客户端请求request的URL * http://10.63.205.120:7001/undwrt/GetRealIP?param=a * */ protected void getURL(HttpServletRequest request){ int ContentLength= request.getContentLength(); String ContentType= request.getContentType(); String CharacterEncoding= request.getCharacterEncoding(); String AuthType= request.getAuthType(); String Method= request.getMethod(); String RemoteAddr= request.getRemoteAddr(); String PathInfo= request.getPathInfo(); String ServerName= request.getServerName(); String Protocol= request.getProtocol(); String QueryString= request.getQueryString(); String RequestURI= request.getRequestURI(); String Scheme= request.getScheme(); int ServerPort= request.getServerPort(); String ServletPath= request.getServletPath(); String RequestURL= request.getRequestURL().toString(); String ContextPath= request.getContextPath(); System.out.println("ContentLength :"+ContentLength); System.out.println("ContentType :"+ContentType); System.out.println("CharacterEncoding :"+CharacterEncoding); System.out.println("AuthType :"+AuthType); System.out.println("Method :"+Method); System.out.println("RemoteAddr :"+RemoteAddr); System.out.println("PathInfo :"+PathInfo); System.out.println("ServerName :"+ServerName); System.out.println("Protocol :"+Protocol); System.out.println("QueryString :"+QueryString); System.out.println("RequestURI :"+RequestURI); System.out.println("Scheme :"+Scheme); System.out.println("ServerPort :"+ServerPort); System.out.println("ServletPath :"+ServletPath); System.out.println("RequestURL :"+RequestURL); System.out.println("ContextPath :"+ContextPath); }
run as:
ContentLength :-1
ContentType :null
CharacterEncoding :GBK
AuthType :null
Method :GET
RemoteAddr :10.63.205.79
PathInfo :null
ServerName :10.63.205.120
Protocol :HTTP/1.1
QueryString :param=a
RequestURI :/undwrt/GetRealIP
Scheme :http
ServerPort :7001
ServletPath :/GetRealIP
RequestURL :http://10.63.205.120:7001/undwrt/GetRealIP
ContextPath :/undwrt