2

ZK Session Timeout

asked 2013-01-23 09:08:12 +0800

Brand gravatar image Brand
32 1

updated 2013-01-24 01:10:08 +0800

jumperchen gravatar image jumperchen
3909 2 8
http://jumperchen.blogspo... ZK Team

Hi guys

I have a table "UserLoginHistory" in my Sql Database. When the user logs in to the web app it saves the log-in date for that user in the table. When the user clicks on the "Logout" button in the webapp, it fetches that same record and sets the log-out date and time.

My problem is i need to do the same thing for when the user logs out, but when the session expires(30 minutes). How would i call a method in one of my controller classes to update the database when the session expires.

Any help is appreciated. Thanks!

delete flag offensive retag edit

3 Answers

Sort by ยป oldest newest most voted
4

answered 2013-01-23 09:23:17 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

When user click logout directly, the event is triggered by the user action, so you can do the db-log by controller. If you intent to listen to session-timeout, you have to write a HttpSessionListener and access the database in the listener lifecycle method.

link publish delete flag offensive edit

Comments

Hi Dennis

Thanks for the info. Just tried it but realized I wont be able to do it as the user who is logged in is saved as a session attribute. When the sessionDestroyed method is called, all attributes are removed, so I could not get the user from the session.

Brand ( 2013-01-23 11:01:38 +0800 )edit

about no-attr in session, maybe this help. Maybe you could try to put your listener at very begin of the web.xml

dennis ( 2013-01-24 02:30:01 +0800 )edit
2

answered 2013-01-23 18:47:08 +0800

rickcr gravatar image rickcr
704 7

updated 2013-01-23 18:47:43 +0800

What you want to use here is a class that implements HttpSessionBindingListener.( For example maybe your User class have it implement the interface.)

Assuming your user was put into the session, when it's about to be removed from the session because the session is going to expire the valueUnbound(HttpSessionBindingEvent event) is called, so you would implement this method in your User class.

and you'd do something like ...

public void valueUnbound(HttpSessionBindingEvent event) {
   User user  = (User)event.getValue();
   someServiceClass.recordTheUserWhosSessionExpired(user);
}
link publish delete flag offensive edit
0

answered 2013-01-24 04:26:25 +0800

rdgrimes gravatar image rdgrimes
735 7

Another option is to keep your own copy of Sessions (a minimalistic copy), and have a thread that periodically checks it against the live sessions in the Sessions object that the ZK framework is managing. Any session that is not found, you can assume it expired or was abandoned, and record it as such. In this way, you don't have to worry about catching an expired session before ZK wipes out its session data.

I would also wire in a Runtime.addShutdownHook to make sure you run the sessions check before the container shuts down.

Ron

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
2 followers

RSS

Stats

Asked: 2013-01-23 09:08:12 +0800

Seen: 96 times

Last updated: Jan 24 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More