You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jonathan Eric Miller <je...@uchicago.edu> on 2001/08/16 21:14:52 UTC

Bug in ServletResponse.flushBuffer() in Tomcat 4.0b7?

I'm having problems using ServletResponse.flushBuffer() and Tomcat 4.0b7.
The following servlet demonstrates.

What I want it to do is print out the title and the "Test 1" line. Then,
pause for 10 seconds and print out the "Test 2" line. It doesn't work the
first time through. However, if I then hit Refresh in my browser after going
through it once, you can see clearly that it prints out the first line
pauses and prints out the last line as I would expect it to. Is this a bug?
Can someone else reproduce this?

The reason I want to get this to work is that I have a servlet where I have
a page with a Submit button on it, then on the next page, there is sometimes
a few second lag while performing an update on a directory/database. I've
had problems in the past where users click the Submit multiple times because
they think it's stuck. Actually, it's not, it's just slow. So, what I want
to do is print out at least the top part of the page so that the Submit
button/previous page is no longer available for them to click on. If someone
could fix this for the final version of Tomcat 4, I would greatly appreciate
it. Either that or, if anyone else knows of a work around, that would be
appreciated too.

Thanks, Jon

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SimpleServlet extends HttpServlet
{
 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws
IOException
 {
  try
  {
   resp.setContentType("text/html");

   PrintWriter pw = resp.getWriter();

   pw.println("<html><head><title>SimpleServlet</title></head><body>");

   pw.println("<p>Test 1</p>");

   pw.flush();

   resp.flushBuffer();

   Thread.sleep(10000);

   pw.println("<p>Test 2</p>");

   pw.println("</body></html>");

   pw.close();
  }
  catch(Exception e)
  {
   System.out.println(e);
  }
 }
}



Re: Bug in ServletResponse.flushBuffer() in Tomcat 4.0b7?

Posted by Jonathan Eric Miller <to...@shark.uchicago.edu>.
Cool, thanks, I appreciate it. I'll give it a try. If this works, that's
good investigative work.

Jon

----- Original Message -----
From: "Mauro Bertapelle" <ma...@jmatica.com>
To: <to...@jakarta.apache.org>
Sent: Tuesday, August 21, 2001 12:56 PM
Subject: Re: Bug in ServletResponse.flushBuffer() in Tomcat 4.0b7?


> Jonathan,
>
> this was already discussed in this list some times ago..
>
> Regards,
>
> mauro
> --
>
> > Scott,
> >
> > I've finally got it.
> > The problem with Internet Explorer is that, no matter
> > how many flavors of no-cache, cache-no, no-cache-thanks, etc..
> > you put in your header, it'll not output anything until it
> > has read at least 256 characters:
> >
> > public void doPost(HttpServletRequest request, HttpServletResponse
response)
> >     throws ServletException, IOException {
> >
> >   response.setContentType("text/html");
> >   response.setIntHeader("Expires", -1);
> >   response.addHeader("Cache-Control", "no-cache");
> >   response.addHeader("pragma", "no-cache");
> >   PrintWriter out = response.getWriter();
> >   out.println("<html>");                     // 7
> >   out.println("<head><title>Title</title>"); // 34
> >   out.println("</head><body>");              // 48
> >
out.println("<!-------------------------------------------------------------
----------------"); // 128
> >
out.println("---------------------------------------------------------------
----------------"); // 208
> >   // out.println("---------------------------------> phase 1<br>"); //
255, doesn't display till end of page
> >   out.println("----------------------------------> phase 1<br>"); //
256, start display immediately
> >
> >   response.flushBuffer();
> >
> >   try {
> >     Thread.sleep(5000);
> >   } catch (Exception e) {
> >   };
> >   out.println("phase 2");
> >   out.println("</body>");
> >   out.println("</html>");
> >   out.close();
> > }
> >
> > Regards,
> >
> > Mauro Bertapelle
> > JMatica Srl
> > mauro.bertapelle@jmatica.com
> > --
>


Re: Bug in ServletResponse.flushBuffer() in Tomcat 4.0b7?

Posted by Mauro Bertapelle <ma...@jmatica.com>.
Jonathan,

this was already discussed in this list some times ago..

Regards,

mauro
--

> Scott,
> 
> I've finally got it.
> The problem with Internet Explorer is that, no matter
> how many flavors of no-cache, cache-no, no-cache-thanks, etc..
> you put in your header, it'll not output anything until it
> has read at least 256 characters:
> 
> public void doPost(HttpServletRequest request, HttpServletResponse response)
>     throws ServletException, IOException {
> 
>   response.setContentType("text/html");
>   response.setIntHeader("Expires", -1);
>   response.addHeader("Cache-Control", "no-cache");
>   response.addHeader("pragma", "no-cache");
>   PrintWriter out = response.getWriter();
>   out.println("<html>");                     // 7
>   out.println("<head><title>Title</title>"); // 34
>   out.println("</head><body>");              // 48
>   out.println("<!-----------------------------------------------------------------------------"); // 128
>   out.println("-------------------------------------------------------------------------------"); // 208
>   // out.println("---------------------------------> phase 1<br>"); // 255, doesn't display till end of page
>   out.println("----------------------------------> phase 1<br>"); // 256, start display immediately
> 
>   response.flushBuffer();
> 
>   try {
>     Thread.sleep(5000);
>   } catch (Exception e) {
>   };
>   out.println("phase 2");
>   out.println("</body>");
>   out.println("</html>");
>   out.close();
> }
> 
> Regards,
> 
> Mauro Bertapelle
> JMatica Srl
> mauro.bertapelle@jmatica.com
> --

Re: Bug in ServletResponse.flushBuffer() in Tomcat 4.0b7?

Posted by "Pier P. Fumagalli" <pi...@betaversion.org>.
Jonathan Eric Miller at jemiller@uchicago.edu wrote:

> I'm having problems using ServletResponse.flushBuffer() and Tomcat 4.0b7.
> The following servlet demonstrates.
> 
> What I want it to do is print out the title and the "Test 1" line. Then,
> pause for 10 seconds and print out the "Test 2" line. It doesn't work the
> first time through. However, if I then hit Refresh in my browser after going
> through it once, you can see clearly that it prints out the first line
> pauses and prints out the last line as I would expect it to. Is this a bug?
> Can someone else reproduce this?
> 
> The reason I want to get this to work is that I have a servlet where I have
> a page with a Submit button on it, then on the next page, there is sometimes
> a few second lag while performing an update on a directory/database. I've
> had problems in the past where users click the Submit multiple times because
> they think it's stuck. Actually, it's not, it's just slow. So, what I want
> to do is print out at least the top part of the page so that the Submit
> button/previous page is no longer available for them to click on. If someone
> could fix this for the final version of Tomcat 4, I would greatly appreciate
> it. Either that or, if anyone else knows of a work around, that would be
> appreciated too.
> 
> Thanks, Jon

I just tested your servlet, and it works as expected...
I get the first part, then the second part after 10 seconds...
To verify that it did that correctly, I placed the compiled class in
$CATALINA_HOME/webapps/ROOT/WEB-INF/classes/SimpleServlet.class
and then I did

# telnet localhost 8080
Trying 127.0.0.1...
Connected to localhost.betaversion.org.
Escape character is '^]'.
GET /servlet/TestFlush HTTP/1.0

HTTP/1.1 200 OK
Content-Type: text/html
Date: Tue, 21 Aug 2001 16:03:42 GMT
Server: Apache Tomcat/4.0-b8-dev (HTTP/1.1 Connector)
Connection: close

<html><head><title>SimpleServlet</title></head><body>
<p>Test 1</p>
<p>Test 2</p>
</body></html>
Connection closed by foreign host.
#

"Test 1" came out as soon as I hit return twice after HTTP/1.0, and "Test 2"
after 10 seconds... It's the browser...

    Pier