获取session方法1:
在controller层,可以直接注入,如果需要在service层获取,就可以直接将session传入进去
public JsonResult login(String username, String password, HttpSession httpSession) {
System.out.println(httpSession);
获取session方法2:
使用监听器的方式
在web.xml中配置
<!-- 用于管理请求相关内容的监听器,只有配置了这个监听器才能使用RequestContextHolder -->
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
原理:RequestContextListener实现了ServletRequestListener接口(就和我们自定义监听器一样),RequestContextListener使用了RequestContextHolder(利用ThreadLocal将reqeust对象存储起来)