• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

resetBuffer() OR flushBuffer()

 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Poonam Agarwal
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic