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?
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.
----- 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.
[ August 02, 2002: Message edited by: William Brogden ]
Originally posted by Mike Curwen:
Matthew, what container were you using? I remember coding a similar "prove it to myself" servlet pair and it worked as expected (per the API).
It was Orion, but I am not sure of the version at the moment.
[ August 07, 2002: Message edited by: Matthew Phillips ]