简体   繁体   English

Servlet 3.0包含html页面

[英]Servlet 3.0 include html page

I'm trying to achieve the following behavior using the Servlet 3.0 API: 我正在尝试使用Servlet 3.0 API实现以下行为:

  • send an inital html page 发送初始HTML页面
  • send subsequent responses that update the page 发送后续响应以更新页面

This all works except the only way I could send the initial page without getting the response committed is by manually writing using the HttpResponse Writer... 除了我可以发送初始页面而不提交响应的唯一方法之外,所有这些方法都可以使用HttpResponse Writer手动编写...

I was wondering if there is a way of using something similar to RequestDispatcher#include with an html page without running into problems with the AsyncContext. 我想知道是否有一种方法可以将类似RequestDispatcher#include的内容与html页面一起使用,而不会遇到AsyncContext的问题。 Some things I tried until now and didn't work: 我到目前为止尝试过的一些方法没有起作用:

  • use AsyncContext#dispatch: as much as I read on the Internet, it is destined for sending the final response to the container in order to render it 使用AsyncContext#dispatch:正如我在Internet上阅读的内容一样,它的目的是将最终响应发送到容器以进行渲染
  • use RequestDispatcher#forward: getting IllegalStateException due to trying to write more content in the OutputStream 使用RequestDispatcher#forward:由于尝试在OutputStream中写入更多内容而得到IllegalStateException
  • use RequestDispatcher#include: if I initialize the AsyncContext before calling this method, request.isAsyncSupported returns true, after calling the method, it returns false. 使用RequestDispatcher#include:如果在调用此方法之前初始化AsyncContext,则request.isAsyncSupported返回true,在调用该方法之后,它返回false。 I read that it calls flushBuffer() and sets the commit flag to true on the response 我读到它调用flushBuffer()并将响应上的提交标志设置为true

Also, in the Servlet 3.0 spec there are some lines mentioning that dispatching from async servlet to normal servlet is possible but will commit the answer. 另外,在Servlet 3.0规范中,有几行提到从异步servlet到正常servlet的分派是可能的,但将提交答案。 I believe a static html page belongs to this category... 我相信静态的html页面属于该类别...

If you have any ideas of how an elegant include can be done without affecting the ability to still send streamed responses back to the client, please let me know. 如果您有关于如何在不影响将流式响应发送回客户端的能力的前提下进行优雅包含的任何想法,请告诉我。

Thanks 谢谢

use static elements on the page that store data and use requestdispatcher. 使用页面上存储数据的静态元素并使用requestdispatcher。

Or you could also just simply refresh the entire page with such an arrangement using response.setHeader("refresh", "5; URL=officer.html"). 或者,您也可以使用response.setHeader(“ refresh”,“ 5; URL = nzl.html”)以这种安排简单地刷新整个页面。

I really don't understand your need to send multiple requests without the response being committed to a servlet. 我真的不理解您是否需要在不将响应提交到Servlet的情况下发送多个请求。 are you trying to interact with a serving thread multiple times ? 您是否尝试与服务线程多次交互?

一种解决方案(不是唯一的一种):如果它只是一个html页面,则用html编写html页面本身,并对需要提供更新的serrvlet进行ajax调用。

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

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