在同一个jsp页面上即使用字符流,又使用字节流就会出现以下异常:getOutputStream() has already been called for this response
2013-02-05 12:46
543 查看
l在同一个页面上即使用字符流,又使用字节流就会出现以下异常:
•同时使用字符流和字节流的代码:
<%
out.print(“HelloA”);//在JSP直接使用字符流
response.getOutputStream().print(“HelloB”);//又使用字节流
%>
•getOutputStream() hasalready been called for this response
它的意思是输出流已经被调用过了。
•即使你在页面上只使用字节流(Stream),也会出现上面的错误,因为out对象是默认存在的,而且所有数据包括<html>等标签都是由out写出的。获取out对像的过程也是从输出流中获取对象的过程,而对于一个jsp页面此方法只可以被调用一次,所以当多次调用时就会出错。
l解决方案:
•将out(JspWriter)对像保存在缓冲区中的内容清空,不允许它输出。
再调用pageContext.pushBody()返回一个新的out对像。
---------------------------------------------------------------------------------------------
<%
//因下面执行了clear方法,即清空缓冲区,所以此内容不会输出
out.print("HelloA");
//再调用字节流,因为没有缓冲它会直接显示给客户
response.getOutputStream().print("HelloB");
//清空JspWriter的缓冲区
out.clear();
//重新得到一个JspWriter对像
out =
pageContext.pushBody();
%>
•同时使用字符流和字节流的代码:
<%
out.print(“HelloA”);//在JSP直接使用字符流
response.getOutputStream().print(“HelloB”);//又使用字节流
%>
•getOutputStream() hasalready been called for this response
它的意思是输出流已经被调用过了。
•即使你在页面上只使用字节流(Stream),也会出现上面的错误,因为out对象是默认存在的,而且所有数据包括<html>等标签都是由out写出的。获取out对像的过程也是从输出流中获取对象的过程,而对于一个jsp页面此方法只可以被调用一次,所以当多次调用时就会出错。
l解决方案:
•将out(JspWriter)对像保存在缓冲区中的内容清空,不允许它输出。
再调用pageContext.pushBody()返回一个新的out对像。
---------------------------------------------------------------------------------------------
<%
//因下面执行了clear方法,即清空缓冲区,所以此内容不会输出
out.print("HelloA");
//再调用字节流,因为没有缓冲它会直接显示给客户
response.getOutputStream().print("HelloB");
//清空JspWriter的缓冲区
out.clear();
//重新得到一个JspWriter对像
out =
pageContext.pushBody();
%>
相关文章推荐
- omcat5下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
- tomcat5下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法(转)
- tomcat6下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
- tomcat6下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
- tomcat5下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
- tomcat5下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
- tomcat5下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
- tomcat5下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
- tomcat5下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
- tomcat5下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
- tomcat5下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
- jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
- jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
- 用jsp页面实现验证码出现getOutputStream() has already been called for this response
- 生成图片验证码jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
- 用jsp页面实现验证码出现getOutputStream() has already been called for this response
- tomcat5下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
- jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
- tomcat5下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
- tomcat5下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法