• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling println method after commit

 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have doubt regarding committing response. As per my knowledge response is committed either when the buffer is full or when we flush the buffer. But my doubt is regarding usage of println() method after the buffer is flushed.
Say I have a code snippet like:

PrintWriter out = response.getWriter();
out.println("This is a test");
out.flush();
out.println(response.isCommitted());
out.println("Test again");

When I try this I am not getting IllegalStateException, everything works fine. It also displays true for isCommitted() method. Should it be throwing this exception and in case not then does it imply that the output can be sent to client even after the response is committed.

-Rancy
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try this I am not getting IllegalStateException

Why do you think you'll get one ? Nothing prevents you to continue writing to the output.
 
Rohit Sajan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey your printing something, then flushing and then printing again. What's wrong in that. It's perfectly fine. If you are forwarding something after flushing you will get IllegalStateException

Saju
 
madhav changala
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

You will get the exception only when you forward the request after flushing the output..

It is also container dependent

Regards
 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Thanks to all of you for your replies.
The reason I asked this question was: I was under the impression that once the response stream is committed any other attempt to write to it would result in an IllegalStateException, except if it is after a sendError or sendRedirect, in which case it will simply be ignored. Also any attempt to add a header or content type to the response after it is committed will also be ignored. Other than that any attempt to write to the response stream will result in an IllegalStateException. Probably I am not clear with the concept of committing the response. So far from what I have understood I think committing response means ensuring that data in the buffer has been sent to the client. If some one can explain the concept of committing the response to me I would really appreciate that.

Thanks,
-Rancy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic