在普通类中获取request和session

在使用spring时,经常需要在普通类中获取session,request等对像. 
比如一些AOP拦截器类,在有使用struts2时,因为struts2有一个接口使用org.apache.struts2.ServletActionContext即可很方便的取到session对像. 

用法:ServletActionContext.getRequest().getSession(); 

但在单独使用spring时如何在普通类中获取session,reuqest呢? 


第一种:
在web.xml增加如下代码: 

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <listener>   
  2.    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>   
  3. </listener> 

注解方式

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @Autowired    
  2. private HttpSession session;      
  3. @Autowired    
  4. private HttpServletRequest request;    
  1. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();   

第二种:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class RequestUtils {  
  2.     private static final Logger log = LoggerFactory  
  3.             .getLogger(RequestUtils.class);  
  4.     private static ThreadLocal<HttpServletRequest> requestLocal= new ThreadLocal<HttpServletRequest>();  
  5.       
  6.   
  7.     public static HttpServletRequest getRequest() {  
  8.          return (HttpServletRequest)requestLocal.get();    
  9.     }  
  10.   
  11.     public static void setRequest(HttpServletRequest request) {  
  12.           requestLocal.set(request);    
  13.     }  
  14. }  
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class GetRequest implements Filter {  
  2.   
  3.     /** 
  4.      * Default constructor.  
  5.      */  
  6.     public GetRequest() {  
  7.         // TODO Auto-generated constructor stub  
  8.     }  
  9.   
  10.     /** 
  11.      * @see Filter#destroy() 
  12.      */  
  13.     public void destroy() {  
  14.         // TODO Auto-generated method stub  
  15.     }  
  16.   
  17.     /** 
  18.      * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) 
  19.      */  
  20.     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {  
  21.         // TODO Auto-generated method stub  
  22.         // place your code here  
  23.         RequestUtils.setRequest((HttpServletRequest)request);  
  24.         // pass the request along the filter chain  
  25.         chain.doFilter(request, response);  
  26.     }  
  27.   
  28.     /** 
  29.      * @see Filter#init(FilterConfig) 
  30.      */  
  31.     public void init(FilterConfig fConfig) throws ServletException {  
  32.         // TODO Auto-generated method stub  
  33.     }  
  34.   
  35. }  
在web.xml增加如下代码: 

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <filter>  
  2.                 <filter-name>GetRequest</filter-name>  
  3.         <filter-class>com.filter.GetRequest</filter-class>  
  4. </filter>  
  5.     <filter-mapping>  
  6.         <filter-name>GetRequest</filter-name>  
  7.         <url-pattern>/*</url-pattern>  
  8.     </filter-mapping>  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值