aspose file tools*
The moose likes JSP and the fly likes javascript and session info Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "javascript and session info" Watch "javascript and session info" New topic
Author

javascript and session info

sudeshna de
Greenhorn

Joined: Feb 28, 2006
Posts: 4
i want to find out whether my session has timed out or not by using java script.and finally how much time is left for timeout and that i want to show as an alert.please help me with some code if possible..
[ March 01, 2006: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Sorry. It is not possible.

Also, please be sure to use descriptive subject titles. I have taken the opportunity to change this one for you.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Well its not directly possible, but you could do something like this on every page.

<script language="javascript">
var sessionTimeout = new Date( new Date().valueOf() + <%= session.getMaxInactiveInterval() %>);
</script>

You now have a javascript variable representing the date/time when the session on the server will expire.
You can use the javascript window.setTimeout or window.setInterval methods to call javascript after a certain amount of delay.
I'm sure you can figure out the rest from here.
[ March 02, 2006: Message edited by: Stefan Evans ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Except that the act of polling the session in this way resets the session timeout, making it ineffective for determining how much time is left on the session timeout.

I assumed that the OP was more interested in determining the amount of time remaining in the session as a result of user events on the page rather than at page load time when the answer will always be the length of the session timeout itself.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Sorry, guess I didn't make it clear.
Agreed: any access to a page resets the session timeout.

What I am suggesting is that you load into a javascript variable, the info you need to predict approximately when the session will time out
That being the maxInactiveInterval of the session.

Given this value in a javascript variable, you can perform javascript calculations as required. eg


If the user does not navigate off this page, it should call the javascript alertSessionExpiring() method approximately a minute before the session would expire. The user can then take action to keep their session alive (ie make a request)
[ March 02, 2006: Message edited by: Stefan Evans ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: javascript and session info