spring中普通java类如何存取session、request对象

1、首先要在web.xml增加如下代码:

 
        org.springframework.web.context.request.RequestContextListener
 

2、在需要存的类中获取request以及session对象,并存入值或者对象(DataTransit为我自己创建的类,用来存数据)

 DataTransit dataTransit = new DataTransit();
 dataTransit.setQidianvalue(qidianUtils.qidianMonth(crimeName,res));
 HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
 HttpSession session1 = request.getSession();
 session1.setAttribute(crimeName,dataTransit);

3、在需要取session值的java类中,通过session取到存在里面的对象或者值


HttpServletRequest requestGet = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
HttpSession sessionGet = requestGet.getSession();
DataTransit dataTransit = (DataTransit) sessionGet.getAttribute(zuiming);

至此即完成了在spring中普通java类通过session存取数据。

你可能感兴趣的:(spring中普通java类如何存取session、request对象)