Forums Register Login

resetBuffer() OR flushBuffer()

+Pie Number of slices to send: Send
From the servlet specification, please consider the following line of methods...
flushBuffer

public void flushBuffer()
throws java.io.IOExceptionForces any content in the buffer to be written to the client. A call to this method automatically commits the response, meaning the status code and headers will be written.

--------------------------------------------------------------------------------

resetBuffer
public void resetBuffer()Clears the content of the underlying buffer in the response without clearing headers or status code. If the response has been committed, this method throws an IllegalStateException.



My doubt is related to the request.getRequestDispatcher(). We are not allowed to commit the response before a call to this method, otherwise it will throw an IllegalStateException. by commiting we mean responce headers (status code etc)are written to the brower.
that means we can call resetBuffer() before making the call , nut can not call the flushBuffer().

Agree or Disagree??

Thanks in advance
so
+Pie Number of slices to send: Send
 

Poonam Agarwal wrote:by commiting we mean responce headers (status code etc)are written to the brower.
that means we can call resetBuffer() before making the call , nut can not call the flushBuffer().


Please clarify this line properly. Its hard to understand what you are trying to ask.
+Pie Number of slices to send: Send
Poonam I don't think calling getRequestDispatcher throws IllegalStateException if the response has already been committed. Calling forward after the response has been committed throws an IllegalStateException. You can include any other content after the response has been committed but the included resource is not allowed to set response content type or any other response headers.

flushBuffer flushes the response to the client. So you cannot forward using a request dispatcher after calling flushBuffer. resetBuffer is just the opposite. It clears the response buffer. So if you have set any response headers or anything, then resetBuffer will clear that response. But this will happen only if the response is in the buffer. If any content is already sent to the client, then calling resetBuffer throws an IllegalStateException...
+Pie Number of slices to send: Send
 

Poonam I don't think calling getRequestDispatcher throws IllegalStateException if the response has already been committed. Calling forward after the response has been committed throws an IllegalStateException.



well ankit, you are right. it just i forgot to mention in my post.

You can include any other content after the response has been committed but the included resource is not allowed to set response content type or any other response headers.



No that is absolutely wrong, you cannot right anything on the respons after it's comitted. no matter weither its forward() OR include(). once response get comitted , the satus code and header are written.


included resource is not allowed to set response content type or any other response headers.


yes that is the case with requestDispatcher.include(). it has limited access to the response object. but with forward() , the forwarded resource can allow to set the header or its content type.

But this will happen only if the response is in the buffer. If any content is already sent to the client, then calling resetBuffer throws an IllegalStateException...



please elobrate this, i did't understand what this means??

+Pie Number of slices to send: Send
 

Poonam Agarwal wrote:

You can include any other content after the response has been committed but the included resource is not allowed to set response content type or any other response headers.



No that is absolutely wrong, you cannot write anything on the response after it's comitted. no matter whether its forward() OR include(). once response get committed , the status code and header are written.



Well I created a program to test this. I wrote a servlet in which I wrote some response and then included another srevlet in it. And it worked. The only thing the included servlet can't do is to set the response headers.


Poonam Agarwal wrote:

But this will happen only if the response is in the buffer. If any content is already sent to the client, then calling resetBuffer throws an IllegalStateException...



please elobrate this, i did't understand what this means??



Look when you write some response, it goes into a buffer (if there is a buffer). Usually the buffer size is I think 2048 bytes but I'm not sure. If the contents of the buffer are written to the client once i.e. some of the response is sent to the client, then calling resetBuffer throws IllegalStateException. So you can only clear the buffer if nothing has been sent to the client as a response...
+Pie Number of slices to send: Send
 

Ankit Garg wrote:
Well I created a program to test this. I wrote a servlet in which I wrote some response and then included another srevlet in it. And it worked. The only thing the included servlet can't do is to set the response headers.



Whatever you did before calling the include method didn't actually commit it to the response. It only wrote it in the buffer. So, in this case, if you had called resetBuffer() after you included the other servlet, it would have worked perfectly. But had you flushed the response (ie, commit the response) before you included that servlet, you would have recieved an IllegalStateException.

The reason why an included resource doesn't have full control over the response object is so that nothing gets commited to the response before it is included. So, even the calling servlet shouldn't have commited the response before including the resource.
+Pie Number of slices to send: Send
 

Sourin K. Sen wrote:But had you flushed the response (ie, commit the response) before you included that servlet, you would have recieved an IllegalStateException.



Well I tried that too. I called out.flush in my servlet and then included the other servlet and there was no exception. I can see the output of both the servlets. Strange
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3158 times.
Similar Threads
IllegalStateException doubt ?
doubt in flushBuffer
Response.sendRedirect()
Response Headers
Doubts on buffers, committing response (with RequestDespatcher, sendError)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:59:21.