tags:

views:

32

answers:

1

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.

+1  A: 

The 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.

skaffman
My question was about internal state. When you request Writer, servlet implementation remembers that and next time when you try to request OutputStream you will get Illegal state exception. Now, if you do reset, will the state be cleared and you can now request OutputStream without taking the exception?
Dmitriy
Read from your own link
Dmitriy
http://download.oracle.com/javaee/6/api/javax/servlet/ServletResponse.html#getWriter()
Dmitriy