Question: Calling include() before forward() throws IllegalStateException - why?
Answer: Because, the RequestDispatcher.include() method calls response.flushBuffer() method and this flushBuffer() method sets the 'committed' flag for that response object. And hence the exception is thrown in such a scenario as we know that any forward() call on a 'committed' response results in an IllegalStateException. Read more in this article - Why and when an IllegalStateException is thrown?
Thus we can conclude that RequestDispatcher.forward() method should typically be used only in the scenarios where the servlet calling this method is not supposed to do anything for building the response. All the response building task should be delegated to the servlet to which the request is forwarded. Any modification to the response object (header or body or both) will simply cause the RequestDispatcher.forward() method to throw an IllegalStateException for the simple reason that the forward() method is not capable to form a data pipeline among multiple servlets. How can we have moduler Response-Writers in Servlets? How can we form the response object by using multiple servlets? Read the answers to these questions in this article - Implementing modular response-writers for Servlets>>
Wednesday, June 18, 2008
include() before forward() throws IllegalStateException - why?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment