I have a long running servlet process that is being called from a JSP.
I would like to send the status of the servlet process to the client as it is happening.
I have everything working the way I want except for the streaming part.
I'm using ajax to call the servlet and populating a text area with the response. The response, however, is sent to the client all at once at the end instead of streamed step by step.
I'd like the client to see each of the responses (flush()) as they happen, rather than all at once at the end of the call.
Ajax calls:
postUploadFile = function() {
$("#status").val("");
YES.getAndShowStatus("status", "${home}/Upload");
}
YES.getAndShowStatus = function(listenerId, url, successFunction) {
jQuery.get(url,
function(data) {
val = jQuery("#" + listenerId).val();
jQuery("#" + listenerId).val(val + data);
}
);
};
Servlet code:
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
log.info("----------");
log.info("Doing post");
resp.setContentType("text/plain");
OutputStream out = resp.getOutputStream();
out.write("FOOBAR 1
".getBytes());
out.flush();
out.write("FOOBAR 2
".getBytes());
out.flush();
out.write("FOOBAR 3
".getBytes());
out.flush();
log.info("Done.");
}
I get this in the end but do not see the progress as it occurs. What do I need to do to see the progress as it occurs?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…