1

I working with JSP.
I need run a method in ever session closing, i.e., whenever the browser is closed.
how do I make this?

HagaHood
  • 199
  • 2
  • 9

2 Answers2

1

On server-side you can register an HttpSessionListener:

package com.example
public class MySessionListener implements HttpSessionListener {

    public void sessionCreated(HttpSessionEvent event) {
        System.out.println("Session created");
    }

    public void sessionDestroyed(HttpSessionEvent event) {
        System.out.println("Session destroyed");
    }
}

To register you can either add @WebListener annotation to the listner's class or add the listener to web.xml

<listener>
    <listener-class>com.example.MySessionListener</listener-class>
</listener>

The listener won't be called in the moment when the browser is closed it will be called when the session is timed out.

Neothorn
  • 329
  • 1
  • 7
0

You have to detect the browser close event in javascript.see Trying to detect browser close event to detect browser close event

Send ajax call to serverside which will invalidate the session

Community
  • 1
  • 1
M Sach
  • 33,416
  • 76
  • 221
  • 314