• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to determine if a session has expired...  RSS feed

 
SAFROLE YUTANI
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a very simple JSP below....


I am trying to confirm that session expiration can be caught in the code using getLastAccessedTime(), but it doesnt seem to work right. I have set the max interval to 60 seconds before the container invalidates the session. I am checking for a maximum of 5000 milliseconds between session requests. If this time has been compromised, then I print "The session has expired". The strange thing is that when the value of "Last" is printed to the browser, it has the same value as System.currentTimeMillis(). That means session.getLastAccessedTime() returns the last time that the session was requested from the client, not (last - 1), which is what you really need to calculate the time between the last request and the current request.
If you let the container invalidate the session, then you have no way to determine if the user is accessing a page with no session context, or, a session that has expired, which is what I am trying to do. Any ideas?
SAF

[This message has been edited by SAFROLE YUTANI (edited December 04, 2001).]
[This message has been edited by SAFROLE YUTANI (edited December 04, 2001).]
 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
theoretically I suppose that is an interesting question, but what are you trying to do, actually?
Adam
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a SessionBindingListener -- on valueBound() and valueUnbound() it will perform an action -- this one simply prints a message in the Application Server window:

------------------
- Jessica Bradley
HP Bluestone
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do u want to see this out put ??
Sys=1007562599734
Last=1007562590812
The session has expired

~Kumar
 
SAFROLE YUTANI
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam,
When a page is accessed by the user from the context of a session that has not yet been invalidated by the container, I need to calculate the time between the last request (not the current request, the one before it) and the current time. If the time is more than a time that I have previously set, say 10 seconds, then I know that the user's session has expired and I can redirect them back the login page.
Jessica,
Using a listener would help only if an object is bound or unbound on the session. What happens when the user accesses a page from the context of a session but does not manipulate the session in any way? Then your listener will not fire any events, and as a result, you cannot determine using valueBound() and valueUnBound() that the user has accessed the session. Is there a listener interface that allows you to catch events when a session is accessed?
In my opinion, getLastAccessedTime() is completely worthless. You can only call getLastAccessedTime() from the session itself, but to get the session you must first access it from the request object, and once you access it, the "last accessed time" is update to the current time. The "last accessed time" should return (last - 1)! Am I an idiot to think that?
thanks guys, and girls :-)
SAF
 
SAFROLE YUTANI
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kumar,
Yes! That is what I would like to see, but I dont. The values of 'sys' and 'last' are always equal.
SAF
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, just attach a Long object created from the last Accessed time when the session is first created - every time the session is accessed you can get the old value and compute the difference, then replace with the new value.
Bill
 
SAFROLE YUTANI
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill, that's definitely a good work-around for the problem. I just tested it out and she worked like a charm. Thanks!
SAF
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!