This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes Handling session Time out in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Handling session Time out in Java" Watch "Handling session Time out in Java" New topic
Author

Handling session Time out in Java

Manjunath Rajashekar
Greenhorn

Joined: Mar 20, 2012
Posts: 2
Hi,

I need sample code which will handle session time out and forward the control to some page

Thanks ,
Manjunath
milind sonar
Greenhorn

Joined: Mar 15, 2012
Posts: 1
add this code at starting of your code and add your code in else part.hope it will work...

if(request.getSession().getAttribute("userId") == null)
{
request.getSession().invalidate();
return mapping.findForward("logOut");
}else{

}
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 503

milind sonar wrote:add this code at starting of your code and add your code in else part.hope it will work...

if(request.getSession().getAttribute("userId") == null)
{
request.getSession().invalidate();
return mapping.findForward("logOut");
}else{

}


For this to work, first on session creation, you need to set the userId attribute into session. Also, not sure, whether the OP is using struts( OP might be using other framework or no framework only), so suggesting a struts code is not a good idea.

The solution which should work for the OP problem, is use a listener and implement HTTPSessionListener and override sessionDestroyed method.

But, forwarding to a page once is session is expired is not a good idea. We had a discussion on this few days back here.
In the site, please read Rob's response to understand why this is considered bad.

P:S: Please ShowSomeEffort and javaranch Is NotACodeMill

Regards, Prasad
SCJP 5 (93%)
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Handling session Time out in Java
 
Similar Threads
How to introduce Time Out feature in web application
increase servlet timeout period
Firefox window open on unload
Kill the Session
Session Problem