java.lang.IllegalStateException异常:简单的分析和简单解决方案
2015-03-04 11:49
330 查看
我们在做文件上传或者下载,或者过滤等操作时,可能要用到页面的输出流.
例如在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;
例如在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;
相关文章推荐
- java.lang.IllegalStateException异常:简单分析和简单解决方案
- 简单理解java.lang.IllegalStateException异常:简单分析和简单解决方案
- 简单理解java.lang.IllegalStateException异常:简单分析和简单解决方案
- 简单理解java.lang.IllegalStateException异常:简单分析和简单解决方案
- 简单理解java.lang.IllegalStateException异常:简单分析和简单解决方案
- 运行mvn命令出现java/lang/NoClassDefFoundError: java/lang/Object错误解决方案及原因分析
- 关于java出现 java.lang.InstantiationException异常的分析与解决方案
- java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig 原因分析与解决方案
- Android开发者,必须知道的并且没有遇到的异常,附带简单的解决方案和异常过程、源码的分析从java层到Jni层
- java.lang.IllegalStateException异常简单分析和简单解决
- Unable to start activity:java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 完全分析与解决方案
- 不设置classpath可以编译简单的java程序没错(java.lang.NoClassDefFoundError错误)
- 简单理解java.lang.IllegalStateException异常
- java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit 解决方案
- java.lang.outofmemoryerror java heap space 解决方案
- 分析java.lang.OutOfMemoryError: PermGen space
- java.lang.ClassCastException: java.lang.Integer的解决方案
- exception in thread "main" java.lang.NoClassDefFoundError 解决方案
- (java)snmp 管理中心程序简单分析,欢迎拍砖
- java程序简单的内存分析