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

穿精又带淫゛_ 提交于 2019-12-06 08:02:55

问题


<%
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.


回答1:


<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>



回答2:


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.




回答3:


You have to set your "Transfer-Encoding: chunked" header before you write your content.

response.setHeader("Transfer-Encoding", "chunked");


来源:https://stackoverflow.com/questions/11444047/jsp-programming-response-getwriter-flush-doesnt-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!