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 RequestDispatcher.forward() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "RequestDispatcher.forward()" Watch "RequestDispatcher.forward()" New topic
Author

RequestDispatcher.forward()

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Mar 19, 2002
Posts: 405
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

Joined: Jan 21, 2002
Posts: 52
hi,
go to here for a look.


SCJP2, SCWCD
tony lee
Ranch Hand

Joined: Jan 21, 2002
Posts: 52
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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Mar 19, 2002
Posts: 405
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

Joined: Apr 20, 2000
Posts: 3252
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RequestDispatcher.forward()