• java.lang.IllegalStateException: getOutputStream()

    普通类
    • 支持
    • 批判
    • 提问
    • 解释
    • 补充
    • 删除
    • 问题描述

    学习元tomcat后台报错如下:

    2012-4-100:45:57 org.apache.catalina.core.StandardWrapperValve invoke

    严重:Servlet.service() for servlet jsp threw exception

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

    atorg.apache.catalina.connector.Response.getWriter(Response.java:611)

    atorg.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)

    atjavax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)

    atorg.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)

    atorg.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)

    atorg.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:188)

    atorg.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:118)

    atorg.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:77)

    atorg.apache.jsp.pageComp.authcode_jsp._jspService(authcode_jsp.java:117)

    atorg.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)

    atjavax.servlet.http.HttpServlet.service(HttpServlet.java:717)

    atorg.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)

    atorg.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)

    atorg.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)

    atjavax.servlet.http.HttpServlet.service(HttpServlet.java:717)

    atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)

    atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

    atorg.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)

    atorg.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)

    atorg.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)

    atorg.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)

    atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

    atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

    atetc.struts.filter.ChooseWebsiteFilter.doFilter(ChooseWebsiteFilter.java:295)

    atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

    atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

    athep.webapp.struts.EncodingFilter.doFilter(EncodingFilter.java:48)

    atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

    atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

    athep.webapp.struts.CheckAdminLoginFilter.doFilter(CheckAdminLoginFilter.java:155)

    atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

    atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

    athep.webapp.struts.CheckLoginFilter.doFilter(CheckLoginFilter.java:356)

    atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

    atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

    atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)

    atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)

    atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)

    atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

    atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

    atorg.apache.catalina.ha.session.JvmRouteBinderValve.invoke(JvmRouteBinderValve.java:227)

    atorg.apache.catalina.ha.tcp.ReplicationValve.invoke(ReplicationValve.java:347)

    atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)

    atorg.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)

    atorg.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:291)

    atorg.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:776)

    atorg.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:705)

    atorg.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:898)

    atorg.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)

    atjava.lang.Thread.run(Thread.java:619)

     

    • 原因分析

    根据提示“java.lang.IllegalStateException:getOutputStream() has already been called for this response”可知,引起错误的原因是方法:getOutputStream()被调用之后再次被调用所导致。

    根据错误提示查看相应的jsp页面authcode.jsp,发现最后一行如下:

    jsp页面中调用getOutputStream()为什么会有错误呢?先补充两个知识点:

    1. tomcat会将jsp页面先转换成servlet,对于jsp页面中<%%>之外的东西,jsp默认的输出流为Printwriter。并且jsp页面对应的servlet最后生成以下代码:

    这里是在释放在jsp中使用的对象,会调用response.getWriter()

    1. J2EEAPI参考里有这么个:

    ServletResponsegetWriter()方法里会抛出这个异常,

    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.

    基于以上两点,可以看出不能在一个servlet中同时调用getWriter()getOutputStream()方法。

    所以导致上述异常的原因就是jsp页面最后的:

     

    • 问题解决

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

    out.clear();

    out= pageContext.pushBody();

    这两句话我理解的也不是很好。查看api,其中说:

    out.clear();

    Clear the contents of the buffer. If the buffer has been already been flushed then the clear operation shall throw an IOException to signal the fact that some data has already been irrevocably written to the client response stream.

    out= pageContext.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.

    修改后的jsp页面最后的代码为:

     

    • 在新页面中查看内容
    • 下载源文件
    • 标签:
    • atorg.apache.catalina.core.applicationfilterchain.dofilter
    • 调用
    • called
    • 235
    • jsp
    • 206
    • applicationfilterchain.java
    • getoutputstream
    • 页面
    • atorg.apache.catalina.core.applicationfilterchain.internaldofilter
  • 加入的知识群:
    学习元评论 (0条)

    评论为空
    聪明如你,不妨在这 发表你的看法与心得 ~



    登录之后可以发表学习元评论
      
暂无内容~~
顶部