Link to home
Start Free TrialLog in
Avatar of matthew016
matthew016Flag for Belgium

asked on

Session expired

Hi,

When a session expires,
I want to display an alert message "Session Expired" (or maybe you have a better idea ?)
And then forward the user to the main page.

The problem is that in my code I don't know how I can check if a session expired.
For example he stays on the same page during mre than 30 minutes, then he clicks on a button calling some action, there will be errors because I am trying to read information in the session.

Thank you for any help.
Avatar of Bart Cremers
Bart Cremers
Flag of Belgium image

You can add some JavaScript to all your pages:

function alertSessionTimeout() {
   var delayInSeconds = <%=session.getMaxInactiveInterval() %>;
   var delay = delayInSeconds * 1000;
   self.setTimeout('showSessionAlert()', delay);
}

function showSessionAlert() {
    var msg = 'Session Expired'
    alert(msg);
    self.location = '/';
}
Avatar of matthew016

ASKER

The problem is in the code,
it will have exceptions at runtime.
SOLUTION
Avatar of Bart Cremers
Bart Cremers
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mnrz
mnrz

You can also  use a listener HttpSessionListener
it has two methods

sessionCreated(HttpSessionEvent e)
and
sessionDestroyed(HttpSessionEvent e)

so just before a session is going to destroy you can set an attribute in servlet context


SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The only good suggestion is the filter,
but I assist others for the effort of course