getOutputStream() has already been called for this

<%

BufferedImage image = new BufferedImage(400, 400, 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,20);

g.setColor(new Color(0,0,255));

g.fillArc(20,20,100,100,270,120);

g.setColor(new Color(0,0,0));

g.drawString("red:climb" , 300, 80);

g.drawString("green:swim", 300, 120);

g.drawString("blue:jump", 300, 160);

ImageIO.write(image, "bmp", response.getOutputStream());

//out.clear();

//out = pageContext.pushBody();

%>

在Tomcat下运行时抛出如下异常:

at org.apache.catalina.connector.Response.getWriter(Response.java:601)

at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:196)

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:185)

at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:116)

at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:76)

at org.apache.jsp.pages.drawImage_jsp._jspService(drawImage_jsp.java:84)

at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)

at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)

at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)

at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)

at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)

at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)

at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)

at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)

at java.lang.Thread.run(Thread.java:595)

查看转换后的JSP代码,发现第84行如下(绿色高亮处):

public void _jspService(HttpServletRequest request, HttpServletResponse response)

throws java.io.IOException, ServletException {

JspFactory _jspxFactory = null;

PageContext pageContext = null;

HttpSession session = null;

ServletContext application = null;

ServletConfig config = null;

JspWriter out = null;

Object page = this;

JspWriter _jspx_out = null;

PageContext _jspx_page_context = null;

try {

_jspxFactory = JspFactory.getDefaultFactory();

response.setContentType("text/html; charset=UTF-8");

pageContext = _jspxFactory.getPageContext(this, request, response,

null, true, 8192, true);

_jspx_page_context = pageContext;

application = pageContext.getServletContext();

config = pageContext.getServletConfig();

session = pageContext.getSession();

out = pageContext.getOut();

_jspx_out = out;

out.write("\r\n");

out.write("\n");

out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");

out.write("<html>\n");

out.write("<head>\n");

out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");

out.write("<title>Draw Image</title>\n");

out.write("</head>\n");

out.write("<body>\n");

out.write("\r\n");

BufferedImage image = new BufferedImage(400, 400, 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,20);

g.setColor(new Color(0,0,255));

g.fillArc(20,20,100,100,270,120);

g.setColor(new Color(0,0,0));

g.drawString("red:climb" , 300, 80);

g.drawString("green:swim", 300, 120);

g.drawString("blue:jump", 300, 160);

ImageIO.write(image, "bmp", response.getOutputStream());

//out.clear();

//out = pageContext.pushBody();

out.write("\r\n");

out.write("</body>\n");

out.write("</html>");

} catch (Throwable t) {

if (!(t instanceof SkipPageException)){

out = _jspx_out;

if (out != null && out.getBufferSize() != 0)

out.clearBuffer();

if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);

}

} finally {

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

}

}

我们看到在JSP页面释放资源的时候,调用了ServetResponse.getWriter()方法,之后程序即抛出异常了,查看Servlet的API发现问题:

public java.io.PrintWriter getWriter()

throws java.io.IOException

Returns a PrintWriter object that can send character text to the client. The PrintWriter uses the character encoding returned by getCharacterEncoding(). If the response‘s character encoding has not been specified as described in getCharacterEncoding (i.e., the method just returns the default value ISO-8859-1), getWriter updates it to ISO-8859-1.

Calling flush() on the PrintWriter commits the response.

Either this method or getOutputStream() may be called to write the body, not both.

Returns:

a PrintWriter object that can return character data to the client

Throws:

UnsupportedEncodingException - if the character encoding returned by getCharacterEncoding cannot be used

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

java.io.IOException - if an input or output exception occurred

See Also:

getOutputStream(), setCharacterEncoding(java.lang.String)

如API所言,由于ServletResponse.getOutputStream()方法和该方法都有可能被调用,来输出JSP页面的内容,如果其中的一个方法被调用了,再调用另一个方法就会抛出异常。

解决方法如下:

将JSP页面的最后两行代码的注释去掉,这两行代码的作用如下:

out.clear():清空缓存的内容。

pageContext.pushBody():参考API

public BodyContent pushBody()

Return a new BodyContent object, save the current "out" JspWriter, and update the value of the "out" attribute in the page scope attribute namespace of the PageContext.

Returns:

the new BodyContent

·返回一个新的BodyContent(代表一个HTML页面的BODY部分内容)

·保存JspWriter实例的对象out

·更新PageContext的out属性的内容

时间: 2024-10-05 05:07:37

getOutputStream() has already been called for this的相关文章

Caused by: java.lang.IllegalStateException: getOutputStream() has already been called for this respo

1.错误描述 Caused by: java.lang.IllegalStateException: getOutputStream() has already been called for this response at org.apache.catalina.connector.Response.getWriter(Response.java:648) at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFa

在Struts2中使用poi进行excel操作下载的时候报getOutputStream() has already been called for this response 错误 [转]

在项目中用到了poi这个开源的操作excel文件的jar. 项目中用到struts2容器管理servlet.不是单纯的直接用servlet.         workbook.write(os);           os.flush();            os.close();           return "SUCCESS";  在我的action中用是这样处理最后的传出.但是报出了: java.lang.IllegalStateException: getOutputS

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

在使用tomcat6开发的过程中,由于要输出数据统计图,在后台每次生成图形到前台显示时,总是抛出异常,内容显示有: getOutputStream() has already been called for this response 在百度一搜一大把,可见很多人都遇到过类似的问题,解决办法也差不多,在前台使用完输出流以后调用以下两行代码即可: out.clear(); out = pageContext.pushBody(); 但是我们的工程是在后台使用getOutputStream(),因此

jsp中使用out和response.getOutputStream的方法

jsp页面生成的servlet代码中,out为其内置对象,并且是通过response.getWriter()得到的,功能像页面展示一些内容,如out.writer()方法,但是在servlet规范中指出,response.getOutputStream()和response.getWriter()方法会产生冲突,不能同时存在,无论先调用哪一个,在调用第二个时候应会抛出 IllegalStateException: 解决方案: 在程序中添加: out.clear(); out = pageCont

JSP文件下载及出现getOutputStream() has already been called for this response的解决方法

http://iamin.blogdriver.com/iamin/1072546.html 一.采用RequestDispatcher的方式进行 1.web.xml文件中增加  <mime-mapping>    <extension>doc</extension>    <mime-type>application/vnd.ms-word</mime-type>  </mime-mapping> 2.程序如下: <%@pag

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

web容器生成的servlet代码中有out.write(””),这个和JSP中调用的response.getOutputStream()产生冲突. 即Servlet规范说明,不能既调用 response.getOutputStream(),又调用response.getWriter(),无论先调用哪一个,在调用第二个时候应会抛出 IllegalStateException,因为在jsp中,out变量是通过response.getWriter得到的,在程序中既用了response.getOutp

getOutputStream() has already been called for this response解释以及解决方法

异常:getOutputStream() has already been called for this response 的解决方法 今天在第一次接触使用“验证码”功能时,在执行时出现了异常信息: 严重: Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: getOutputStream() has already been called for this response ..

java -- getOutputStream() has already been called for

原文:https://my.oschina.net/zhongwenhao/blog/209653 原因:既调用了response.getOutputStream(),又调用了response.getWriter(). 因此:在同一流程分支中不能同时存在两者.

java.lang.IllegalStateException: getOutputStream() has already been called for this response

警示:有bug应该仔细查看异常代码,里面说明了问题原因. at org.apache.catalina.connector.Response.getWriter(Response.java:604) at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198) ERROR [Engine] StandardWrapperValve[jsp]: Servlet.service() for ser

【Android开发】 HttpURLConnection.getOutputStream() IO异常

HttpURLConnection.getOutputStream()  IO异常百度下,没找到想要的答案.网上的解决方案几乎都是从权限考虑的~最后找到个国外网站上找到答案~ http://stackoverflow.com/questions/23582621/httpurlconnection-getoutputstream-throws-ioexception 其实报IO异常就是说,你用UI的进程访问网络,并加入POST数据,导致异常.这时你需要将你访问网络的代码写入一个线程中就没不会有这