• 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 can I catch session timeout event?

 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to catch that event and I want to direct the user to login page.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can know when a session expires via a listener.

But you cannot direct to anywhere when this occurs. There's no request context. In fact, how would you know whether the user is still looking at your pages, or off looking at videos on YouTube?

What you can do is to write a filter that checks, for each request, whether the session is still active, and if not, redirect to the login.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My requirement is , whenever user session expires whether through explicitly log off, automatic session timeout or browser (browser tab) is closed , I need to make db entry so that later on admin can see the login activities of various users.

I do not know how but I have seen a popup (Javascript alert or html) comes up on various websites (AT&T, Bank Of America), when my session expires.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the listener to make the DB entry because it doesn't matter what the user is doing at that point.

The sites you mention probably guess when the session will expire and use a JavaScript timer. It's hard to know for sure when the session will expire because polling to assess its condition resets the counter.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The sites you mention probably guess when the session will expire and use a JavaScript timer. It's hard to know for sure when the session will expire because polling to assess its condition resets the counter.



I do not understand how JavaScript can be helpful here. Because JS executes on client side. Let's say I load it with 30 minutes but I have to reset the counter every time I browse through application and how can I manage/capture that?

This functionality is really important for us. Client is mad at us. ;-)
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JavaScript is all you have. If you send any kind of request back to the server, be it Ajax or not, the session counter resets so you can't use that technique to ask the server how much time is left.

When the page is generated to send to the client, you can create a JavaScript timer that's the duration of the session timeout.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:JavaScript is all you have. If you send any kind of request back to the server, be it Ajax or not, the session counter resets so you can't use that technique to ask the server how much time is left.

When the page is generated to send to the client, you can create a JavaScript timer that's the duration of the session timeout.



And, if you're using AJAX, you'll want to reset the timer any time an AJAX request is made to the server.

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys. I follow what you are saying.

If application is opened in multiple tabs, does JavaScript gets loaded individually for each tab or only one time?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each tab is an island unto itself.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each tab will load the JS and hence each tab will have its own counter.

Then JavaScript solution will not work, right?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not?

How do browser tabs affect this?
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case I open my application in multiple tabs. JavaScript gets loaded for each tab you have mentioned above. So whatever JavaScript I will write, will be loaded in each tab. So if I am working one tab its counter will get reset as soon as page loads or AJAX, but the counter will not reset for other tabs (because no page submit/AJAX) and will time me out. I hope I am understanding properly here.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you taking about browser tabs? Or something else?

How are you controlling how many browser tabs are opened? That's really us to the user.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Are you taking about browser tabs? Or something else?

How are you controlling how many browser tabs are opened? That's really us to the user.



Yes, I am talking about browser tabs. One FireFox instance and multiple tabs with same application. See the screenshot.

Here I have opened JavaRanch in multiple tabs.

And I do not want to control how many browser tabs they have opened but what I am assuming , that this JavaScript technique will fail if application is opened in multiple tabs.

MultipleTabsJavaRanch.JPG
[Thumbnail for MultipleTabsJavaRanch.JPG]
MultipleTabsJavaRanch
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So your users do this? Or are you spreading your app out over many windows with window.open()?

In any case, as each tab has it's own timer and is loaded correctly, what's the issue?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the OP is asking about opening a tab (counter starts at 30 minutes). A new tab is opened 15 minutes later and is in use. The first tab will time out in 15 more minutes and throw up even though the second tab is still active and the session isn't really over.

What if the JavaScript timeout was sent to something slightly *greater* than the server side timeout? At the end of the timeout an Ajax request is made; if the server session actually *has* timed out, the Ajax request could return a specific error code, and the JavaScript can throw up. If the server session *hasn't* timed out, all goes on as normal and nothing else happens.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:So your users do this? Or are you spreading your app out over many windows with window.open()?


Both are practical scenarios so they can occur. For example, for first one, You moderate JSP and Servlet forums and just for *convenience* you open both forum in separate tabs so that you do not need to switch between forums. and for seond one, let say some user has used a link in the post and if you click that it opens up in new tab.

In any case, as each tab has it's own timer and is loaded correctly, what's the issue?


David explained the issue precisely.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:I think the OP is asking about opening a tab (counter starts at 30 minutes). A new tab is opened 15 minutes later and is in use. The first tab will time out in 15 more minutes and throw up even though the second tab is still active and the session isn't really over.


Exactly

David Newton wrote:What if the JavaScript timeout was sent to something slightly *greater* than the server side timeout? At the end of the timeout an Ajax request is made; if the server session actually *has* timed out, the Ajax request could return a specific error code, and the JavaScript can throw up. If the server session *hasn't* timed out, all goes on as normal and nothing else happens.



If JavaScript sends an AJAX request to server, you are actually telling the server to keep the session alive,right? Because for server it is a request actually like an other request.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup. Can't use Ajax.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does that mean this *thing* is not possible? C'mon guys, there got be some solution or atleast workaround.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the multi-window/tab problem (remember, most people will open new pages in new windows, not tabs) really a problem? Do have hard evidence that people will open multiple windows onto your app, and then switch between them enough for this to be an issue?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a solution but it's a bit of work.

Create a map and bind it to context scope with a context listnener.
Create a session listener that binds new sessions to this map when they are created.
Make sure it also removes them when they are invalidated or destroyed or you'll have all kinds of weird issues.
Use the session's ID as the key.

You now have a way to access sessions, by ID, without using getSession (which would reset the session's timer)
Create a servlet that reads the accesses the session for this user via the context scoped map.
This servlet can read the getMaxInactiveInterval and the getLastAccessedTime to figure out how much time is remaining for this session.

You can now make requests to this servlet with an AJAX function to see if the session is active and how much time is remaining before it times out.

I do this in one of my apps.
On the client side, I have a JS counter that shows in a task bar like div at the bottom of the page.
It refreshes itself every second.
Every 60 seconds, it syncs up with the server via an AJAX call.
When the timer reaches 0 it makes one last call to the server to make sure that the session is actually dead and then redirects the user to the login page.

When the counter gets within 3 minutes of timing out, I open a div popup warning the user that their session is about to expire.
At this point I also have the timer start writing the time remaining to the page title every second. This insures that, if they're working in another window, they can see the timer number in the task bar tab (for windows and Linux users anyway).

Note, this will not work if you have a cluster of servers (unless you can find a way to synchronize the context scoped map).



 
My, my, aren't you a big fella. Here, have a 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