Greetings-
I have a web application that has a form that triggers a server process that
I would like to provide a "Searching..." page for.
My strategy was to send the browser the "top half" of a web page, including
an animated "Searching..." gif. I write the contents of this page to the
HttpServletResponse output, and then flush the buffer (which I had thought
would cause the bytes written thus-far to make their way to the client). I
then do the time-intensive operation on the server side (simulated below
with a 10 second Thread sleep), followed by streaming to the output the
"bottom half" of the web page, including a META refresh that would cause the
client to redirect to a "Results" page.
Unfortunately, what actually happens is that the client (IE 6) waits until
the entire page is sent, briefly shows the "Searching..." image, and then
immediately redirects to the "Results".
I have tried explicitely setting the bufferSize of the response to be
smaller than the length of the "top half", but this didn't seem to alter the
behavior.
I am serving from Tomcat 5.0.16, which I'm beginning to suspect might be a
culprit here, somehow micro-managing the buffer until the servlet returns
from processRequest( ), thus thwarting my flushBuffer( ) strategy.
Below is the processRequest( ) method of my Servlet. Below that are the two
halves of the web page. I have removed some domain-specific names and
modified slightly for readability.
Thanks in advance for any insight!
-Eric
void processRequest(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
response.setHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT");
response.setHeader("Cache-Control", "no-cache, must-revalidate");
response.setHeader("Pragma", "no-cache");
BufferedInputStream inStreamA = null;
BufferedInputStream inStreamB = null;
BufferedOutputStream outStream = null;
try
{
response.setContentType("text/html");
outStream = new BufferedOutputStream(response.getOutputStream());
File fileTop = new File(FILE_TOP);
long halfFileSize= fileTop.length()/2;
response.setBufferSize(((int)halfFileSize));
inStreamA = new BufferedInputStream(new FileInputStream(fileTop));
int byteRead = inStreamA.read();
while (byteRead != -1)
{
outStream.write(byteRead);
byteRead = inStreamA.read();
}
outStream.flush();
response.flushBuffer();
try { Thread.sleep(10000); }
catch (InterruptedException ignored) { }
inStreamB = new BufferedInputStream(new
FileInputStream(FILE_BOTTOM));
byteRead = inStreamB.read();
while (byteRead != -1)
{
outStream.write(byteRead);
byteRead = inStreamB.read();
}
outStream.flush();
response.flushBuffer();
}
catch(Exception e)
{
log.log("problem : " + e);
}
finally
{
if (inStreamA != null)
{
inStreamA.close();
}
if (inStreamB != null)
{
inStreamB.close();
}
}
}
==========TOP FILE============
<html>
<head>
<title>Searching</title>
</head>
<body>
<img src="images/searching.gif" border="0">
==============================
==========BOTTOM FILE=========
<META http-equiv=refresh content="0;URL=results.html">
</body>
</html>
==============================