Basically I have a java application that can display the data it outputs onto a webpage. I have a div containing tables, each table representing one java object and its values, after a period of time I want AJAX to refresh that div with the new data. Problem, I'm using a java servlet as certain actions have to be performed on the data before it can be displayed on the page(they are snmp objects so I have to use snmp methods to get the values of their OIDs).

It seemed to work at first but I noticed the refresh interval seemed to get shorter each time:

    <script>
    $(document).ready(function() {

     window.setInterval(function() {// set time limit for AJAX call
            $(function() {
                $('#id').load('servlets');
            });
        }, 5000);
    });
    </script>

I looked at the page with firebug and noticed that the whole html doc is being put into the div where I want the info to be refreshed. Is it possible to use the .load() on a servlet call to get the div I want from its response? I.E $('#id').load('servlet #id') or is there an easier way of getting the div I want from the response?

There are two issues here :

Unreliable interval

This is most probably caused by internet/server lag. For short intervals it's better to call setTimout() from a .load() callback function thus imposing a delay starting from when each server response has been handled.

Loading each div individually

You could work out a way of extracting an individual data set (table) from a whole HTML doc but it would be more economical to modify the servlet to return just one data set (a <table>...</table> fragment) if an id=... parameter is supplied. The same servlet can still deliver the whole doc if no id parameter is supplied.

Putting all this together, the javascript will be something like this:

$(function() {
    var timeouts = {};//cache of timeout referneces
    function loadFromServelet(id, t) {
        $('#'+id).load('servlets?id=' + id, function() {
            stopLoadFromServelet(id);
            if(!t || isNaN(t)) { return; }
            timeouts[id] = setTimeout(function() {
                loadFromServelet(id, t);
            }, t);
        });
    }
    function stopLoadFromServelet(id) {
        if(timeouts[id]) { clearTimeout(timeouts[id]); }
    }

    //sample calls
    loadFromServelet('myId_1'); //load element "#myId_1" once
    loadFromServelet('myId_2', 5000); //load element "#myId_2" every 5 seconds
    loadFromServelet('myId_3', 20000); //load element "#myId_3"  every 20 seconds
    loadFromServelet('myId_2', 15000); //change load interval of element "#myId_2" to 15 seconds
    loadFromServelet('myId_2'); //load "#myId_2" once and stop repeated loading
    stopLoadFromServelet('myId_3'); //stop loading element "#myId_3"
}

I can't help much with the server-side stuff except to show you how to pass an id in the url's query string (see above). It's up to you to pick up the value and act on it in the servlet.

Airshow

Thank you it worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.