开发者

Countdown timer through multiple jsp pages

Hey, I'm developing an o开发者_JAVA技巧nline test using servlets/jsps and was wondering how to create a timer that counts down from 6 minutes in which the test will end once it hits zero. Is there a timer class in java that can be used?, if so, how would I implement it in the jsp page?

Any help with this would be greatly appreciated.

P.S. The way I implement the timer doesn't solely have to be the Java language. I have no quarrel using javascript but wasn't sure how to implement it into multiple pages without the counter resetting.


You can go this route:

When the test is started, put start time in user's session. Afterwards, every time you display a page dealing with test, make sure you have a common part which will be used across these pages.

At this part, every time a page is clicked, using jsp/servlets, you "push" in remaining time left, and using something like JavaScript countdown, you can elegantly display remaining time without that counter resetting itself.


Whatever triggers the start of the timer, you could (completely in the servlet) create a Thread with a timer in it that also has a shutdown hook for the test.

void myTestCaseThatStartsTimer() {

    // inline class - actually legal in Java
    class TestShutdownHook implements Runnable {
        Test test;
        TestShutdownHook(Test test) { this.test = test; }
        public void run() {
            Thread.sleep(1000 * 60 * 6); // 6 minutes, catch it's InterruptedException...
            test.triggerShutdown();
        }
    }
    Threat hook = new Thread(new TestShutdownHook(this));
    hook.start();

    // rest of test here


}


You could have the counter in JavaScript in the page, and load the next steps of your test using AJAX, in order to avoid refreshing the whole page.

Or you could put a hidden "timerCurrentValue" field in your form, updated every second by the JavaScript time, and submit it at each step of the test, so that the next page restarts the time at this given value.


The simplest solution is to store the timeout value in the user's session. You can then insert a JavaScript timeout into the page, or use a refresh tag in the generated HTML (replace the 0 here with the calculated timeout:

<meta http-equiv="Refresh" CONTENT="0; URL=/timeout.jsp"/>

Of course, this isn't terribly secure. So you'll need to back it up on the server with a check against the timeout for any submitted results. And you'll need to ensure that the user can't simply start a new session.


You'll have to run the timer client side in javascript, since there'd be no way for a jsp page/servlet to notify the client when the timer is up. I'd suggest the following: Start a javascript timer (using setTimeout on page load), when they submit an answer also send the start time of the question so you can calculate how long they have left, if the timer goes off, use javascript to notify them (and the server when they click) that the test has ended.

Java does have a java.util.Timer class, but I don't think it will give the user experience you want, and threading in servlets can be dangerous.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