JSP flush buffer confusion
If i have the below code:
<% out.write("This will print!<br/>"); out.flush(); out.write("This will also print!<br/>"); out.flush(); <-- Should fail. out.write("This will get sent to the client too!<br/>"); %>
Why is the second flush being allowed? I thought that a flush will commit a response and being sent to the client. Once a response is commited, you can't write and flush anymore. The first flush commited the response and has sent to the client. So i would expect the second flush to fail.
I also read that for an include on the requestDispatcher, one can flush as many times as you want (but not for a requestDispatcher forward). How is that so?
I'm confused on this and i would appreciate any enlightening. Thanks.
You can do flush many times. A flush called (automatically by container OR manually by code) caused existing data in output buffer sent to client. After first flush, the response is marked committed. We have no chance to write response headers/cookies After response is committed. After each flush, the buffer will be reset for storing data of next content writes. You can do flush manually or container will do flush when the buffer is full or at the end of request.