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.
When I run the following code:
I get the following output: Will you see the source? Will you see the target? I was expecting to receive either an exception or Will you see the target. Why am I getting the output from both servlets?
The point is that the output from the first servlet has not been flushed from the buffer that out writes to. Your second servlet could call the ServletResponse method resetBuffer() or reset() and thus discard what the first servlet wrote. Thats what is meant by uncommitted output. Note that you can call the ServletResponse method setBufferSize() before doing any writing if you want your first servlet to be capable of writing more than just a few lines. Bill
Thank you for your reply. My confusion stems from the fact that the API states that the response buffer will automatically be cleared before forward is called. It does not seem to be doing that.
Author and all-around good cowpoke
Joined: Mar 22, 2000
You are right - that statement in the API contradicts what you observe and other statements in the JavaDocs API! ----- the servlet API in PDF form says: 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. The path elements of the request object exposed to the target servlet must reflect the path used to obtain the RequestDispatcher. The only exception to this is if the RequestDispatcher was obtained via the getNamedDispatcher method. In this case, the path elements of the request object must reflect those of the original request. Before the forward method of the RequestDispatcher interface returns, the response content must be sent and committed, and closed by the servlet container. --------- So it looks to me like there is a conflict between the specs and reality. Bill [ August 02, 2002: Message edited by: William Brogden ]