Smajl
Smajl

Reputation: 7995

Java - refresh opened html page from application

I have an application that creates a html page from app (I use freemarker). After that, I open the generated webpage from application using Desktop like this:

public void openPage() {
        if (Desktop.isDesktopSupported()) {
            try {
                File file = new File("index.html");
                Desktop.getDesktop().open(file);
            } catch (IOException ex) {
                System.out.println("Error opening a html page.");
                ex.printStackTrace();
            }
        }
    }

Now, my question is: Is there a way to refresh the page from my application? I am changing the concent dynamically and I would like to refresh the page in the browser every few seconds.

Or would it be better to just update the page on background and refresh it directly in the html code using javascript?

Thanks for any tips!

EDIT: Note, that I would like to communicate back to my java application from some form on that webpage (for example sending parametres to specify the way my page is updated)

Upvotes: 0

Views: 737

Answers (1)

Powerslave
Powerslave

Reputation: 1428

Use AJAX technology (jQuery pretty much fits your needs) to invoke a server side controller in your application. You can then negotiate the need for a data update. A JSON API is recommended for this. You can use Jackson for JSON-related operations in your Java code.

To save bandwidth, you could poll for only a boolean value to determine whether the server has new data since your last update (e.g. provide since=[some_timestamp] as request param) and query for the actual data only if it makes sense (that is, the server returned true).

Upvotes: 1

Related Questions