Originally posted by Bhavna Jharbade:
The control goes to the next page only after request.forward() is called.So even after setting the request attribute, you can do anything u like and then use forward when you want to forward the control to the jsp.
From Sun's Doc:
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.
Since the output buffer is flushed before the forward, any response written in the calling( dispatching ) servlet is also flushed, hence lost. So, while forwarding a request, request attributes, response headers etc, setting beans etc is possible but writing some response here and some response there is not possible.
Also, the control from the calling servlet is not 'lost'. Using rd.forward(...) works just like a method call and the calling servlet gains control after the service method of the dispatched request returns.
[Edited to add last paragraph]
[ October 17, 2005: Message edited by: Neeraj Dheer ]