sitelink1 http://www.java-forums.org/advanced-java...-ajax.html 
sitelink2  
sitelink3  
sitelink4 http://1 
extra_vars4 ko 
extra_vars5  
extra_vars6 sitelink1 

Question : flush data from servlet to jsp with ajax

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

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:

1

2

3

4

5

6

7

...

for(int i=1;i<20;i++){

                 

                out.print(i);

                out.flush();

            }  

...

 

 

 

 

Answer : Reply t flush data from servlet to jsp with ajax

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:

1

2

3

4

5

6

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!! :)

 

 

 

번호 제목 글쓴이 날짜 조회 수
» 연속해서 스트림 받기 (flush data from servlet to jsp with ajax) 황제낙엽 2013.01.04 5098
29 [JavaScript Tutorials] Handling runtime errors in JavaScript using try/catch/finally (해석중) 황제낙엽 2009.04.08 3197
28 외부 라이브러리 (.js) 의 바람직하지 않은 동적 로딩 (eval함수 이용) 황제낙엽 2012.01.18 2169
27 CORS(Cross-Origin Resource Sharing) - 4 file 황제낙엽 2017.03.07 1181
26 Javascript CORS/XSS 극복하는(피하는) 방법 file 황제낙엽 2017.07.31 968
25 Javascript 를 사용하여 Binary File 읽기 황제낙엽 2010.09.29 814
24 XMLHttpRequest 의 이벤트 onreadystatechange 황제낙엽 2012.05.30 691
23 진행 상황 추적하기(XMLHttpRequest.readyState) file 황제낙엽 2012.05.23 664
22 CORS(Cross-Origin Resource Sharing) - 5 file 황제낙엽 2017.03.07 597
21 두 서버의 자원을 접근하는 클라이언트 프레임웍(Next.js)에서의 CORS오류 file 황제낙엽 2021.12.05 546
20 XMLHttpRequest.timeout 황제낙엽 2018.11.03 530
19 Fetch API (CORS 극복을 위한 노력) 황제낙엽 2021.12.05 494
18 CORS 의 내용과 이에 대한 우회 방안들 file 황제낙엽 2021.12.05 484
17 fetch() 함수 사용 예제 file 황제낙엽 2023.11.23 463
16 CORS(Cross-Origin Resource Sharing) - 1 file 황제낙엽 2017.03.07 454
15 [URLSearchParams] URL 파라미터(매개변수) 값 가져오기 file 황제낙엽 2023.02.02 451
14 XMLHttpRequest Specification 황제낙엽 2021.04.29 449
13 [Copilot] JavaScript로 서버에 특정 값을 POST 방식으로 전달하고 응답을 기다리지 않고 바로 종료 황제낙엽 2024.05.31 431
12 XMLHttpRequest.setRequestHeader 황제낙엽 2013.09.30 428
11 HTTP 접근 제어 (CORS) 황제낙엽 2017.05.29 427