Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher.forward()

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The forward method of the RequestDispatcher interface may be called by the
calling servlet only when no output has been committed to the client. If output data
exists in the response buffer that has not been committed, the content must be
cleared before the target servlet’s service method is called. If the response has been
committed, an IllegalStateException must be thrown.
"does this mean I should have no out.println stmt
before calling requestDispatcher.forward() and after calling the forward"???
please someone clear my doubt
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I believe it means is that you should check the response with:
ServletResponse.isCommitted() method prior to forwarding of the request, at that point processing ends for this request within this servlet.
craig
 
tony lee
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
go to here for a look.
 
tony lee
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Besides, I tried the code:
out.println("##");
RequestDispatcher dispatcher =
req.getRequestDispatcher("/servlet/aView");
dispatcher.forward(req, res);
out.println("!!");
It works, but there's no printout either "##" or "!!". Why
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
out.println("##");
"##" is stored in the output buffer (but not yet sent to the browser).
RequestDispatcher dispatcher =
req.getRequestDispatcher("/servlet/aView");
dispatcher.forward(req, res);

The output buffer is cleared, removing "##". Then "/servlet/aView" is called, its response is committed and the output stream is closed.
out.println("!!");
An IOException is thrown because "out" is closed.
By the way, there is no need to call isCommitted() if you know that the response will not have been committed yet, i.e. if you have generated (almost) no output and have not called any methods that would commit the response.
- Peter
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason why you are not getting an IOException message or the output "!!" is because
after the request and response has been forwarded to the the url, processing ends for this request within this serlvet.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, processing doesn't really end; it continues, but the output stream has been closed and nothing will be sent to the client anymore (as you pointed out, any exceptions thrown will be effectively invisible). Is this merely an academic point? Well, I could imagine situations in which perceived response time is improved by first sending a response to the client and then performing some further processing.
- Peter
[ April 07, 2002: Message edited by: Peter den Haan ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic