Suppose you have 2 divs inside your html file.
<div id="div1">some text</div> <div id="div2">some other text</div>
The java program itself cannot update the contents of the html file, since html is connected to the client, while java is connected to the content.
You can, however, exchange information between the server (back-end) and the client.
What we're talking about is the AJAX that you use with JavaScript, I recommend using jQuery, which is a shared JavaScript library.
Suppose you want to refresh the page every constant interval, then you can use the interval function to repeat the same action every x times.
setInterval(function() { alert("hi"); }, 30000);
You can also do it like this:
setTimeout(foo, 30000);
The Whereea foo function is a function.
Instead of a warning (“hello”), you can execute an AJAX request that sends a request to the server and receives some information (for example, new text) that you can use to load into the div.
Classic AJAX is as follows:
var fetch = true; var url = 'someurl.java'; $.ajax( { // Post the variable fetch to url. type : 'post', url : url, dataType : 'json', // expected returned data format. data : { 'fetch' : fetch // You might want to indicate what you're requesting. }, success : function(data) { // This happens AFTER the backend has returned an JSON array (or other object type) var res1, res2; for(var i = 0; i < data.length; i++) { // Parse through the JSON array which was returned. // A proper error handling should be added here (check if // everything went successful or not) res1 = data[i].res1; res2 = data[i].res2; // Do something with the returned data $('
While the backend can receive POST data and can return an information data object, for example (and very preferred) JSON, there are many tutorials where this is done, Google’s GSON is what I used a while ago, you can take a look on him.
I'm not a professional in getting Java POST and returning this kind of JSON, so I'm not going to give you an example with this, but I hope this is a decent start.