Two Laptop Bag
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes calling println method after commit 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 "calling println method after commit" Watch "calling println method after commit" New topic

calling println method after commit

Rancy Chadha
Ranch Hand

Joined: Jul 12, 2006
Posts: 135
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.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.


Thanks,<br />-Rancy
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

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.

[My Blog]
All roads lead to JavaRanch
Rohit Sajan
Ranch Hand

Joined: Jun 15, 2006
Posts: 34
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

madhav changala
Ranch Hand

Joined: Dec 20, 2005
Posts: 57

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

It is also container dependent


Rancy Chadha
Ranch Hand

Joined: Jul 12, 2006
Posts: 135
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.

I agree. Here's the link:
subject: calling println method after commit
jQuery in Action, 3rd edition