• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using RequestDispatcher - doubt

 
suresh guru
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • 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:
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • 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.
 
shweta bulbule
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • 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.
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic