aspose file tools*
The moose likes JSP and the fly likes How can I catch session timeout event? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How can I catch session timeout event?" Watch "How can I catch session timeout event?" New topic
Author

How can I catch session timeout event?

Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
I want to catch that event and I want to direct the user to login page.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

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

Joined: Aug 16, 2007
Posts: 1374
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

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.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

Yup.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

Each tab is an island unto itself.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Each tab will load the JS and hence each tab will have its own counter.

Then JavaScript solution will not work, right?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

Why not?

How do browser tabs affect this?
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

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

Joined: Aug 16, 2007
Posts: 1374
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.




[Thumbnail for MultipleTabsJavaRanch.JPG]

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

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?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Aug 16, 2007
Posts: 1374
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

Joined: Aug 16, 2007
Posts: 1374
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

Yup. Can't use Ajax.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Does that mean this *thing* is not possible? C'mon guys, there got be some solution or atleast workaround.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

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

Joined: Dec 11, 2004
Posts: 13410

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).



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I catch session timeout event?