views:

67

answers:

4

Our application logs off after 30 min and gets redirected to login page,i am specifying session timeout in web.xml and using a requestProcessor for redirecting.I want to show to the user a message saying your session got expired once the session expires,how can i do that.Auto log off ? I wuold like to prompt the error message on the page"The session is timout, please login again" . Then how could I detect the session is timeout? will any methods trigger automatically?

A: 

If you're using servlet sessions, you can check to see if the session the jsp / servlet is returning is new using the isNew() method. If yes, then the user's session has expired and you can display the relevant messages.

futureelite7
user session expire message has to be shown only after the time out ,in your case it will display even after logout ,which i dont need
sarah
+1  A: 

Create a Listener class implementing HttpSessionListener and define it in web.xml

This will notify you when any session is destroyed. Use the sessionDestroyed() method.

See a full example here:

http://www.mkyong.com/servlet/a-simple-httpsessionlistener-example-active-sessions-counter/

JoseK
The above solution wont force browser to show a dialog.
Script Runner
The above solution will force the application to prompt the message even after logout,it will not check expiry time say 30 mins and show the message
sarah
A: 

Include a javascript utility function inside your JSP and ping the server every 31 minutes. The above mentioned utility function should be using setTimeout() JS function internally.

setTimeout ( "checkServerSession()",  /* intervalInMilliSeconds */ 31000);

Note that

checkServerSession()

is a regular JS function which may fire HTTP requests. If the request is successful session exists otherwise show the prompt to the user.

Script Runner
Let me know an example ,how can this be functioned
sarah
A: 

You can use Servlet's HttpSession#getMaxInactiveInterval() to get the remaining lifetime of the current session and you can use JavaScript's setTimeout() to timeout a function callback.

1 + 1 = 2, so here's a basic example which needs to be placed inside a JSP (so that the EL, those ${} things, can do its work):

<script>
    setTimeout(function() {
        alert('Your session is timed out!'); // Display alert.
        window.location = 'logout.jsp'; // Then redirect to logout page.
    }, ${pageContext.session.maxInactiveInterval} * 1000); // It returns seconds, but setTimeout expect milliseconds.
</script>
BalusC
I want the alert message to be displayed after 30mins,how can i alter the above script,i have defined the session time out in web.xml
sarah
This Script shows the pop up even if the application is in use.
sarah
That may indeed happen when you've multiple windows open in the same session. It was a basic kickoff example. You'll need to go some steps further. Fire ajaxical polls to the server side to keep the session alive as long as the user is active. You can find an example in [this other answer of me](http://stackoverflow.com/questions/2319020/mvc-with-jquery-handling-session-expire/2319211#2319211).
BalusC
Is it a Jquery approach?
sarah
Not specifically. You can also use plain JS for it (jQuery itself is also "just" JavaScript). It's only going to be more code.
BalusC
Do u have any script for it?I am not aware of ajax so not getting it clearly
sarah
Just add `<script src="http://code.jquery.com/jquery-latest.min.js"></script>` to your HTML `<head>` and all jQuery code will work. They also have tutorials at their homepage http://jquery.com.
BalusC
Not able to resolve this issue
sarah