// 保存用戶IP到cookie public void setUserIp2Cookie(String cookieName) { HttpServletRequest request = ServletActionContext.getRequest(); String userIp = request.getRemoteAddr(); if (request.getHeader("x-forwarded-for") != null) {// 當有代理的時候 獲取用戶真實IP userIp = request.getHeader("x-forwarded-for"); String[] userIps = userIp.split(","); for (int i = 0; i < userIps.length; i++) { if (!"unknown".equals(userIps[i])) { userIp = userIps[i]; break; } } } Cookie cookie = null; if (userIp.split(":").length > 1) { cookie = new Cookie(cookieName, cookieName); } else { cookie = new Cookie(cookieName, userIp); } cookie.setMaxAge(3600 * 24); response.addCookie(cookie); }