Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

trace the browser window closing in a servlet/jsp

 
Ven Ram
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Is there any way to trace the browser window closing in a servlet/jsp? If some one clicks on the x of the browser, How can we trace ?

Please help me.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSP/Servlet == server side. Browser events == client side. So no. The only thing a Servlet (and hence a JSP) in its typical context is going to know about is an HTTP request. Since closing the browser doesn't make any request to the server, not message is passed to the Servlet. You need JavaScript for browser events.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and even then there's no guarantee.

Not all ways in which a browser window can be closed will result in a Javascript event being fired.

If there were you could in a Javascript event handler do a request to the server which would launch some cleanup code or whatever, but this isn't reliable (as we found out, one of our marketing people had thought up a requirement to do exactly that, we had to tell him it wasn't possible and to train the users to use the logout button instead).
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no way to detect when the browser window is closed...onunload is the closest you are going to get to it.

Just a few points to ponder as you develop this application:
You need to consider that the onunload gets called when the page is closed, user navigates away by link or typing in the url, and refreshing the page. So if you need to distinguish between these, then you will have some difficulty.

Also the onunload event will not fire if user gets disconnected from internet, user's browser freezes, or my favorite reason "If JavaScript is disabled by the user"

That is why I really on my session timing out on the server to clean up the mess.

Eric Pascarello
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic