wood burning stoves 2.0*
The moose likes JSF and the fly likes Session time out in JSF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Session time out in JSF" Watch "Session time out in JSF" New topic
Author

Session time out in JSF

Eugene Abarquez
Ranch Hand

Joined: May 18, 2006
Posts: 211
How do you do a session time out in JSF? What I basically wanted to do is if after some time when the user is idle, I have to manually destroy the session and log the user out of the system. I should also be able to set the number of minutes on how long the idle time should be after the session will be destroyed.

Any suggestions will be appreciated. Thanks in advance.


There's so much to learn in this industry, and not everybody has the necessary interest.
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Hi,

You can do it in two ways:

1) The first way: Since your writing your JSF in a JSP page all after all, all JSP have implicit objects, and one of these implict objects is named session, which is of type HttpSession. Take a look at the HttpSession API and you'll find a method named setMaxInactiveIntervalTime() which will help you!

2) The second way is to the same thing programatically, but you have to three more steps in your managed bean, which are:



After this step, you have an HttpSession as you had in your JSP page, so you apply the same steps mentioned above.

For more information, please do not hesitate to ask.

Best of luck ...


Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Eugene Abarquez
Ranch Hand

Joined: May 18, 2006
Posts: 211
Thanks Vassili!

One more thing. I don't know if this is technically possible, but I want to be able to display the number of seconds that the user has left before the session will be destroyed. You know, like a time clock of some sort. So the number of seconds is deducting while the user is idle.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15959
    
  19

JSF doesn't actually manage session timeouts - it just uses the standard J2EE timeout facility. So you can set it in the usual location in your web.xml file. JSF tries not to re-invent things that were already working well enough.

For a session countdown timer, you'll have to fake it. HTTP does not permit unsolicited data transmission, whether it's in Java or any other language/platform. And unless you do some really evil things to your infrastructure, if the client polls for session time remaining (via timed page refresh or JavaScript), the mere act of polling will reset the session time remaining. So about the closest you can get is to run a JavaScript timer process on the user's web browser that starts counting when the page is (re)loaded.


Customer surveys are for companies who didn't pay proper attention to begin with.
Eugene Abarquez
Ranch Hand

Joined: May 18, 2006
Posts: 211
Thanks Tim! I think I got what I needed. :thumb:
Eugene Abarquez
Ranch Hand

Joined: May 18, 2006
Posts: 211
Hi,

Just a follow-up question on this topic. I wanted to redirect the user to another page after the session times out. Is there a way to detect programmatically that the session has expired so I can redirect the user to another page?

Thanks again in advance.

Eugene
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15959
    
  19

Do a getSession(false), i think is it. "False" means return null if no session presently exists.
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
I've set up a servlet filter that does this



MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
ravi vijaykumar
Greenhorn

Joined: Feb 24, 2009
Posts: 4
Hey,

We have implemented similar way of session timeout in our application calling a filter in web.xml.

But we are facing one problem with this way of timeout.

Once Session expires proper logout doesn’t not happen while clicking on tabs created in jspx using <tr:panelTabbed> tags

This is very specifically for the tabs and everything works fine while clicking on link or buttons etc

In server logs we get Session Timed Out message, however at UI nothing happens, same teb is displayed and never logs out.

Additionally we get the below message in logs:-

[3/4/09 16:19:01:125 IST] 00000035 StateManagerI E org.apache.myfaces.trinidadinternal.application.StateManagerImpl restoreView Could not find saved view state for token -1909e0a

Any help or pointers would be appreciated.

Thanks in Advance,
Ravi
Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
You can implement HttpSessionListener and implement sessionDestroyed or created and this will get you both the solutions for time clock and redirecting to the url if session is timeout.

Thanks,
Rahul


Rahul Juneja
ThoughtClicks - http://techlabs.thoughtclicks.com
Brian Davids
Greenhorn

Joined: Dec 21, 2009
Posts: 1
How can you redirect using HttpSessionListener? It does not have access to the response object? Also, a redirect can only be in response to a user's request - which does not necessarily happen the moment the session time out..

Thanks
Marcelo Yamashita
Greenhorn

Joined: Feb 08, 2006
Posts: 2
Hi everybody,

I've implemented the Listener to handle the redirection on exception (http://techieexchange.blogspot.com/2008/02/jsf-session-expiry-timeout-solution.html) and it worked fine.

I know that by the Listener time, I do not have the FacesContext. But I have to inform the user that the session was expired. So it's desired to have a message set in a FacesContext manner.

Any idea on how to set up the Faces message ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session time out in JSF
 
Similar Threads
Maintaining the scope of a session login in struts2 web app
Session in jsp
User Idle Time?
Alerting the user about Session expiry after idle-time
MVC-model 2(Session Management)