再用jsp进行图片的生成的时候,例如一些验证码,比例图的时候,在对图片进行输出调用 getOutputStream() 的时候会报该方法已经被调用的冲突。

例如如下的程序:

<%@ page contentType="image/jpeg" language="java"%>
<%@ page import="java.awt.image.*,javax.imageio.*,java.io.*,java.awt.*" %>
<%
 BufferedImage image=new BufferedImage(340,160,BufferedImage.TYPE_INT_RGB);
 Graphics g=image.getGraphics();
 g.fillRect(0, 0, 400, 400);
 g.setColor(new Color(255,0,0));
 g.fillArc(20, 20, 100, 100, 30, 120);
 g.setColor(new Color(0,255,0));
 g.fillArc(20, 20, 100, 100, 150, 120);
 g.setColor(new Color(0,0,255));
 g.fillArc(20, 20, 100, 100, 270, 120);
 g.setColor(new Color(0,0,0));
 g.setFont(new Font("Arial Black",Font.PLAIN,16));
 g.drawString("red:climb", 200, 60);
 g.drawString("green:swim", 200, 100);
 g.drawString("blue:jump", 200, 140);
 g.dispose();
 ImageIO.write(image, "jpg", response.getOutputStream());
%>

运行以后的结果是:

十二月 02, 2014 9:58:53 上午 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
十二月 02, 2014 9:58:53 上午 org.apache.catalina.startup.Catalina start
INFO: Server startup in 581 ms
十二月 02, 2014 9:59:00 上午 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/testt1] threw exception [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
    at org.apache.catalina.connector.Response.getWriter(Response.java:638)
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:214)
    at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
    at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
    at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:190)
    at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:126)
    at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:80)
    at org.apache.jsp.img_jsp._jspService(img_jsp.java:95)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)

出现这种情况的原因是因为在产生web容器的时候servlet代码中已经调用了out.write()的方法,所以我们在jsp页面上进行调用的时候系统会提示已经调用过了的重复请求的冲突。

解决的方法有以下的几个:

第一种直接在调用getOutputStream()之前对out方法进行清空,即调用out.clear()方法。

同时注意不要在%><%之间写内容包括空格和换行符

第二种是对于outputstream方法进行刷新即outputStream output=response.getOutputStream();
output.flush();

jsp中调用getOutputStream()产生冲突的更多相关文章

  1. 在Jsp中调用静态资源,路径配置问题,jsp获取路径的一些方法

    在Jsp中调用图片.JS脚本等,针对取得的路径有两种调用方式: 1.放入Body中生成绝对路径(不建议) <%@ page language="java" import=&q ...

  2. 在Jsp中调用静态资源,路径配置问题

    在Jsp中调用图片.JS脚本等,针对取得的路径有两种调用方式: 1.放入Body中生成绝对路径(不建议) <%@ page language="java" import=&q ...

  3. 在JSP中使用jQuery的冲突解决(收集整理)

    在JSP中使用<jsp:include page="somethingPage.jsp"></jsp>来嵌套页面的时候,会出现jQuery之间的冲突 解决办 ...

  4. JSP中调用Spring的方法

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  5. jsp中的out对象 和 servlet中的response.getOutputStream()

    web容器生成的servlet代码中有out.write(””),这个和JSP中调用的response.getOutputStream()产生冲突. 即Servlet规范说明,不能既调用 respon ...

  6. jsp实现文件下载,out = pageContext.pushBody();out.close();不用写到jsp中

    测试jsp: <%@ page contentType="text/html; charset=gbk" %> <% try{ com.enfo.intrust. ...

  7. JSP中setattribute与setParameter的区别

    HttpServletRequest类既有getAttribute()方法,也由getParameter()方法,这两个方法有以下区别: (1)HttpServletRequest类有setAttri ...

  8. JSP中getParameter和getAttribute区别

    (1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法 (2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter ...

  9. JSP中引用CSS样式文件却无法显示的问题解决方案

    你也遇到过这种问题吗,CSS写好了,JSP写好了,在JSP中调用CSS文件,路径检查后也正确,但是无法显示渲染后的页面 原因:罪魁祸首就是过滤器响应数据的时候,响应头设置为了“text/html”,但 ...

随机推荐

  1. LeetCode104: Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  2. magento目录结构精编版

    1 /app – 程序根目录 2 3 /app/etc – 全局配置文件目录 4 5 /app/code – 所有模块安装其模型和控制器的目录 6 7 /app/code/core – 核心代码或经过 ...

  3. junit学习(3.x)

    自动化测试 测试所有测试类 import junit.framework.TestCase; import junit.framework.Assert; /** *测试类必须要继承TestCase类 ...

  4. Holding Bin-Laden Captive!_hdu_1085(DP).java

    /*  * 9607741 2013-11-17 18:04:23 Accepted 1085 187MS 5700K 1251 B Java zhangyi  http://acm.hdu.edu. ...

  5. Decode放在where条件后的新用法

    今天遇到一种特殊情况的查询,在查询某表时,要通过判断其中一个字段的值再用其他字段作为条件查询,比如有3个字段 columnA,columnBm,columnC,columnA的值由两个——分别是0和1 ...

  6. 全代码实现ios-1

    第一次接触ios开发时,就决定用代码开发,而不用ib.因为被ib的各种控件的联线弄得一头雾水,而且ib和storyboard变动太快了. 开始的时候真是麻烦,因为网上关于全代码开发的例子太少了,大多数 ...

  7. PPT美化大师

    PPT美化大师 http://docer.mysoeasy.com/ PPT文档美化大师是一款专门为Office文档优化锦上添花的工具,该软件时尚个性打造出最漂亮的模板,有了这款软件在操作起来也简单许 ...

  8. Sending messages to non-windowed applications -- AllocateHWnd, DeallocateHWnd

    http://delphi.about.com/od/windowsshellapi/l/aa093003a.htm Page 1: How Delphi dispatches messages in ...

  9. Java数据结构之树和二叉树(2)

    从这里始将要继续进行Java数据结构的相关讲解,Are you ready?Let's go~~ Java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来 ...

  10. Why the framework uses ruby instead of perl?[转]

    During the development of the framework, the one recurring question that the Metasploit staff was co ...