Here is a situation. Custom servlet obtains writer and it is going to do some work, however exception happens, so control is taken by some servlet framework. The framework has capability to report problem directly in a browser. So it resets current response and obtains outputstream to report problem in. Since a Writer is already taken from response object, it raises an exception. So question is again, should reset function make forgotten returned stream or writer, or raising exception is legit? Servlet specification doesn't give clear answer, so I hope on help of community. Yes, I am not only developer of servlet framework, I am a developer of servlet container as well, so I need to know appropriate behavior of every component.
views:
32answers:
1The Servlet spec seems to be clear and unambiguous on this:
void reset()
Clears any data that exists in the buffer as well as the status code and headers. If the response has been committed, this method throws an
IllegalStateException
.
In other words, if enough data has been written to the response, so that the memory buffer is exceeded, then that data will be "committed", i.e. actually sent to the client. After that point, the response cannot be re-purposed, and the exception will be thrown if you try.
If practical, it is a good idea to buffer your response data, to avoid the possibility of an exception being raised partway through the generation of the data. This gives you the option of chucking it away and rendering an error response instead. This isn't always an option, though, for performance and/or scalability reasons.