简体   繁体   中英

writing to Response OutputStream while reading from Request InputStream

I am writing a web service that is able to compute its output incrementally given its input. In other words, the underlying algorithm is a streaming algorithm. I am writing a servlet to expose this service.

Does the Servlet API / HTTP lifecycle allow the servlet to write to Response.getOutputStream() incrementally as it is reading from Request.getInputStream() ? Or must the request be fully received before the response starts to send?

Does the Servlet API / HTTP lifecycle allow the servlet to write to Response.getOutputStream() incrementally as it is reading from Request.getInputStream()?

Request.getInputStream() is mainly for reading the request body. The body request is not progressive data put in the body but a body send in one time by the client that in the server side, you will read line by line.

To answer to your question, no you cannot do incrementally response. Servlet HTTP is based on the classical http protocol . It's a client-response stateless protocol.So, for having progressive data in a response, another request must be send by the client.

To address your need, you should use Ajax mechanisms which allow to send data to and retrieve from a server asynchronously without forcing the reload of all the web page. In this way, you could query the server regularly and when the data is retrieved from the Ajax request, you could update the UI in the browser in a progressive way without reloading all the page.

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