Huh!! This discussion caused more confusion. I didn't quite get when IllegalStateException will be thrown.
What do you mean by committing a response exactly? isn't it printWriterObject.println() or outputStreamObject.write()
--------------------------------------------------------------------------------
Servlet 1 : Used getWriter and println method, followed by forward method. This did NOT throw any error.
This would not throw an error as you have not committed your response. Try calling forward after committing your response(by closing the writer), and it should throw exception.
--------------------------------------------------------------------------------
Generally we close the writer in finally statement but before that itself we handle the control to another servlet.So I felt that, in this case, after println followed by forward method should throw the exception, but as per the above statement it didn't throw exception. Can somebody explain in detail?
--------------------------------------------------------------------------------
Servlet 2 : Used getOutputStream and write method (no flush method), followed by forward method. This threw the above stated error.
Do you use getWriter() in your forwarded servlet. If yes, then you would obviously get this error as you have already called getOutputStream on the response object in the forwarding servlet.
--------------------------------------------------------------------------------
Based on my understanding in the first case, it should throw exception in this case as forward method is invoked after write method. In this case again, by write method I assume that response committed and hence the exception.
--------------------------------------------------------------------------------
Servlet 3 : Used just getOutputStream (no write and no flush method), followed by forward method. This threw the above stated error as well.
--------------------------------------------------------------------------------
As per my understanding, just getting an OutputStream object does not necessarily mean committing the response, then why did we get exception in this case? Moreover, no write method and no flush method used before forwarding to the servlet. please explain