java webservice服务器端获取request对象的三种方式
原文地址 www.cnblogs.com
有的时候在 webservice 里我们需要获取 request 对象和 response 对象,比如想要获得客户端的访问 ip 的时候就需要这么做,下面说三种方式,当然三种方式可能是针对不同方式部署 webservice 获取 request 对象的方法。
第一种: 先配置注入:
@Resourceprivate WebServiceContext webServiceContext;
其次是下面的代码:
MessageContext mc = webServiceContext.getMessageContext();HttpServletRequest request = (HttpServletRequest) (mc.get(MessageContext.SERVLET_REQUEST));
第二种:
WebServiceContext context = new WebServiceContextImpl();MessageContext ctx = context.getMessageContext();
HttpServletRequest request = (HttpServletRequest) ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
第三种 (附带获取客户端 ip 地址的方法):
Message message = PhaseInterceptorChain.getCurrentMessage();HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
获取 ip:
public static 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 || "unknow".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
以上是 java webservice服务器端获取request对象的三种方式 的全部内容, 来源链接: utcz.com/z/393935.html