• 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

Using RequestDispatcher - doubt

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

It has been said that "A request cannot be forwarded if some response has been already written into the output stream OR out.flush() is called "
But my doubt is unless the stream is closed (ie, out.close()) the object holding the stream ( ie, response) can be forwarded right??
out.flush() just flushes out the contents of the buffer but doesnt closes the stream :roll:
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A more straightforward wording of the rule is "A request cannot be forwarded is the response has already been committed".

A response is said to be committed if the output stream has already been flushed (either explicitly via flush() or implicitly by reaching the buffer's capacity). A buffer flush causes all headers and the first lot of content to be written to the client - so obviously it's too late to go back on yourself, modify headers, send the client elsewhere or present them with a different page!

It has nothing to do with closing the stream itself, although this would also cause a flush. There is a rule about "closing the response" which occurs when the total amount of data specified by setContentLength() has been written to the response - after that, the response is certainly considered committed and no more data should be written to the output stream (as presumably all of it already has been). This has been clarified in the Servlet 2.5 specification for the case when setContentLength(0) is used.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Charles that means if there has been written something into RESPONSE and it hasn't reach till the point where the buffer has to be flushed and we do FORWARD it will work?
 
Charles Lyons
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Charles that means if there has been written something into RESPONSE and it hasn't reach till the point where the buffer has to be flushed and we do FORWARD it will work?


Exactly right! In fact, the documentation for the forward() method in the API guarantees that any existing content in the buffer will be cleared and ignored, provided the response has not been comitted.
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An analogy

You meet a bully and he asks you to hand over your coke (makes a Request)
You're a teeny weeny guy but you have an Anrold Schwarnegger friend sitting a foot away.
You think to yourself "f*** off you A*******" (response.getOutputStream().write()) {no problem, ure still thinking)} and want to call your friend and ask him to deal with the bully (RequestDispatcher.forward())
but before you tell your friend what's on your mind, you feel like showing off a bit and blurt out what you have in your mind -in other words- "f*** off you a******" (response committed -> out.flush() )
result: the bully knocks a row of teeth of your upper jaw OFF *ouch* (IllegalStateException)
...
but suppose you whispered in Arnie's ears "please teach this harebrain a lesson" and stopped yourself from blurting-
result: the bully gets strung to the nearby tree with his thumbs + you get a coke for getting the bully handled so smoothly
[ April 18, 2006: Message edited by: Akshay Kiran ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic