• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8567
  • Last Modified:

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?

Thanks
0
rospcc
Asked:
rospcc
  • 3
  • 2
2 Solutions
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
what exactly you want to do by using the timer.
0
 
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.

0
 
YoungBonziCommented:
You could do a meta refresh:  <meta http-equiv="Refresh" content="7200;url=/mainpage.jsp">

or
-----------------------------------------
http://www.experts-exchange.com/Web/Web_Languages/Q_20525939.html
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.
-----------------------------------------


or
-----------------------------------------
http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_21323834.html
Look at how Brainbench , http://www.brainbench.com/xml/bb/mybrainbench/mybrainbench.xml , 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)

e.g.
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
else{
//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." ~ http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_20445945.html
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
YoungBonziCommented:
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.
0
 
Siva Prasanna KumarPrincipal 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.
0
 
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now