aspose file tools*
The moose likes Servlets and the fly likes Where does request.getOutputStream() actually write the data? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Where does request.getOutputStream() actually write the data?" Watch "Where does request.getOutputStream() actually write the data?" New topic
Author

Where does request.getOutputStream() actually write the data?

Jef Smith
Greenhorn

Joined: Jul 28, 2011
Posts: 14

I'm looking at a Servlet that does output in a loop. The code in Servlet.doGet() looks like



The client shows all of the stuff as a single line. So it looks like the data is written to a buffer somewhere and then committed when the Servlet.service() method is exited.

Several questions come to mind:

The ServletOutputStream javadoc says the print method "...writes...to the client". In my loop case is the Servlet sending a partial response on each loop iteration or is it writing to a buffer?
If it is writing to a buffer is there some buffer size limit?
If there is a buffer size limit does the Servlet send a partial response when the buffer is full?
How does the Servlet tell the Client that it is sending a partial response?
What is a partial response? I don't see anything in the HTTP protocol about partial responses.

Sorry for asking multiple questions but they all occurred to me when I saw this loop in the code.

Thanks for any explanation,
Jsmith
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

Jef Smith wrote:In my loop case is the Servlet sending a partial response on each loop iteration or is it writing to a buffer?


I guess you could say it's sending a "partial response", whatever that might mean to you, and yes, it goes into a buffer.

If it is writing to a buffer is there some buffer size limit?


You don't control the buffer size so there's really no point in asking whether there's a limit on how big the buffer can be. (If that's what you meant to ask.) The servlet container, or the operating system, or both, will assign space for buffers based on whatever rules and algorithms their programmers put into them.

If there is a buffer size limit does the Servlet send a partial response when the buffer is full?


Regardless of whether there is a buffer size limit or not, the servlet container does send the contents of the buffer in the general direction of the client when the buffer gets full. Whether it's correct to call that a "partial response" I don't know. If you meant to ask whether the container would enlarge the buffer if it got full, then the answer is no. That's not how buffers usually work.

How does the Servlet tell the Client that it is sending a partial response?


It doesn't, because it isn't sending a "partial response". It's just sending the response.

What is a partial response? I don't see anything in the HTTP protocol about partial responses.


I don't know what it is. You're the one who brought the term in here. And you're right about HTTP; since it is transmitted over TCP, the response gets broken into packets which are sent independently. If you had the idea that the response would be collected on the server into one big lump and then sent all at once to the client, then no, that's not how it works.

Thanks for any explanation


I hope that answered your questions.
Jef Smith
Greenhorn

Joined: Jul 28, 2011
Posts: 14
Paul,

Thanks for the great explanation. I was concerned that the output in a loop was something that the client was depending on. As you pointed out, this is meaningless. The client waits until it gets a complete HTML response or times out. How it was sent by the Servlet, as one piece or many, is of no importance. My thinking has been somewhat muddled by the intermingled concepts of multi-part response and output in a loop.

Thanks again for taking the time to answer,
jSmith
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

Yeah, there's less there than meets the eye. Basically the way to think about it is, you send the data and eventually the client will receive and deal with the data.

Although if you send a lump of data, then pause for quite a while (doing complex processing for example), and then send the rest of the data, the client may receive and display the first lump and then pause until it gets more data. I've seen this occasionally: the example I have in mind sends out page headings and then collects the data that it's going to send. From the client side I see the first few buffers of the heading, then a pause, then everything shows up.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Where does request.getOutputStream() actually write the data?
 
Similar Threads
'Buffer' Question: What does 'written to the client' mean?
cleared, commited and flushed
java.lang.IllegalStateException
Doubts on buffers, committing response (with RequestDespatcher, sendError)
Response buffering