因为在spring的AOP里面需要用到HttpSeesion的一些资料,所以建了线程变量来保存HttpSeesion。
(1)建一个filter,主要是将HttpRequest的session拦截保存:
HttpSessionFile.java
public class HttpSessionFile implements Filter {
public static final ThreadLocal<HttpSession> session = new ThreadLocal<HttpSession>();
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
session.set(((HttpServletRequest)req).getSession());
chain.doFilter(req, resp);
}
public void destroy() {
// TODO Auto-generated method stub
}
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
配置web.xml:
<filter>
<filter-name>HttpSessionFile</filter-name>
<filter-class>com.common.HttpSessionFile</filter-class>
</filter>
<!-- 拦截所有请求 -->
<filter-mapping>
<filter-name>HttpSessionFile</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
如何在使用aop使用httpsession呢:
HttpSessionFile.session.get()
本文介绍了一种在Spring AOP中通过线程局部变量(ThreadLocal)保存HttpSession的方法。通过创建过滤器(HttpSessionFile),可以拦截所有的HTTP请求并将HttpSession保存到线程局部变量中,以便后续在AOP中方便地获取。
7501

被折叠的 条评论
为什么被折叠?



