6

I want to kill session after browser close or tab close.And I need to do one database connectivity after the session get expires using Session Listener.But, for that I need to wait until the session destroys.

Here is the code which executes when the session destroyed.

public void sessionDestroyed(HttpSessionEvent event) {
        synchronized (this) {
            //System.out.println("deletion");
            ServletContext application = event.getSession().getServletContext();
            sessionCount = (Integer) application.getAttribute("SESSION_COUNT");
            application.setAttribute("SESSION_COUNT", sessionCount=sessionCount - 1);
            //application.setAttribute("SESSION_COUNT", --sessionCount);


            try
            {
                Class.forName("com.mysql.jdbc.Driver");
            }
            catch (ClassNotFoundException e) {
                System.out.println("" + e);
            }

            Connection connection = null;
            try {
                connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/raptor1_5", "root", "");
                Statement st = connection.createStatement();

                st.executeUpdate("update adminlogin set Password='admin' where Username='admin'");  

            }   catch (SQLException e) {
                e.printStackTrace();
            }

        }
        System.out.println("Session Destroyed: " + event.getSession().getId());
        System.out.println("Total Sessions after delete: " + sessionCount);
    }

But, I don't want to wait until the session destroys.I need to do this code after the browser gets close.Hope someone will get me out of this.

Thank you

Anand Kumar
  • 389
  • 1
  • 7
  • 21

2 Answers2

2

If you use jquery you can listen for the unload event

https://api.jquery.com/unload/

or using JS

 window.onbeforeunload = function() {...

Then of couse you could fire some ajax to call the server side code.

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
2

Detect the Browser's Close Event and Invalidate Session using

if(session!=null) { 
session.invalidate();   
}   

How to detect browser Close Event ?

$(document).ready(function()
{
 $(window).bind("beforeunload", function() { 
   return confirm("Do you really want to close?"); // here you can invalidate
 });
});

Update

How to differentiate between Browser Close and Refresh Event ??

Community
  • 1
  • 1
Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
  • Neeraj I tried this code but it also executes for window refresh. I need only for tab close or window close event not for refresh event – Anand Kumar Apr 13 '15 at 07:40
  • Check out Updated answer , Hope that'll help you out !! – Neeraj Jain Apr 13 '15 at 07:48
  • the link which you updated was good, but I can achieve that using session time out. If suppose the user not interact with browser for some minutes means it expires know I don't want that – Anand Kumar Apr 13 '15 at 09:01
  • for.ex: if I give time out for session means I can achieve what I need. But the session not to expires before user invalidate that or close the browser – Anand Kumar Apr 13 '15 at 09:07