0


I'm trying to read a file in servlet and send(Response) it to jsp as chunked message, where I can see it in browser as plain text.
Here's what I have tried:
Servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/plain");
    response.setHeader("Transfer-Encoding", "chunked");
    response.setHeader("Connection", "keep-alive");
    //response.flushBuffer();
    try (PrintWriter writer = response.getWriter();BufferedReader br = new BufferedReader(new FileReader("/some/file/path.txt"))) {
            String line;
            while ((line = br.readLine()) != null) {
                writer.println(line);
                try{Thread.sleep(500);}
                catch(InterruptedException e){}
            }
    }
}

jsp:

<head>
<meta charset="UTF-8">
<title>example</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
    $(document).ready(function() {                                   
      $.get('/project/trial', function(responseText) {
        $('#message').text(responseText);                  
      });
  });
</script>
</head>
<body>
<div id="message"></div>
</body>

I need to update page with new line every 500ms without refreshing page but instead I'm getting whole file at a time after whole file is read.
Am I doing something wrong? Am I missing something?
Is my jsp correct?
I tried many resources online but couldn't find out what I'm missing.
Is there any example where I can get support on chunked response reading and writing?

Thanks in advance.

Y.Prithvi
  • 1,221
  • 3
  • 14
  • 27
  • It doesn't work like that. The browser will wait to read the entire response and then render it. If you want it refreshed in segments, you'll have to do something via AJAX. NB This has nothing to do with chunked transfer mode. – user207421 Sep 24 '14 at 06:17
  • Oh ok. Can you please suggest me with some example how AJAX can be used in this scenario and how chunked message can be used. Is there any scenario where we can use both? – Y.Prithvi Sep 24 '14 at 06:26
  • Besides ... what you send there is not not really chunk-encoded. That would be ``. Unless JSP takes care of that through some magical intervention, please consult [RFC 7230, sec 4.1](http://tools.ietf.org/html/rfc7230#section-4.1) ff – DaSourcerer Sep 24 '14 at 06:26
  • @DaSourcerer Do you mean we have to split data into chunks and send it? – Y.Prithvi Sep 24 '14 at 06:37
  • @Y.Prithvi that among other things. Preparing a full answer right now. – DaSourcerer Sep 24 '14 at 06:49
  • @Y.Prithvi looks like JSP takes care of encoding on its own. See [here](http://stackoverflow.com/q/11444047/3012385). But the `println()` is wrong, IMHO. – DaSourcerer Sep 24 '14 at 07:01
  • @DaSourcerer Looks good. But we are sending response through servlet and more over it's keep on loading until everything is printed out. Do we have to use AJAX for this? – Y.Prithvi Sep 24 '14 at 07:25
  • See my answer. Your best bet is indeed ajax. – DaSourcerer Sep 24 '14 at 07:31

1 Answers1

2

I think what you effectively want to do here is implementing a version of Comet, also known as server pushing. This can be done via Ajax but requires careful preparation.

One way to do this would be to take advantage of the multipart/x-mixed-replace MIME type to send out updated content to the client. An example of this can be found here (please take note that the used boundaries in that example are not in compliance to MIME, IMHO. Boundaries between parts should look like {$bondary}-- while the very last one should be {$boundary}<CR><LF>). However, this comes at the price of impaired functionality with MSIE.

A list of comparable implementations among some alternatives can be found over at ajaxpatterns.org. In any way, you should see to it that your output writer is actually being flushed after writing out a line.

DaSourcerer
  • 6,288
  • 5
  • 32
  • 55