Building an AJAX Application (9): Client-side Session Notification
(by Mark Qian 10/31/2006)
Introduction
Session timeout client-side notification has always been an issue in web development.
Many applications just make it in a "user-self-service" manner. That is, users have no
idea about whether the http session is timeout until they make their first submission after
the session timeout. Usability is low in this way: users may enter a lot of data
without realizing the session is timeout and may lose all the data they enter when submiting
in case the form is not cached locally.
With AJAX+Javascript, Session timeout notification is made easy. The result of my approach
is that almost "as soon as" server session timeout, browser will pop up a message alert to indicate
what is going on in the server. It is done in a "in parallel timing" style. That is, client
and server counting timeout in parallel while client wait a little bit longer. When client side
timeout browser sends an AJAX request to server to verify if the session is over. Any client side
submission will reset the client side counter just like web container does at the server side.
In this low overhead way, no client polling nor server pushing, only one verification request
is needed per HTTPSession timeout.
Here are some key steps you may want to pay attention to when you read the source codes:
- read session timeout from web.xml and write into javascript to be used as
timeout period in JSP
- provide a server side session timeout checker
- launch a timeout thread in Javascript with given period (a little bit longer)
from web.xml on page load
- when javascript timeout is trigged, make an AJAX call to the session timeout checker
- handle the timeout
Demo
This is a shopping page where session timeout is set to 2 minutes. Please read the "Introduction"
in the popped up page to learn what to try.
|