监听session,在其失效时触发事件

来源:互联网 发布:集团管控软件 编辑:程序博客网 时间:2024/06/05 00:09
因为项目中有个需求:记录登录用户的在线时间,开始时间设为用户登录时,结束时间为session失效。1、创建一个监听器MySessionListener implements HttpSessionListener{    @Override    public void sessionCreated(HttpSessionEvent se){}           @Override        public void sessionDestroyed(HttpSessionEvent se){       HttpSession hs=se.getSession();     //TODO    } }2、在web.xml中:<listener>        <listener-class>xxxx.MySessionListener</listener-class></listener>3、如果MySessionListener中有什么Bean是要注入的,那么这些Bean要写在XML中,在MySessionListener中 用 : String configLocation = "classpath:xxx.xml";  ApplicationContext ctx = new ClassPathXmlApplicationContext("configLocation");  //ctx.getBean("beanName");这样,在Session失效后,自动触发 sessionDestroyed();


0 0