This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes resetBuffer() OR flushBuffer() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "resetBuffer() OR flushBuffer()" Watch "resetBuffer() OR flushBuffer()" New topic
Author

resetBuffer() OR flushBuffer()

Poonam Agarwal
Ranch Hand

Joined: May 12, 2008
Posts: 323
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


Thanks <br />Poonam Agarwal SCWCD 86%
Sourin K. Sen
Ranch Hand

Joined: May 02, 2006
Posts: 86
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.


Regards,<br />Sourin.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Poonam Agarwal
Ranch Hand

Joined: May 12, 2008
Posts: 323
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??

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

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...
Sourin K. Sen
Ranch Hand

Joined: May 02, 2006
Posts: 86
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.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: resetBuffer() OR flushBuffer()