zoukankan      html  css  js  c++  java
  • java.lang.IllegalStateException异常:简单的分析和简单解决方案

    我们在做文件上传或者下载,或者过滤等操作时,可能要用到页面的输出流.
    例如在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;

  • 相关阅读:
    About HDFS blocks
    Hadoop源代码分析(一)
    Hadoop源代码分析(三)
    MapReduce基础
    Pig安装与配置教程
    MapReduce数据流(三)
    MapReduce数据流(二)
    c++中的临时对象
    如何修改CMD命令行窗口下的默认路径
    VC2005: warning LNK4076: 无效的增量状态文件
  • 原文地址:https://www.cnblogs.com/bsyx/p/4312909.html
Copyright © 2011-2022 走看看