File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes response.flushBuffer() and jsp:forward 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 "response.flushBuffer() and jsp:forward" Watch "response.flushBuffer() and jsp:forward" New topic
Author

response.flushBuffer() and jsp:forward

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi,

Straight from book of David BridgeWater:

1-There is no buffer (in a JSP, this can be achieved with a page directive,
setting the buffer attribute to �none�), and even one character has been
written to the response.
2: The buffer has been explicitly flushed (response.flushBuffer()).
3: The buffer has been automatically flushed on filling up (in a JSP, this
will happen by default�see the page directive attribute autoFlush for more
information).

If you try to do any of the above, you�ll get an IllegalStateException.


I have doubt regarding point #2:
I did the following:

ForwardingPage.jsp:


In this case nothing is printed on the screen as response has already been
flushed. But if put
<%out.print("Hello there");%>
It is printed on the browser.

In both the cases I don't get IllegalStateExceoption. Page is not forwarded
too.

Please guide me!


Thanks,


cmbhatt
Mirko Bonasorte
Ranch Hand

Joined: May 14, 2007
Posts: 244
Hi,
consider that the jsp:forward clears the buffer and, if there is something committed, it throws IllegalStateException.

From the apis (javax.servlet.RequestDispatcher):

forward should be called before the response has been committed to the client (before response body output has been flushed).If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.


Note that if you have committed something, the status line cannot be set any more, so you can't see the error on your browser.


SCJP<br />SCWCD 1.4 Upgrade (Remember: me stupid)<br />SCWCD 1.4<br /><a href="http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html" target="_blank" rel="nofollow">SCBCD 5.0</a><br /><a href="http://www.enthuware.com" target="_blank" rel="nofollow">SCBCD 5.0 mock exam</a> <br /> <br />SCEA 5 Part1: Preparing...
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Mirko,

Thanks for reply!

2: The buffer has been explicitly flushed (response.flushBuffer())


How to see whether this works. Could you please give me example?

I did like:



But I see hello on the screen instead of IllegalStateException;


Note that if you have committed something, the status line cannot be set any more, so you can't see the error on your browser.


Please clarify.
Mirko Bonasorte
Ranch Hand

Joined: May 14, 2007
Posts: 244
If you commit something, your browser receives the headers and (part of) your html; in the specific case, you receive "Hello world".
But when you invoke the forward, the application server throws IllegalStateException, but the server cannot send any more an error code (5xx), because part of the body has already sent.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: response.flushBuffer() and jsp:forward