简体   繁体   中英

Servlet 3.0 include html page

I'm trying to achieve the following behavior using the Servlet 3.0 API:

  • send an inital html page
  • 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...

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. 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
  • use RequestDispatcher#forward: getting IllegalStateException due to trying to write more content in the OutputStream
  • use RequestDispatcher#include: if I initialize the AsyncContext before calling this method, request.isAsyncSupported returns true, after calling the method, it returns false. I read that it calls flushBuffer() and sets the commit flag to true on the response

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. I believe a static html page belongs to this category...

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.

Or you could also just simply refresh the entire page with such an arrangement using response.setHeader("refresh", "5; URL=officer.html").

I really don't understand your need to send multiple requests without the response being committed to a servlet. are you trying to interact with a serving thread multiple times ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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