javawebservice服务器端获取request对象的三种⽅式原⽂地址
如何生成webservice客户端有的时候在 webservice ⾥我们需要获取 request 对象和 response 对象,⽐如想要获得客户端的访问 ip 的时候就需要这么做,下⾯说三种⽅式,当然三种⽅式可能是针对不同⽅式部署 webservice 获取 request 对象的⽅法。
第⼀种: 先配置注⼊:
@Resource
private WebServiceContext webServiceContext;
其次是下⾯的代码:
MessageContext mc = MessageContext();
HttpServletRequest request = (HttpServletRequest) (mc.get(MessageContext.SERVLET_REQUEST));
第⼆种:
WebServiceContext context = new WebServiceContextImpl();
MessageContext ctx = MessageContext();
HttpServletRequest request = (HttpServletRequest) (AbstractHTTPDestination.HTTP_REQUEST);
第三种 (附带获取客户端 ip 地址的⽅法):
Message message = CurrentMessage();
HttpServletRequest request = (HttpServletRequest) (AbstractHTTPDestination.HTTP_REQUEST);
获取 ip:
public static String getIpAddr(HttpServletRequest request) {
String ip = Header("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = Header("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknow".equalsIgnoreCase(ip)) {
ip = Header("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = RemoteAddr();
}
return ip;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。