Adding a timer in JSP pages

How can I add a timer/countdown to a main JSP page? There are other jsp pages linked to this main jsp page. When users closed these other pages, they will return to the main JSP page. How can I use session or it there a better way?

Who is Participating?
Siva Prasanna KumarConnect With a Mentor Principal Solutions ArchitectCommented:
i don't think there is a need to make things complex first of all set a session varable to 2 hours and go on decreasing this varivbale every minute and display the same varibale to the user and finally as this variable is in the session as soon as it becomes zero stop the task or say time out and redirect to some other page.

Thank You.
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
what exactly you want to do by using the timer.
rospccAuthor Commented:
The users will be asked to do a task and they are given 2 hours duration to complete the task. I want the timer to let them know how many minutes they have left to complete the task.

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

YoungBonziConnect With a Mentor Commented:
You could do a meta refresh:  <meta http-equiv="Refresh" content="7200;url=/mainpage.jsp">

Put the following line of JavaScript into the <head> of the page:
window.setTimeout('window.location.href = \'insert main page's URL here\';', 3600000);

It sets a timer on the page that will fire the action (the first argument) when the timer limit (the second argument - in milliseconds) is reached.

Look at how Brainbench , , do it. Sign up for a free test and have a look at the HTML.

They use a client-side javascript to set the timer and auto submit the page. To ensure that this is not being abused by your user. You will need to set a value in the session that sets the time when the form page is downloaded.

long t0 = System.currentTimeMillis() ;
session.setAttribute("StartTime" , t0 );

And in the form handler jsp you compare this value to check that the value is not greater that 30 mins (plus some seconds for the download time)

long t1 = System.currentTimeMillis() ;
long t0 = ( (Long)session.getAttribute("StartTime") ).longValue();
//now check difference between start of test and end
if ( (t1 - t0 ) < 1000 * 60 * 31)//check for 31 minutes to allow for network issues
//do your process
//longer than 31 minutes

but "you're taking [a terrible security risk] by putting the timing mechanism on the client side. If you are really worried about timing someone out, do it server side, not client side. Someone could just turn JavaScript off, and then you'd have no control over the Timing aspect of your application." ~
But if you use the meta refresh method, when the mainpage.jsp loads again, you can have your code update your database with a timestamp, or some other indicator, that will be checked for if the user tries to run the test again.
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
I think you will be wondering how to do it in all pages just try writing a include page which just retrieves the value of this timer varibale from the session and then goes  on decrementing and when it time out goes to a new jsp file.

And include it in all the jsp's.

Thank You.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.