How to call a method before the session object is destroyed?

后端 未结 1 507
难免孤独
难免孤独 2020-12-01 09:33

When developing a JSP application it\'s possible to define a session timeout value, say 30 minutes.

After that timeout, the session object is destroyed. Moreover I

相关标签:
1条回答
  • 2020-12-01 10:15

    Yes, that's possible. You could use HttpSessionListener and do the job in sessionDestroyed() method,

    @WebListener
    public class MyHttpSessionListener implements HttpSessionListener {
    
        @Override
        public void sessionDestroyed(HttpSessionEvent event) {
            // Do here the job.
        }
    
        // ...
    }
    

    Or you could let the complex object which is been stored as a session attribute implement the HttpSessionBindingListener and do the job in valueUnbound() method.

    public class YourComplexObject implements HttpSessionBindingListener {
    
        @Override
        public void valueUnbound(HttpSessionBindingEvent event) {
            // Do here the job.
        }
    
        // ...
    }
    

    It will be called whenever the object is to be removed from the session (either explicitly by HttpSession#removeAttribute() or by an invalidation/expire of the session).

    0 讨论(0)
提交回复
热议问题