• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

trace the browser window closing in a servlet/jsp

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic