基于ThreadLocal的HttpSession类

因为在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()

你可能感兴趣的:(threadLocal)