【发布时间】:2012-05-12 01:12:21
【问题描述】:
我们知道在Java EE servlet/jsp 中,客户端对于一个http 请求可以获得一个http 响应。我想实现一些http响应长度未知的东西。在客户端发送第一个 http 请求后,我希望服务器继续为客户端推送数据。在那种情况下,我不想使用 AJAX,因为 AJAX 的重量很重。
例如,我想创建一个可以从 Web 服务器检索 Log Message 的网页,当从另一个模块生成 Log 消息时,Web 服务器可以发送 Log 消息(所以在这种情况下,时间间隔是未知,我们假设这里不推荐定期检查计时器)。日志消息附加到 Web 浏览器,这意味着 Web 浏览器无法刷新,例如:
日志下午 12:00 ..... 日志 12:03 pm ..... 日志 12:04 pm ..... . . .
我怎样才能通过仅发送一个 http 请求并继续检索 http 响应来做到这一点?
@dystroy
你的意思是,我可以在 doPost/doGet 结束之前多次刷新打印机(当有新的日志数据时)?
请忽略语法错误!我没有使用 IDE。
protected void doPost(HttpServletRequest req, HttpServletResponse resp){
PrintWriter pw = resp.getWriter();
pw.println("<html><body>Testing for the streaming.</body></html>");
pw.flush();
/*
The syntax could not be correct, please focus on logic.
This while loop check the log sent by web server is finised or not and flush the data to the
client, after that the javascript will change the content of the inner Html. Is the logic below
valid?
*/
while(!log.finish()){
pw.println("document.setInnerHtml("+log.newLog()+")");
pw.flush();
}
}
【问题讨论】:
标签: jsp jakarta-ee servlets