问题描述
在Struts2 中 怎样通过ActionInvocation对象 获得访问者的ip ;好像是在getInvocationContext()里面 , 各位帅哥美女 !大哥大姐 ! 小弟菜鸟一个!请求支援啊 问题补充:laorer 写道
解决方案
Struts2中获得request及ip//在struts2中获得request HttpServletRequest request = ServletActionContext. getRequest(); //获得ip地址(防止代) public String getIpAddr(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; }//http://hi.baidu.com/wxyhaiy/blog/item/67aeef3f9cac77c87d1e710d.html
解决方案二:
request.getRemoteAddr()
解决方案三:
ActionContext ctx = invocation.getInvocationContext(); HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); 有了request,剩下的不用我说了吧