webservice服务器端获取request对象的三种方式

  有的时候在webservice里我们需要获取request对象和response对象,比如想要获得客户端的访问ip的时候就需要这么做,下面说三种方式,当然三种方式可能是针对不同方式部署webservice获取request对象的方法。

第一种:先配置注入:

@Resource
private 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:

webservice服务器端获取request对象的三种方式
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;
}
webservice服务器端获取request对象的三种方式
上一篇:matlab读取excel里的数据并用imagesc画图


下一篇:Java反射获取class对象的三种方式,反射创建对象的两种方式