Adding a timer in JSP pages

Posted on 2006-05-28
Last Modified: 2008-01-09
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?

Question by:rospcc
    LVL 23

    Expert Comment

    what exactly you want to do by using the timer.

    Author Comment

    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.

    LVL 8

    Assisted Solution

    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." ~
    LVL 8

    Expert Comment

    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.
    LVL 23

    Accepted Solution

    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.
    LVL 23

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    If you're not part of the solution, you're part of the problem.   Tips on how to secure IoT devices, even the dumbest ones, so they can't be used as part of a DDoS botnet.  Use PRTG Network Monitor as one of the building blocks, to detect unusual…
    This video discusses moving either the default database or any database to a new volume.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now