If you are working in a web based project, the term “session” must be a frequently used one. “I want to implement a HttpSessionListener”, “How do I access http session?”, “Should i store my user state in http session or in a session bean?”.
But I just wondered, how to implement that in a swing application? (Sorry, i haven’t learn swing application framework yet)
Consider this typical scenario:
A user has logged into the swing application and has opened some 2 or three windows (JFrames). Now the user goes somewhere leaving the system idle.
How can we logout the user automatically here (for security reasons)?
How to close the opened resources?
I initially thought of writing a global event listener and implementing that in all the user interface classes. But that looked like a bad idea for me, because that would involve a lot of code changes.
So I searched through the net and found out a solution from a java forum (thanks camickr).
In contrast to my expectations, it was quite simple though. And here it is:
private void trackSystemEvents()
{
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener()
{
public void eventDispatched(AWTEvent event)
{
String eventText = event.toString();
if(eventText.indexOf(“PRESSED”) != -1 || eventText.indexOf(“RELEASED”) != -1)
{
SessionMonitor.getInstance().setLastActionTime(System.currentTimeMillis());
}
}
}, AWTEvent.MOUSE_EVENT_MASK + AWTEvent.KEY_EVENT_MASK);
}
Here, i want to track the user activity through a “global” listener kind of thing. And here the event listener is registered with AWTEvent.MOUSE_EVENT_MASK and AWTEvent.KEY_EVENT_MASK. That means, only mouse events and key events shall be tracked.
And i don’t want to track the mouse moved/entered events and that’s why the small “if” condition which checks only for “pressed/released” actions like key pressed, mouse released etc.
This will help you in implementing the logic to track session timeout.
And spice it up with “observer” pattern to notify your user interfaces to close the opened resources.
thanks this helped alot i am writing a kiosk program and when theres no activity for a while i can now return to the main menu.
Comment by Jetlag — February 3, 2008 @ 1:40 pm |
Hi
I have one question …
From where will i get the jar containing “SessionMonitor” class ?
Comment by Abhay — March 20, 2008 @ 8:06 am |
Hi Abhay,
You need to write the SessionMonitor yourself. It’s just a singleton class with few methods buddy. If you want further information, I can help you with some sample code.
Comment by James Selvakumar — March 20, 2008 @ 8:19 am |
Hi,
I want to store the details of user login in the home page and on subsequent pages, I want to use those values. To be clear, I am authenticating an user with user name and password in an Java Swing application and on successful authentication, I have to store the user name and password in session. If I want the details of user name and password, I have to retrieve that from the session. How I can achieve that in Java Swing. If you have any sample code, send me to elamaran24@gmail.com.
Thanks a Lot.
Elamaran Krishnamurthy
Comment by Elamaran — January 5, 2009 @ 10:32 am |
Hi Elamaran,
There are many ways to do it.
The simplest possible way is to store it as a system property. For example,
System.setProperty("application.userName", myUserName);Then you can fetch it anywhere from your application very easily. For example, you can use
System.getProperty("application.userName");to retrieve the user name.Another option is to store it in a hash map or value object and setting that object in the same way as above.
But why would you need to store the password of the user in the system? It is not advisable.
Comment by James — January 12, 2009 @ 2:37 am |
Thanks James…
I am going to use only the user name and not the password. In the same way I am going to store some values in the session for future retrieval. Thanks for the info. It will be very useful for my application development.
Elamaran Krishnamurthy
Comment by Elamaran — January 12, 2009 @ 4:12 am |
Thanks, this is exactly what I want to do.
Check for activity in a Java Swing app and log the user out if no activity after a certain amount of time
How do I implement this if I have multiple windows open?
Do I have a superclass containing the above code which my jframes will extends?
Can you also give me sample code for SessionMonitor? Does this have some kind of Timer thread which time out if no activity are detected?
Comment by Viet Bui — September 2, 2009 @ 3:47 pm |