使用HttpSessionListener
@WebListener
public class LogoutListener implements HttpSessionListener {
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
// I don't know which user you are logging out here (you probably want to get some data from session)
LogoutBean lgub = new LogoutBean();
LogoutDao lgud = new LogoutDao();
// don't need to check if session is null (it obviously isn't at this point, it's being destroyed)
lgud.logoutUser(lgub);
}
// sessionCreated() goes here
}
但是请注意,当会话超时时,这并不能保证立即发生。它可以在以后的任何时间发生。这取决于一些预定的 servlet 容器线程。
您可以使用 Servlet 3.0@WebListener
或web.xml
作为
<listener>
<listener-class>your.domain.listeners.LogoutListener</listener-class>
</listener>