打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
在基于CXF的webservice中访问HttpServletRequest的方法

基于CXF开发webservice,想在webservice实现类中获得HttpServletRequest对象,用了几种方式,用@Resources标签或者@Context标签都不行。@Context标签下对象为null,@Resource标签虽然注入了org.apache.cxf.jaxws.context.WebServiceContextImplWebServiceContext 对象,但是该对象内部值为null,什么也取不到。

 

后面用了个笨办法,在web.xml中增加一个filter

 

Xml代码  
  1. <filter>  
  2.     <filter-name>ContextServlet</filter-name>  
  3.     <filter-class>xxx.filter.ContextServlet</filter-class>  
  4. </filter>  
  5. <filter-mapping>  
  6.     <filter-name>ContextServlet</filter-name>  
  7.     <url-pattern>/services/*</url-pattern>  
  8. </filter-mapping>  

 

 

在filter中访问HttpServletRequest,将需要获得的值保存在ThreadLocal中

 

Java代码  
  1. public static ThreadLocal<String> path = new ThreadLocal<String>();  
  2.     @Override  
  3.     public void destroy() {  
  4.         path.remove();        
  5.     }  
  6.   
  7.     @Override  
  8.     public void doFilter(ServletRequest arg0, ServletResponse arg1,  
  9.             FilterChain arg2) throws IOException, ServletException {  
  10.         HttpServletRequest request=(HttpServletRequest)arg0;          
  11.         String path = request.getContextPath();  
  12.         basePath.set(path);  
  13.         arg2.doFilter(arg0, arg1);  
  14.     }  

 

这样,在webservice的实现类中只要用 ContextServlet.path.get()就可以获得当前HttpServletRequest对中的变量。 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
CXF WebService Notification(WS-N)使用简介
CXF(前身xfire) 整合 spring 以及 aop事务配置
拦截器和过滤器的区别(转)
拦截器(Interceptor)和过滤器(Filter)的执行顺序和区别
spring mvc 拦截器
SpringMVC拦截器
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服