Reputation: 21311
This is a method that's used for handle ajax request. So the output is written to the response
public ModelAndView myAction(HttpServletRequest request, HttpServletResponse response, BindException errors) throws Exception
{
//call other methods and send the response as arg
//call getWriter on the response
}
According to this doc, I would definitely have an IllegalStateException if I call getWriter having already called getOutputStream(), so I suspect the methods I passed the response to did this although I don't really see them doing so... The only thing for sure is that at some point, one of the methods may do response.sendError(). Does this some how call getOutputStream()?
Upvotes: 1
Views: 2121
Reputation: 53513
I had similar issues but I had not called sendError(), just setContentType() before that. As per this source, it can trigger the same behaviour:
I am guessing its because you have already opened the stream by calling the resp.setContentType("text/plain"); method, and are then trying to get a Writer object. You can either use Stream based classes, or Writer based classes - not both.
Either remove the setContentType(), or send the response using response.getOutputStream() method. That should solve the problem.
And indeed, it resolved the similar error for me.
Upvotes: 0
Reputation: 280132
HttpServletResponse#sendError()
commits the response and send an error status code. The javadoc states
If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.
In other words, after you call that method, the HTTP response has basically been sent. It makes no sense to call any of the getOutputStream()
or getWriter()
methods. Your Servlet
container further makes it foolproof by throwing an Exception if you attempt to.
Upvotes: 1