java servlet取得客戶端相干信息的簡略代碼。本站提示廣大學習愛好者:(java servlet取得客戶端相干信息的簡略代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是java servlet取得客戶端相干信息的簡略代碼正文
java web很經常使用的功效代碼,可以合營完成IP白名單掌握,直接貼代碼:
import javax.servlet.http.HttpServletRequest;
public class ClientInfoUtil{
/**
* 取得客戶真個IP地址
* @param request
* @return
*/
static public String getIP(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
/**
* 取得客戶端拜訪辦事器的url地址
* @param request
* @return
*/
static public String getURL(HttpServletRequest request) {
String url = request.getScheme()+"://";
url+=request.getHeader("host");
url+=request.getRequestURI();
if(request.getQueryString()!=null) {
url+="?"+request.getQueryString();
}
return url;
}
}