作者:xyzroundo
創建一個類:
public class SystemContext {
private static ThreadLocal webSession = new ThreadLocal();
public static HttpSession getWebSession() {
HttpSession session = (HttpSession) webSession.get();
return session;
}
public static void setWebSession(HttpSession session) {
webSession.set(session);
}
}
再寫一過濾器,將session放到以上的類對象裡
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
SystemContext.setWebSession(httpRequest.getSession());
FilterChain chain) throws IOException, ServletException {
SystemContext.setWebSession(httpRequest.getSession());
}
這樣在service類中就可以使用SystemContext.getWebSession()來取得session了。