简体   繁体   中英

How can I remove session related information from database when the session gets killed?

I have web application as soon as a user logs in the session id, username and timestamp gets inserted to database.

But when the browser crashes or the session is killed the entry in the Db remains. So How to remove this entry in the database?

You will have to define a HttpSessionListener

<listener>
     <listener-class>mypackage.MySessionListener</listener-class>
</listener>

The listener code is called whenever a session is created and destroyed.

package mypackage;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Date;


public class MyHttpSessionListener implements HttpSessionListener
{
    public void sessionCreated(HttpSessionEvent se)
    {
           // DO CREATION LOGIC
    }

    public void sessionDestroyed(HttpSessionEvent se)
    {

             HttpSession session = se.getSession();
           // PERFORM DELETION LOGIC
    }
}

Your listener class has to implement the HttpSessionListener interface. Rest is self explanatory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM