首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

getOutputStream() has already been called for this response的解决办法

2012-11-15 
getOutputStream() has already been called for this response的解决方法%??BufferedImage?image??new?

getOutputStream() has already been called for this response的解决方法

<%
getOutputStream() has already been called for this response的解决办法??BufferedImage?image?=?new?BufferedImage(400,?400,?BufferedImage.TYPE_INT_RGB);
getOutputStream() has already been called for this response的解决办法??Graphics?g?=?image.getGraphics();
getOutputStream() has already been called for this response的解决办法??g.fillRect(0,0,400,400);
getOutputStream() has already been called for this response的解决办法??g.setColor(new?Color(255,0,0));??
getOutputStream() has already been called for this response的解决办法??g.fillArc(20,20,100,100,30,120);
getOutputStream() has already been called for this response的解决办法??g.setColor(new?Color(0,255,0));
getOutputStream() has already been called for this response的解决办法??g.fillArc(20,20,100,100,150,20);
getOutputStream() has already been called for this response的解决办法??g.setColor(new?Color(0,0,255));
getOutputStream() has already been called for this response的解决办法??g.fillArc(20,20,100,100,270,120);
getOutputStream() has already been called for this response的解决办法??g.setColor(new?Color(0,0,0));
getOutputStream() has already been called for this response的解决办法??g.drawString("red:climb"?,?300,?80);
getOutputStream() has already been called for this response的解决办法??g.drawString("green:swim",?300,?120);
getOutputStream() has already been called for this response的解决办法??g.drawString("blue:jump",?300,?160);
getOutputStream() has already been called for this response的解决办法??ImageIO.write(image,?"bmp",?response.getOutputStream());
getOutputStream() has already been called for this response的解决办法??//out.clear();
getOutputStream() has already been called for this response的解决办法??//out?=?pageContext.pushBody();
getOutputStream() has already been called for this response的解决办法%>
在Tomcat下运行时抛出如下异常:
getOutputStream() has already been called for this response的解决办法
getOutputStream() has already been called for this response的解决办法????at?org.apache.catalina.connector.Response.getWriter(Response.java:601)
getOutputStream() has already been called for this response的解决办法????at?org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:196)
getOutputStream() has already been called for this response的解决办法????at?org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
getOutputStream() has already been called for this response的解决办法????at?org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
getOutputStream() has already been called for this response的解决办法????at?org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:185)
getOutputStream() has already been called for this response的解决办法????at?org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:116)
getOutputStream() has already been called for this response的解决办法????at?org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:76)
getOutputStream() has already been called for this response的解决办法????at?org.apache.jsp.pages.drawImage_jsp._jspService(drawImage_jsp.java:84)
getOutputStream() has already been called for this response的解决办法????at?org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
getOutputStream() has already been called for this response的解决办法????at?javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
getOutputStream() has already been called for this response的解决办法????at?org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
getOutputStream() has already been called for this response的解决办法????at?org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
getOutputStream() has already been called for this response的解决办法????at?org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
getOutputStream() has already been called for this response的解决办法????at?javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
getOutputStream() has already been called for this response的解决办法????at?org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
getOutputStream() has already been called for this response的解决办法????at?org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
getOutputStream() has already been called for this response的解决办法????at?org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
getOutputStream() has already been called for this response的解决办法????at?org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
getOutputStream() has already been called for this response的解决办法????at?org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
getOutputStream() has already been called for this response的解决办法????at?org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
getOutputStream() has already been called for this response的解决办法????at?org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
getOutputStream() has already been called for this response的解决办法????at?org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
getOutputStream() has already been called for this response的解决办法????at?org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
getOutputStream() has already been called for this response的解决办法????at?org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
getOutputStream() has already been called for this response的解决办法????at?org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
getOutputStream() has already been called for this response的解决办法????at?org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
getOutputStream() has already been called for this response的解决办法????at?org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
getOutputStream() has already been called for this response的解决办法????at?java.lang.Thread.run(Thread.java:595)
查看转换后的JSP代码,发现第84行如下(绿色高亮处):

