Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Handling session Time out in Java

 
Manjunath Rajashekar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Thanks ,
Manjunath
 
milind sonar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 665
4
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic