在JSP中使用getOutputStream()方法,经常会遇到getOutputStream() has already been called for this response的错误,尤其是使用TOMCAT作为服务器时。对于 这种错误,网上的解决办法也比较多,有的是通过在getOutputStream()方法前不输出任何东西,我试了一下这种方法,不是很妥。于是就采用第二个方法:在使用getOutputStream()方法后加上两句代码:out.clear();out=pageContext.pushBody();原因也很简单,主要是因为:在tomcat中jsp编译成servlet之后在函数_jspService(HttpServletRequest request, HttpServletResponse response)的最后有一段这样的代码
finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
}
这里是在释放在jsp中使用的对象,会调用response.getWriter(),因为这个方法是和response.getOutputStream()相冲突的!所以会出现以上这个异常。
getOutputStream() has already been called for this response
最新推荐文章于 2020-12-12 20:49:58 发布