Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP Programming - response.getWriter().flush(); doesn't work

Tags:

jsp

flush

tomcat

<%
response.getWriter().write("Hello world<BR>\n");
response.getWriter().flush();
wait(10000); // 10 seconds
response.getWriter().write("Goodbye happiness.<BR>\n");
%>

Expected results: "Hello World" is displayed in the browser. 10 seconds later, "Goodbye happiness." is displayed.

What happens: The page sits there loading for 10 seconds, and then "Hello World Goodbye Happiness" are displayed at the end.

What I want to do is display the status of a long-running operation as different milestones are reached. Is this possible?

I'm using Eclipse EE (with Tomcat) on Windows 7.

like image 680
ChopperCharles Avatar asked Jan 27 '26 14:01

ChopperCharles


2 Answers

<html>
<body>
 You didn't tell us which browser you were using. Chrome and Firefox behave as you   expect them to do. 
But, IE needs some filler at the start of the page. IE will wait for a certain amount of content to render.
I am testing with IE8 and this works for me.
<br/>
<%
   out.print("Hello world<br/>");
   out.flush();
   Thread.sleep(3000); // 3 seconds for easy testing.
   out.print("Goodbye happiness.");
 %>
 </body>
 </html>
like image 55
rickz Avatar answered Jan 29 '26 05:01

rickz


I think the problem here is wait(10000); // 10 seconds. Not sure what response you are seeing but you have to take lock before doing wait(). You should change wait(10000); line to one of this :

synchronized (this){
    wait(10000); //10 secs
}

OR

Thread.sleep(10000);

Even then if it does not work you should be checking if there is anything behind which is overriding writer and buffering the data until close.

like image 45
Ramesh PVK Avatar answered Jan 29 '26 06:01

Ramesh PVK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!