java webservice服务器端获取request对象的三种方式
来源:互联网 发布:dj音乐制作软件 编辑:程序博客网 时间:2024/05/20 10:21
转自http://www.cnblogs.com/longshiyVip/p/4985276.html
有的时候在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 getIp2(HttpServletRequest request) { String ip = request.getHeader("X-Forwarded-For"); if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){ //多次反向代理后会有多个ip值,第一个ip才是真实ip int index = ip.indexOf(","); if(index != -1){ return ip.substring(0,index); }else{ return ip; } } ip = request.getHeader("X-Real-IP"); if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){ return ip; } return request.getRemoteAddr(); }
阅读全文
0 0
- java webservice服务器端获取request对象的三种方式
- 获取request对象的第三种方式
- Java基础之获取Class对象的三种方式
- java 反射之获取class对象的三种方式
- java反射之获取class对象的三种方式
- 在SpringMVC中获取request对象的几种方式
- 在SpringMVC中获取request对象的几种方式
- 在SpringMVC中获取request对象的几种方式
- 在SpringMVC中获取request对象的几种方式
- 在SpringMVC中获取request对象的几种方式
- 在SpringMVC中获取request对象的几种方式
- 在SpringMVC中获取request对象的几种方式
- 在SpringMVC中获取request对象的几种方式
- struts2获取request对象的四种方式
- 在SpringMVC中获取request对象的几种方式
- 在SpringMVC中获取request对象的几种方式
- 在SpringMVC中,获取request对象的几种方式
- spring获取request对象的几种方式
- maven pom.xml配置文件详解
- Linux dhcp server配置
- 倒计时
- 微信curl上传客服头像接口遇到的坑
- Java内部静态类
- java webservice服务器端获取request对象的三种方式
- RobotFramework环境配置二十五:屏幕截图问题(滚动屏幕)
- android中 List
- 在Filter中注入Service
- 用assgin,和用weak修饰delegate的区别
- 纳什均衡的对赌问题
- [leetcode]: 27. Remove Element
- HTML 引入CSS 三种方式
- window10 tensorflow安装,完整安装,解决一切问题!