|
java.lang.IllegalStateException: getOutputStream() has already been called for this response
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.
No. This is not directly related to the RequestDispatcher question. You have to remember that you get the IllegalStateException because you cannot invoke both getWriter() and getOutputStream on the same response object. (Nothing to do with RequestDispatcher in this case.)
so my question still remains
SCJP5 90%
SCJP 1.4 -- 98%<br />SCWCD 5 -- 97%
Originally posted by Ana Nava:
Thanks for the response. Sorry for not being clear with my example.
I did the following :-
Servlet 1 : Used getWriter and println method, followed by forward method. This did NOT throw any error.
Servlet 2 : Used getOutputStream and write method (no flush method), followed by forward method. This threw the above stated error.
Servlet 3 : Used just getOutputStream (no write and no flush method), followed by forward method. This threw the above stated error as well.
I did NOT invoke both getWriter() and getOutputStream on the same response object as they were in two "DIFFERENT" servlets.
The error is due to RequestDispatcher, because calling both getOutputStream and forward method in the same service method causes this exception.
so my question still remains
Thanks.
Thanks and Regards
Thanks and Regards
SCJP 1.4 (81%)<br />SCWCD 5 (95%)
What do you mean by committing a response exactly? isn't it printWriterObject.println() or outputStreamObject.write()
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?
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
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
Thanks and Regards
I didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|