Sargsyan Grigor
Sargsyan Grigor

Reputation: 579

Time tick in JSP without reloading the page

I know it is possible to change time variable using JavaScript or something else, but I try to do it using JSP-s, without reloading the whole page.

To be more specific my code is the following:

<% 
    for(int second = 0; second < 60; i++)
    {
        out.print(second);
        try { Thread.sleep(1000); }
        catch(InterruptedException e) { return; }       
    }

%> 

...and it doesn't tick and show the time passed after every second. Instead, only after 60 seconds it shows that the time is over. So is it possible to show the 'second' variable after every second in JSP or can I use sockets to accomplish this? Thank you.

Upvotes: 1

Views: 93

Answers (2)

Olaf Kock
Olaf Kock

Reputation: 48067

JSP code is executed server-side. So the JSP you implement in your question will take 1 minute to render and then print all the seconds at once. You'll need to utilize Javascript to dynamically update your page once it's loaded by the browser.

Upvotes: 0

Quentin
Quentin

Reputation: 943591

No.

JSP runs on the server and sends the output to the browser.

There is no way to get the output from JSP to change without either:

  • Making a new HTTP request to the server and getting a new response or
  • Moving the logic that updates the content to client-side JavaScript (where you get to run code in the browser and have access to the DOM).

Upvotes: 1

Related Questions