aspose file tools*
The moose likes Servlets and the fly likes How to end an active session using servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to end an active session using servlets" Watch "How to end an active session using servlets" New topic
Author

How to end an active session using servlets

Abhishek Asthana
Ranch Hand

Joined: Sep 08, 2004
Posts: 146
Hi,
I am making an application for online test. One of the requirements is to end the session of the user must be ended 1 hour after the user logged in.
The time set in web.xml ends a session after the specified time only if the session is INACTIVE.
Please tell me how to kill an active session after fixed time.
Abhishek.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

Don't rely on the session timout. Institute your own timer.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Abhishek Asthana
Ranch Hand

Joined: Sep 08, 2004
Posts: 146
What kind of a timer are you talking? Please specify. And how to make such a timer?
Graham Lewis
Greenhorn

Joined: Sep 25, 2005
Posts: 6
Originally posted by Abhishek Asthana:
What kind of a timer are you talking? Please specify. And how to make such a timer?


Hi

I think what you're looking for is a TimerTask - see the Java API java.util.Timer and java.util.TimerTask - a good example is at: http://javaalmanac.com/egs/java.util/ScheduleLater.html.

Then it's simply a case of doing at the appropriate time - inside the timer. I'll leave how to attach the timer thread to the session for you to work out

Not really for me to ask - but why would you want to kill a client that's been logged on for an hour. Don't think they'd apprechiate it much! What if they are in the middle of something which involves putting data in the session - are you just going to loose that data? Are you going to tell the user what's happened?

Cheers,
Gray
Abhishek Asthana
Ranch Hand

Joined: Sep 08, 2004
Posts: 146
Hey Gray,
Thanks for the help. I should have thought of it!
Ok I am actually making an application for online test. So examinee has to either logout within 1 hour, or we will disconnect him. Sort of snatching your answer sheets after stipulated time!
Hope this makes sense!
Thanks again
Abhishek.
Amol Fuke
Ranch Hand

Joined: Apr 08, 2005
Posts: 129
You can get the time when user logs in by System.getCurrentTime() and then on every page calculate the current time again.If this time is greater than the time when user logs in by one hour then invalidate the session.

Let me know if this approch is right.


"There are no mistakes, only lessons"
Graham Lewis
Greenhorn

Joined: Sep 25, 2005
Posts: 6
Hi Abhishek,
No problem!
I understand what your doing now - still sounds a bit brutal - but we've all sat exams right ;-)
Cheers,
Gray
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Ending the test by killing the session is a BAD BAD BAD idea.

THINK! What would you like to happen in a classroom exam when time is up?

Yes, gather the papers and grade them - you can't do that if the session is expired.

Just note the expiration of the time, stop accepting answers, and force the next output to be the grade result.
Mark the session so that the user cant do anything but proceed to whatever final page you want to show, THEN invalidate the session.

Bill
vinod bonde
Greenhorn

Joined: Oct 03, 2005
Posts: 5
Tell me what kind of test engine u r designing. if u have everything running on the client side u can surely use javascript for this purpose.
if u have frames still u can use javascript. If u r making a trip to server for each section or question then u can think of something request time etc


Bond with the Rest
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by vinod bonde:
Tell me what kind of test engine u r designing. if u have everything running on the client side u can surely use javascript for this purpose.
if u have frames still u can use javascript. If u r making a trip to server for each section or question then u can think of something request time etc


Bad idea, I must say. Never rely on client-side scripting.
[ October 03, 2005: Message edited by: Adeel Ansari ]
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
What if they're in the middle of a page when the session ends? Or is it question by question? You could always impliment ajax to update the answers every time they answer a question and then stop accepting answers after a certain time. Maybe use both the javascript timer and server side validation in this way... worth checking out? What do the rest of you think?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to end an active session using servlets