Struts2 保存用户IP到cookie
?
// 保存用户IP到cookiepublic void setUserIp2Cookie(String cookieName) {HttpServletRequest request = ServletActionContext.getRequest();String userIp = request.getRemoteAddr();if (request.getHeader("x-forwarded-for") != null) {// 当有代理的时候 获取用户真实IPuserIp = 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);}