關於session監聽瀏覽器關閉伺服器執行destory的證實
阿新 • • 發佈:2019-01-26
session監聽初次訪問的時候會生成session放在cookie裡
瀏覽器關閉 伺服器延遲執行destory
tomcat8+spring4
public class SessionHelpListen implements HttpSessionListener { private final String FORMAT = "yy-MM-dd HH:mm:ss"; public void sessionCreated(HttpSessionEvent sce) { System.out.println("建立session:"+DateUtil.getStringNowTime(new Date(),FORMAT)); WebApplicationContext appctx = WebApplicationContextUtils.getWebApplicationContext(sce.getSession().getServletContext()); SessionTestService asi = (SessionTestService)appctx.getBean("test"); SessionTest st = new SessionTest(); st.setUpdateTime(DateUtil.getStringNowTime(new Date(),FORMAT)); st.setSessionId(sce.getSession().getId()); asi.insertSessionMsg(st); } public void sessionDestroyed(HttpSessionEvent sce) { System.out.println("銷燬session:"+DateUtil.getStringNowTime(new Date(),FORMAT)); WebApplicationContext appctx = WebApplicationContextUtils.getWebApplicationContext(sce.getSession().getServletContext()); SessionTestService asi = (SessionTestService)appctx.getBean("test"); SessionTest st = new SessionTest(); st.setSessionId(sce.getSession().getId()); asi.deleteSessionMsg(st); } }