2

我有一个while (true)将数据发送到浏览器的 JSP。代码片段

while (true) {
    out.print("pushing some data to browser");
    out.flush();

    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
}

它大部分时间都可以工作,但很少会以 1 秒的间隔将数据发送到浏览器。

而不是排队并同时抛出所有数据。

4

1 回答 1

0

Here is the explaination why its not working : JSP is a servlet object at runtime and JSP scriplet/expressions are getting called as part of service() method of Servlet object.

In this case when you access JSP page it will call service() method and keep appending data to out stream until thread is interrupted by app server(InterruptedException) finally it renders page and shows data at once.

You should consider implementing CommetServlet by extending HttpServlet which can leavarage app server support.

App Server comet support examples:

于 2013-04-13T23:49:45.723 回答