shashi27
shashi27

Reputation: 199

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?

Upvotes: 0

Views: 311

Answers (1)

Pushkar
Pushkar

Reputation: 7580

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.

Upvotes: 3

Related Questions