• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to inform the user that his session has expired  RSS feed

 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can any one tell me away to inform the user, when his session has expired. (I mean to redirect him to another page ...etc)

This is the way I have tried it. But it doesn't work.

long current=System.currentTimeMillis();
long last=session.getLastAccessedTime();
long dif=current-last;

if((dif/1000)>session.getMaxInactiveInterval())
{
response.sendRedirect("index2.jsp");
}


Is there any other way of doing this? Or can anyone tell me what's wrong with my code.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a Servlet, you could request for the Session from the request object using the request.getSession(boolean) method.



Its trickier in a jsp because part of the boilerplate code added by the container when converting the jsp to a Servlet consists of providing a Session object (basically the container returns a Session object handle to all jsp code). This is the default behaviour (with an option to turn it off). If you have to do it, use the isNew() method on the session object.



That said, the best place to put repetitive tasks like a session check is in a filter

ram.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I prefer to do this by binding an object to session when the user logs in (userBean). With each hit to the site, I check for the existence of that userBean in session and redirect the user to the login page if it is not there.
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
It works perfectly

Dilshan Edirisuriya
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!