`
jordan_micle
  • 浏览: 245172 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

java.lang.IllegalStateException: getOutputStream()

阅读更多

在做图形验证码的时候随便从网上找了段代码,结果出现了这个异常,详细内容如下:

[http-8080-Processor25] ERROR: org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/xixibar].[jsp]#invoke : Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
    at org.apache.catalina.connector.Response.getWriter(Response.java:599)
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:195)
    at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:124)
    at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:117)
    at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:182)
    at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:115)
    at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75)
    at org.apache.jsp.image_jsp._jspService(image_jsp.java:105)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
    at java.lang.Thread.run(Thread.java:595)

 

刚开始都没看明白怎么回事,后来找Google帮忙,找到如下解释:

 

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

具体的原因就是在tomcat中jsp编译成servlet之后在函数_jspService(HttpServletRequest request, HttpServletResponse response) 的最后有一段这样的代码
finally {
    if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
}

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

然后当然是要提出解决的办法,其实挺简单的(并不是和某些朋友说的那样
--将jsp内的所有空格和回车符号所有都删除掉),

在使用完输出流以后调用以下两行代码即可:

out.clear();
out = pageContext.pushBody();

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics