Original Message Posted by : agrah upadhyay
Can anyone explain the reason behind following--
(1).When using a PrintWriter object to return the response, set the content type 'before' accessing the PrintWriter object.
Servlet container may commit the response output to the client when the response buffer is filled. But the servlet container providers may choose not to implement buffering at all. See the following quote from Servlet Spec 2.4 Section SRV.5.1 page 43.
A servlet container is allowed, but not required, to buffer output going to the client
for efficiency purposes. Typically servers that do buffering make it the default, but
allow servlets to specify buffering parameters.
So, if as a servlet developer you are not setting the content type before writing to your output stream the container may commit the response and subsequently your call for setting the content-type will be ignored by the container as the response has already been committed.
Can't get the answer for your second question.