JSP编程 - response.getWriter()。flush();不起作用

时间:2012-07-12 01:53:45

标签: jsp tomcat flush

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

预期结果:浏览器中显示“Hello World”。 10秒后,“再见幸福。”显示。

会发生什么:页面在那里加载10秒钟,然后在最后显示“Hello World Goodbye Happiness”。

我想要做的是显示长时间运行的状态,因为达到了不同的里程碑。这可能吗?

我在Windows 7上使用Eclipse EE(使用Tomcat)。

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

我认为这里的问题是wait(10000); // 10 seconds。不确定您看到的是什么回复,但在执行wait()之前必须先锁定。您应该将wait(10000);行更改为以下之一:

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

OR

Thread.sleep(10000);

即便如此,如果它不起作用,你应该检查是否有任何东西覆盖了编写器并缓冲数据直到关闭。

答案 2 :(得分:0)

在编写内容之前,您必须设置“Transfer-Encoding:chunked”标题。

response.setHeader("Transfer-Encoding", "chunked");
相关问题