SSH框架action中获取服务器端ip,端口,项目名


获取服务器端:

"http://"
+ org.apache.struts2.ServletActionContext.getRequest()
.getLocalAddr()
+ ":"
+ org.apache.struts2.ServletActionContext.getRequest()
.getServerPort()     


+ org.apache.struts2.ServletActionContext.getRequest()
.getContextPath();



本地测试结果为:http://127.0.0.1:8080/pojectTest/


action中获取request

ServletActionContext.getRequest();



获取客户端ip地址方法

  1.   public String getIpAddr(HttpServletRequest request) {     
  2.       String ip = request.getHeader("x-forwarded-for");     
  3.       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {     
  4.          ip = request.getHeader("Proxy-Client-IP");     
  5.      }     
  6.       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {     
  7.          ip = request.getHeader("WL-Proxy-Client-IP");     
  8.       }     
  9.      if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {     
  10.           ip = request.getRemoteAddr();     
  11.      }     
  12.      return ip;     
  13. }   



你可能感兴趣的:(博大精深java)