0

I am new to Programming

I searched a lot in online but no one could satisfy my requirement.Here I want to delate data from database when user close the browser window.

Here i found one solution in stack overflow

public class YourHttpSessionListener implements HttpSessionListener {       
public void sessionCreated(HttpSessionEvent event) {
//put row in the database
}

public void sessionDestroyed(HttpSessionEvent event) {
 //delete the row from database  
 System.out.println("entered in to listener class");   
}
}  

and I add in web.xml these lines

<listener>
<listener-class>YourHttpSessionListener</listener-class>
</listener>

Here i don't use any ajax call or some else.But here the sessionDestroyed () is not called when closing the browser.Here i didn't create any sessions.

kondapaka
  • 132
  • 1
  • 10
  • There is no reliable way to detect if your user has closed the browser window. You cannot rely on Javascript events since javascript might not be enabled. You will have to find another way to do whatever it is you want to do. –  Sep 06 '13 at 05:47
  • Is it possible if i use any javascript or ajax functions – kondapaka Sep 06 '13 at 05:49
  • Read my comment again. –  Sep 06 '13 at 05:50

2 Answers2

1

Ultimately, you can't. A better alternative is to write a process that deletes data after a period of user inactivity.

Rocklan
  • 7,888
  • 3
  • 34
  • 49
1

You are asking the wrong question. The ultimate question should b: Why do you store temporary data in the database? surely, you can do it in a way which does not store anything in the database but keep it in the memory for example

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89