`

getOutputStream() has already been called for this response

阅读更多

        1.tomcat6.0下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法

    在tomcat6.0下jsp中出现此错误一般都是在jsp中使用了输出流(如输出图片验证码,文件下载等),没有妥善处理好的原因。

    具体的原因就是:
    在tomcat中jsp编译成servlet之后在函数_jspService(HttpServletRequest request, HttpServletResponse response)的最后有一段这样的代码

 

        finally {

                   if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);

}

 

       这里是在释放在jsp中使用的对象,会调用response.getWriter(),因为这个方法是和response.getOutputStream()相冲突的!所以会出现以上这个异常。

   然后当然是要提出解决的办法,其实挺简单的,在使用完输出流以后调用以下两行代码即可:

       out.clear();

       out = pageContext.pushBody();

 

       2.getOutputStream() has already been called for this response问题的解决

在jsp向页面输出图片的时候,使用response.getOutputStream()会有这样的提示:java.lang.IllegalStateException:getOutputStream() has already been called for this response,会抛出Exception

    原因一:
    JSP默认的输出流为PrintWriter ,即<% %>以外的东西所默认的输出方式,如果你尝试在JSP中使用ServletOutputStream就会引起错误.要嘛直接改用Servlet输出(复写service方法),要嘛删除除%><%中的任何东西(包括HTML标签,空格,回车等东西)应该就可以。对于这样的情况应该这样来解决,删除%><%之间的所有内容包括空格和换行符,最后也要消除空格和换行符,最好再加上一句response.reset()。
    原因二:    
    在J2EE的API参考里有这么个:

    ServletResponse的getWriter()方法里会抛出这个异常:

    IllegalStateException - if the getOutputStream method has already been called for this response object

    而它的getOutputStream()方法里会抛出这个异常:

    IllegalStateException - if the getOutputStream method has already been called for this response object ,并且两者的函数申明里都有这么样的一句
    Either this method or getOutputStream() may be called to write the body, not both.
    Either this method or getWriter() may be called to write the body, not both.


    以上说明也解释了为什么在往页面中写入图片的时候要使用如下循环格式
    OutputStream output=response.getOutputStream();
                           while((len=in.read(b)) >0) {
                                 output.write(b,0,len);
                           }
        output.flush();
        而不是把response.getOutputStream().write()放到循环体内

        在页面中直接写:

        <%
               response.getOutputStream();
         %>

         将会出现错误消息如下:
          java.lang.IllegalStateException: getOutputStream() has already been called for this response
          org.apache.catalina.connector.Response.getWriter(Response.java:604)
          org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
          org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
          org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)

分享到:
评论

相关推荐

    getOutputStream() has already been called for this response 错误解决

    在Java Web开发中,"getOutputStream() has already been called for this response" 是一个常见的错误,通常出现在使用Servlet或JSP时。这个错误意味着在HTTP响应中,`getOutputStream()`已经被调用,然后尝试再次...

    tomcat6下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法

    1.在tomcat6.0下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法  在tomcat6.0下jsp中出现此错误一般都是在jsp中使用了输出流(如输出图片验证码,文件下载等),没有...

    java.lang.IllegalStateException: getOutputStream() has already解决办法

    纠结了半天的 java.lang.IllegalStateException: getOutputStream() has already。这个问题困扰了半天,在网上查阅了大量资料 出这个错误一般就是下面2个.....

    验证码出现getOutputStream()问题解决

    验证码出现getOutputStream() has already been called for this response错误解决

    JSP彩色验证码

    解决了getOutputStream() has already been called for this response. 并将产生验证码的逻辑从JSP页面中分离出来,单独写了一个类 便于重用。

    handleStream ServletOutputStream out = response.getOutputStream(); InputStream i

    ServletOutputStream out = response.getOutputStream(); out.write("&lt;html&gt;&lt;body&gt;Hello, World!&lt;/body&gt;&lt;/html&gt;".getBytes()); out.flush(); out.close(); ``` 描述中的"InputStream i" 提到了输入流`InputStream`...

    response设置的实例源码

    本篇将深入探讨`response`设置的实例源码,以及如何在Servlet中应用这些设置。 一、Response对象的基本介绍 `HttpServletResponse`接口是`ServletResponse`接口的子接口,它扩展了通用的响应功能,以适应HTTP协议的...

    java.lang.IllegalStateException: OutputStream already obtain

    标题 "java.lang.IllegalStateException: OutputStream already obtain" 涉及到的是Java编程中的一个常见错误,特别是当处理I/O流时。这个异常通常在尝试获取已经存在的OutputStream实例时抛出,表明该输出流已经被...

    Response-Headers详解

    try (OutputStream out = response.getOutputStream()) { // 使用Apache POI或其他库生成Excel内容并写入out流 } ``` #### 五、动态生成JPEG图像 如果需要动态生成并发送JPEG格式的图像,也需要设置正确的响应...

    servlet2.4doc

    Returns a boolean indicating whether the named response header has already been set. contextDestroyed(ServletContextEvent) - Method in interface javax.servlet.ServletContextListener Notification ...

    java response 下载文件方法

    OutputStream ou = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); // 设置响应类型为二进制流 // 写入响应流并关闭 ou.write(buffer); ou....

    HttpServletRequest-response方法总结

    2. getOutputStream():从Servlet中可以通过getOutputStream方法取得ServletOutputStream对象,既可以输出字符数据,也可以输出MIME格式的二进制数据。 3. setContentType():在响应中可以表明内容格式和长度。 4. ...

    response生成图片验证

    这里使用了Servlet的`HttpServletResponse`对象,调用其`setContentType`方法设置响应的MIME类型为`image/jpeg`或`image/png`,然后使用`getOutputStream`获取输出流,并调用`ImageIO.write`方法将图片写入。...

    response jsp 中的重要知识点

    在Java Web开发中,`response.jsp`通常是指服务器端的响应页面,主要涉及Servlet和JSP(JavaServer Pages)技术。`response`对象是Servlet API中的一个关键组件,全称为`HttpServletResponse`,它用于构建并发送回...

    JSP内置对象request和response.doc

    JSP内置对象request和response详解 JSP内置对象request和response是JSP开发中两个非常重要的对象,它们分别用于处理客户端请求和响应信息。在本文档中,我们将详细介绍request和response对象的功能和使用方法。 ...

    response中文乱码解决的代码

    3. 使用`FileInputStream`读取文件内容,并通过`response.getOutputStream().write()`将内容写入响应流。 通过以上方法,我们可以在处理中文文件名的文件下载时,有效避免乱码问题,确保用户能够正确下载并识别文件...

    JAVAEE中Servlet实例Response与Request对象方法调用范例

    3. `getOutputStream()`:返回ServletOutputStream,用于写入二进制数据到响应体,如文件下载。 4. `getWriter()`:返回PrintWriter,用于写入文本数据到响应体,如HTML、JSON等。 在实际应用中,我们通常会创建一...

Global site tag (gtag.js) - Google Analytics