getOutputStream() has already been called for this response的解决办法??public?void?_jspService(HttpServletRequest?request,?HttpServletResponse?response)
getOutputStream() has already been called for this response的解决办法????????throws?java.io.IOException,?ServletException?{
getOutputStream() has already been called for this response的解决办法
getOutputStream() has already been called for this response的解决办法????JspFactory?_jspxFactory?=?null;
getOutputStream() has already been called for this response的解决办法????PageContext?pageContext?=?null;
getOutputStream() has already been called for this response的解决办法????HttpSession?session?=?null;
getOutputStream() has already been called for this response的解决办法????ServletContext?application?=?null;
getOutputStream() has already been called for this response的解决办法????ServletConfig?config?=?null;
getOutputStream() has already been called for this response的解决办法????JspWriter?out?=?null;
getOutputStream() has already been called for this response的解决办法????Object?page?=?this;
getOutputStream() has already been called for this response的解决办法????JspWriter?_jspx_out?=?null;
getOutputStream() has already been called for this response的解决办法????PageContext?_jspx_page_context?=?null;
getOutputStream() has already been called for this response的解决办法
getOutputStream() has already been called for this response的解决办法
getOutputStream() has already been called for this response的解决办法????try?{
getOutputStream() has already been called for this response的解决办法??????_jspxFactory?=?JspFactory.getDefaultFactory();
getOutputStream() has already been called for this response的解决办法??????response.setContentType("text/html;?charset=UTF-8");
getOutputStream() has already been called for this response的解决办法??????pageContext?=?_jspxFactory.getPageContext(this,?request,?response,
getOutputStream() has already been called for this response的解决办法??????????????????null,?true,?8192,?true);
getOutputStream() has already been called for this response的解决办法??????_jspx_page_context?=?pageContext;
getOutputStream() has already been called for this response的解决办法??????application?=?pageContext.getServletContext();
getOutputStream() has already been called for this response的解决办法??????config?=?pageContext.getServletConfig();
getOutputStream() has already been called for this response的解决办法??????session?=?pageContext.getSession();
getOutputStream() has already been called for this response的解决办法??????out?=?pageContext.getOut();
getOutputStream() has already been called for this response的解决办法??????_jspx_out?=?out;
getOutputStream() has already been called for this response的解决办法
getOutputStream() has already been called for this response的解决办法??????out.write("\r\n");
getOutputStream() has already been called for this response的解决办法??????out.write("\n");
getOutputStream() has already been called for this response的解决办法??????out.write("<!DOCTYPE?html?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?"http://www.w3.org/TR/html4/loose.dtd">\n");
getOutputStream() has already been called for this response的解决办法??????out.write("<html>\n");
getOutputStream() has already been called for this response的解决办法??????out.write("<head>\n");
getOutputStream() has already been called for this response的解决办法??????out.write("<meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8">\n");
getOutputStream() has already been called for this response的解决办法??????out.write("<title>Draw?Image</title>\n");
getOutputStream() has already been called for this response的解决办法??????out.write("</head>\n");
getOutputStream() has already been called for this response的解决办法??????out.write("<body>\n");
getOutputStream() has already been called for this response的解决办法??????out.write("\r\n");
getOutputStream() has already been called for this response的解决办法
getOutputStream() has already been called for this response的解决办法??BufferedImage?image?=?new?BufferedImage(400,?400,?BufferedImage.TYPE_INT_RGB);
getOutputStream() has already been called for this response的解决办法??Graphics?g?=?image.getGraphics();
getOutputStream() has already been called for this response的解决办法??g.fillRect(0,0,400,400);
getOutputStream() has already been called for this response的解决办法??g.setColor(new?Color(255,0,0));??
getOutputStream() has already been called for this response的解决办法??g.fillArc(20,20,100,100,30,120);
getOutputStream() has already been called for this response的解决办法??g.setColor(new?Color(0,255,0));
getOutputStream() has already been called for this response的解决办法??g.fillArc(20,20,100,100,150,20);
getOutputStream() has already been called for this response的解决办法??g.setColor(new?Color(0,0,255));
getOutputStream() has already been called for this response的解决办法??g.fillArc(20,20,100,100,270,120);
getOutputStream() has already been called for this response的解决办法??g.setColor(new?Color(0,0,0));
getOutputStream() has already been called for this response的解决办法??g.drawString("red:climb"?,?300,?80);
getOutputStream() has already been called for this response的解决办法??g.drawString("green:swim",?300,?120);
getOutputStream() has already been called for this response的解决办法??g.drawString("blue:jump",?300,?160);
getOutputStream() has already been called for this response的解决办法??ImageIO.write(image,?"bmp",?response.getOutputStream());
getOutputStream() has already been called for this response的解决办法??//out.clear();
getOutputStream() has already been called for this response的解决办法??//out?=?pageContext.pushBody();
getOutputStream() has already been called for this response的解决办法
getOutputStream() has already been called for this response的解决办法??????out.write("\r\n");
getOutputStream() has already been called for this response的解决办法??????out.write("</body>\n");
getOutputStream() has already been called for this response的解决办法??????out.write("</html>");
getOutputStream() has already been called for this response的解决办法????}?catch?(Throwable?t)?{
getOutputStream() has already been called for this response的解决办法??????if?(!(t?instanceof?SkipPageException)){
getOutputStream() has already been called for this response的解决办法????????out?=?_jspx_out;
getOutputStream() has already been called for this response的解决办法????????if?(out?!=?null?&&?out.getBufferSize()?!=?0)
getOutputStream() has already been called for this response的解决办法??????????out.clearBuffer();
getOutputStream() has already been called for this response的解决办法????????if?(_jspx_page_context?!=?null)?_jspx_page_context.handlePageException(t);
getOutputStream() has already been called for this response的解决办法??????}
getOutputStream() has already been called for this response的解决办法????}?finally?{
getOutputStream() has already been called for this response的解决办法??????if?(_jspxFactory?!=?null)?_jspxFactory.releasePageContext(_jspx_page_context);
getOutputStream() has already been called for this response的解决办法????}
getOutputStream() has already been called for this response的解决办法??}
我们看到在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 clientThrows:UnsupportedEncodingException?- if the character encoding returned by?getCharacterEncoding?cannot be usedjava.lang.IllegalStateException?- if the?getOutputStream?method has already been called for this response objectjava.io.IOException?- if an input or output exception occurredSee 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属性的内容

热点排行