• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

response.flushBuffer() and jsp:forward

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic