jquery - web client to get progressive server response - how to? -
been trying this, perhaps efforts not heading right, appreciate help.
my html page has button , div. when click button, server has keep pushing numbers couple of seconds interval, , server pushes numbers, need them on div, progressively, in-synch server's status. how achieve this? tried using asynccontext, behaves similar response , flushes whole output @ end not want.
protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html"); asynccontext ac = request.startasync(request, response); ac.start(new runnable() { @override public void run() { (int = 1; <= 5; i++) { try { ac.getresponse().getwriter().print("hello-"+i); system.out.println("written hello"); thread.sleep(3000); } catch (exception e1) { e1.printstacktrace(); } } ac.complete(); } }); }
and html below:
<html> <head> <script src="../js/jquery/jquery.js"></script> <script> function ajax_stream() { var xhr = new xmlhttprequest(); xhr.onreadystatechange = function() { $("#mydiv").text($("#mydiv").val+xhr.responsetext); }; xhr.open("get", "../test", true); xhr.send(); } </script> </head> <body> <button onclick="ajax_stream();">start ajax streaming</button> <div id="mydiv">starting : </div> </body> </html>
not sure if understanding on concept correct, progressive state of server response captured client trying achieve.
Comments
Post a Comment