• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to end an active session using servlets

 
Abhishek Asthana
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't rely on the session timout. Institute your own timer.
 
Abhishek Asthana
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of a timer are you talking? Please specify. And how to make such a timer?
 
Graham Lewis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Graham Lewis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic