1

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()?

4

2 回答 2

1

HttpServletResponse#sendError()提交响应并发送错误状态代码。javadoc 状态

如果响应已提交,则此方法将引发 IllegalStateException。使用此方法后,应将响应视为已提交,不应写入。

也就是说,在你调用了那个方法之后,HTTP 响应基本上已经发送出去了。getOutputStream()调用任何orgetWriter()方法是没有意义的。如果您尝试抛出异常,您的Servlet容器会进一步使其万无一失。

于 2013-10-09T20:47:28.573 回答
0

我有类似的问题,但在此之前我没有调用 sendError(),只是 setContentType()。根据这个来源,它可以触发相同的行为:

我猜是因为您已经通过调用 resp.setContentType("text/plain"); 打开了流。方法,然后尝试获取 Writer 对象。您可以使用基于 Stream 的类或基于 Writer 的类 - 不能同时使用两者。

删除 setContentType(),或使用 response.getOutputStream() 方法发送响应。那应该可以解决问题。

事实上,它为我解决了类似的错误。

于 2017-06-14T16:05:54.477 回答