• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how does the servlet knows that the client has closed the browser

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
I want the servlet to know that the user has closed the browser. If the user logs out by clicking on log-out button in the page, the servlets can know that the user has logged out(and can invalidate the session). But if the browser is closed by the user without logging out, how can the servlet know that the user has logged out? or in other words i want the session to invalidated whenever the user logs out(which I can do) or whenever the user closes the browser(which the servlet is not able to know).
The servlet maintains a list of users logged in.
Please let me know the solution.
thanks in advance
Tanveer
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
The servlet will never come to know about the browser closing event, may u can pu tsome javascript to check for browser closing and then report it to the servlet...
 
Tanveer Rameez
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
if i report it to the servlet(by javascript calling the servlet with certain parameters on unload event of the browser), the request will also come back and a new browser opens up to display the response of the servlet.(The original window will however close) .
I don't want the new window to be opened. I guess if u send a request ( I guess any reporting to the servlet is a request) to the servlet, a response will come back to the same browser window. If theat browser window is closed by this time, a new browser window opens up.
So can you or anyone else help me. I don't want the response to come back (which forces the browser to open a new window).
thanks
Tanveer
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The solution is to put a timeout on the login session.
Browsers have no way to reliably notify the server, and in any event, if the Browser or client OS crashed (which was fairly common until recently), there would be cases where notification couldn't be relied on anyway.
A lot of websites do provide a "logout" button, but as a user, I can tell you that I often forget it's there and just move onto another website.
Which is another reason why you can't get a good notification. If I jump to a new website OR open another browser window to a new website, the session to the old website is still there - HTTP isn't a connect-to-only-one-server type of service.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like to use a class that implements HttpSessionBindingListener - if you attach one of these to the user's session, it will get called when the session is deleted after it times out - or - if you call the session invalidate method. That way, no matter what happens your servlet eventually gets notified that the user is not active.
There are also other session listeners that some folks prefer to use.
Bill
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By nature, a web server forgets about the user as soon as the current request has been satisfied. Only through the Session API can the next request from that user be identified. Although a Logout button can be utilized, it is rarely (as mentioned above) used.
If you are using version 2.3 of the JSDK, you can use LifeCycleEvents. Most Web Containers allow you to set a Session Timeout, which invalidates a Session after so much time as passed. By utilizing an HttpSessionListener, you can guarentee you will be notified a Session is about to be destroyed.
While this approach still requires you wait for the Session to time out, it does give you the ability to guarentee the Session is invalidated as you would like it.
I would suggest any resource that can be shared across multiple users/request (Database connections) be used via a pool (DataSource) whenever possible.
 
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic