简体   繁体   English

使用Java Servlet的HTTP流

[英]http streaming using java servlet

I have a servlet based web application which produces two sets of data. 我有一个基于Servlet的Web应用程序,该应用程序生成两组数据。 One set of data in the webpage which is essential and other set which is optional. 网页中的一组数据是必不可少的,另一组数据是可选的。 I would like to render the essential data as fast as possible and then stream the optional data. 我想尽可能快地渲染基本数据,然后流式传输可选数据。 I was thinking of writing the essential data to the output stream of HttpServletRequest and then call HttpServletRequest.flushBuffer() to commit the response to the client, but do not return from the servlet code, but instead create the optional data , write that to the outputstream again and then return from servlet code. 我正在考虑将基本数据写入HttpServletRequest的输出流,然后调用HttpServletRequest.flushBuffer()将响应提交给客户端,但不从Servlet代码返回,而是创建可选数据,将其写入再次输出,然后从servlet代码返回。 What are the things that could go wrong in this scheme ? 在此方案中可能会出什么问题? Is this a standard practice to achieve this goal? 这是实现此目标的标准做法吗?

It makes somewhat sense to flush the response buffer directly between </head> and <body> so that the browser retrieves references to JS/CSS resources as fast as possible. </head><body>之间直接刷新响应缓冲区在某种意义上是有意义的,以便浏览器尽可能快地检索对JS / CSS资源的引用。 It only doesn't make sense to do it in a servlet as it's the JSP who is supposed to be used to generate HTML. servlet中这样做没有任何意义,因为应该使用JSP来生成HTML。

</head>
<% response.flushBuffer(); %>
<body>

(that's one of the 0.01% cases where using a scriptlet is forgiveable as there's no tag which does that; only in EL 2.2 you could use ${pageContext.response.flushBuffer()} ) (这是0.01%的情况下可以使用scriptlet的情况之一,因为没有标签可以做到这一点;只有在EL 2.2中,您才可以使用${pageContext.response.flushBuffer()}

However, most servletcontainers by default already flush the buffer every 2KB and that'll surely cover the entire <head> on the average webapp. 但是,大多数servlet容器默认情况下已经每2KB刷新一次缓冲区,并且肯定会覆盖普通Web应用程序上的整个<head> You can finetune the response buffer size in server configuration (refer server documentation for details) or on a per-JSP basis using as follows: 您可以使用以下方法在服务器配置(有关详细信息,请参阅服务器文档)中或在每个JSP的基础上微调响应缓冲区的大小:

<%@page buffer="1kb" %>

Further, flushing the buffer halfway a HTML <body> makes little to no sense as you're dependent on the browser whether it would render a halfbaked HTML body or not. 此外,将缓冲区<body> HTML <body>中途刷新到HTML <body>几乎没有意义,因为您是否依赖于浏览器是否会呈现未烘焙的HTML主体。 MSIE for example displays nothing until the </body> is arrived. 例如,在</body>到达之前,MSIE不会显示任何内容。

A completely different alternative is to use JS/Ajax to load the "optional" content asynchronously in the background when the page is completed loading. 完全不同的替代方法是在页面加载完成后,使用JS / Ajax在后台异步加载“可选”内容。 Eg (jQuery flavored): 例如(jQuery风格的):

$(function() {
    $("#somediv").load("somefragment.jsp");
});

See also: 也可以看看:

I would do this instead : 我会这样做:

  • return essential data as normal servlet + javascript to do Ajax calling for optional data. 以普通的servlet + javascript的形式返回基本数据,以对可选数据进行Ajax调用。 Then essential data will be shown with out wait the optional data. 然后将显示基本数据,而无需等待可选数据。

so the html will look like this: 因此html如下所示:

<html>
   <body>
        essential data
        <javascript to do ajax call>
        essential data
   </body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM