Results 1 to 2 of 2
  1. #1
    Laerion is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Question flush data from servlet to jsp with ajax

    Hi!

    I've a servlet that process a file and reports the number of line which has processed. In the other side, I have a JSP, wit a div. This jsp uses Ajax to call the servlet.

    The intended result, is that Ajax receives all the out.print of the Servlet and with javascript update an html table in the jsp.

    I've been able to update the table and all that, but only after the servlet finish, all the lines that is printing, are not received in the ajax (or printed in the page) until this servlet finishes.

    I understand that Ajax can monitor the status of the servlet, and as I can see, it finishes when status is 4 (completed) and result is 200 (OK). I'm sending from the servlet the out.print("text"), but as I said, the jsp process it when both 4 state and 200 status are received, nothing is received while the servlet is processing.

    Can you help me with this please?

    These are some code snippets:
    Ajax
    Java Code:
    var dinformacion = document.getElementById("informacion");
    			var isIE;
    			function XMLHttp(url) {
    			    return XMLHttp(url, null);
    			}
    			function XMLHttp(url, callback, callbackprocess) {
    			    var req = init();
    			    req.onreadystatechange = processRequest;
    			    function init() {
    			        if (window.XMLHttpRequest) {
    			            return new XMLHttpRequest();
    			        } else {
    			            if (window.ActiveXObject) {
    			                isIE = true;
    			                return new ActiveXObject("Microsoft.XMLHTTP");
    			            }
    			        }
    			    }
    			    function processRequest() {
    				    //dinformacion.innerHTML = req.readyState + "/" + req.status;
    			    
    			        if ((req.readyState == 4) && (req.status == 200) && (callback)) {
    			        	var a = req;
    			            callback(req.responseText);
    			        }
    			        else {
    			        	//HERE IT FAILS ALL THE TIME
    			        	callbackprocess(req.responseText);
    			        }
    			    }
    			    this.doGet = function () {
    			        req.open("GET", url, true);
    			        req.send(null);
    			    };
    			    this.doPost = function (body) {
    			        req.open("POST", url, true);
    			        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    			        req.send(body);
    			    };
    			}
    			function getElementY(element) {
    			    var targetTop = 0;
    			    if (element.offsetParent) {
    			        while (element.offsetParent) {
    			            targetTop += element.offsetTop;
    			            element = element.offsetParent;
    			        }
    			    } else {
    			        if (element.y) {
    			            targetTop += element.y;
    			        }
    			    }
    			    return targetTop;
    			}
    
    
    		
    			var req = new XMLHttp("../servlet/ConsultaServlet",onServerResponse,onServerProcess);
    			req.doPost();
    			
    			function onServerResponse(responseXML){
    				responseXML = trim(responseXML);
    				alert(responseXML);
    			}
    			var dproc = document.getElementById("process");
    			
    			function onServerProcess(responseXML){
    				responseXML = trim(responseXML);
    				dproc.innerHTML = responseXML;
    				
    			}
    And my servlet snippet:
    Java Code:
    ...
    for(int i=1;i<20;i++){
    				
    				out.print(i);
    				out.flush();
    			}	
    ...

  2. #2
    go4soumya is offline Member
    Join Date
    Oct 2010
    Posts
    32
    Rep Power
    0

    Default Reply t flush data from servlet to jsp with ajax

    Hi, Laerion

    I think you yourself gave the answer to it....

    I understand that Ajax can monitor the status of the servlet, and as I can see, it finishes when status is 4 (completed) and result is 200 (OK). I'm sending from the servlet the out.print("text"), but as I said, the jsp process it when both 4 state and 200 status are received, nothing is received while the servlet is processing.
    You don't receive the XmlHttpRequest object and so the responseText until the servlet you're calling via AJAX completes its execution! You might get a better view of it if u put a Thread.sleep(2000)....i.e., a delay of 2secs in your "for" loop

    Java Code:
    for(int i=1;i<20;i++){
    				
    				out.print(i);
    				out.flush();
                                    Thread.sleep(2000);
    			}
    So, you cannot get any responseText while the servlet is processing. To achieve something like this, people tend to use a 'server push' method. You might like to see topics like 'Comet' also called Reverse Ajax, pushlets to get responses from the servlet to the client and u can use them your own way to achieve your goal!

    good luck!! :)

Similar Threads

  1. AJAX - JSP to Servlet simple sample pls
    By kurorolucifer15 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-20-2011, 08:58 AM
  2. Need ajax implementation of servlet?
    By alok_sharma in forum Java Servlet
    Replies: 0
    Last Post: 11-19-2010, 12:04 PM
  3. How to flush gui from using old data
    By ShinTec in forum AWT / Swing
    Replies: 1
    Last Post: 06-10-2010, 03:18 PM
  4. Ajax / servlet File upload
    By n3mesi83 in forum Java Servlet
    Replies: 0
    Last Post: 06-08-2010, 03:48 PM
  5. how to contact servlet from clientside through ajax
    By santhoskumara in forum Advanced Java
    Replies: 2
    Last Post: 08-10-2007, 07:56 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •