我们在做文件上传或者下载,或者过滤等操作时,可能要用到页面的输出流.
例如在action中使用:
    response.reset();
    response.setContentType(”application/vnd.ms-excel”);
    OutputStream os = response.getOutputStream();  
抛出异常:java.lang.IllegalStateException


原因分析:
这是web容器生成的servlet代码中有out.write(””),这个和JSP中调用的response.getOutputStream()产生冲突.
即Servlet规范说明,不能既调用 response.getOutputStream(),又调用response.getWriter(),无论先调用哪一个,在调用第二个时候应会抛出 IllegalStateException,

因为在jsp中,out变量是通过response.getWriter得到的,在程序中既用了response.getOutputStream,又用了out变量,故出现以上错误。

 

  • 解决:

法一:在JSP文件中,加入下面两句

<%

  out.clear();
  out = pageContext.pushBody();

%>

此法的缺陷:
很多开发项目并不是JSP前端,如freemarker,velocity等
造成问题的"response.getOutputStream()"并未被写在JSP里,而是写在servlet/action里

 

法二: 在action中,不要return 回具体的result文件,而是return null

//return SUCCESS;

return null;

 

相关文章:

  • 2021-08-29
  • 2022-12-23
  • 2022-01-14
  • 2021-09-11
  • 2022-01-15
  • 2022-12-23
  • 2021-09-15
  • 2021-07-05
猜你喜欢
  • 2022-12-23
  • 2021-10-13
  • 2021-05-24
  • 2021-12-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案