Suppose I have a Java Servlet that takes a while to finish computing it's response, but I'd like the client to see intermediate response rather than wait for it to finish. For example, suppose I have the following doGet code:
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setContentType("text/html");
PrintWriter out = new PrintWriter(resp.getOutputStream());
out.println("<h1>The Title</h1>");
out.flush();
// ... some computation that takes a while
// ... now print the result via out
out.close();
}
When I am running Tomcat locally, and accessing the Servlet, I can see the <h1>
before the entire computation is completed.
However, when I run it on my server, and access it via Apache, I only get the response after the entire request is completed (in other words, I get a blank screen instead of the <h1>
which I expect to receive).
I suspect that the issue is somewhere in AJP or Apache, seems like it's ignoring the calls to flush()
, but I can't seem to find where the problem is.
On the server I'm using Tomcat 6, Apache 2.2.21 and AJP 1.3
Turned out all I needed was to build the APR based Native library for Tomcat. After properly building and installing it based on the instructions here, it worked fine with AJP.