我的日常

登录/注册
您现在的位置:论坛 盖世程序员(我猜到了开头 却没有猜到结局) 盖世程序员 > getOutputStream() has already been called for this r ...
总共48086条微博

动态微博

查看: 1232|回复: 0

getOutputStream() has already been called for this response

[复制链接]

114

主题

29

听众

429

金钱

传功长老

该用户从未签到

跳转到指定楼层
楼主
发表于 2014-09-22 14:28:12 |只看该作者 |倒序浏览
  1. at org.apache.catalina.connector.Response.getWriter(Response.java:601)  L. S/ x  x" c2 x+ U, P
  2.     at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:196)1 Q0 T3 c# _# Z/ T' z
  3.     at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
    $ `4 u+ j1 y+ c) q" e. ]: M5 F
  4.     at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
    * h8 ~( {! j# o. l
  5.     at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:185)
    / p3 v8 |0 v+ D7 y1 b0 s/ L
  6.     at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:116)
    $ l  _% F6 v' \' y
  7.     at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:76)
    5 t* n$ S- Q5 N$ ~  T& b
  8.     at org.apache.jsp.pages.drawImage_jsp._jspService(drawImage_jsp.java:84)
    , W4 i( H+ K% O( d2 k+ x* ^
  9.     at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)* X8 N/ w/ d3 p' L. b
  10.     at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)" L& T  q2 a) Y" i
  11.     at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)8 ^# t0 C8 _! j: l
  12.     at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)# P" s, t) `+ O8 J$ N; @
  13.     at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)* h& q+ X4 k; f3 C2 A
  14.     at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)0 T6 m' Z  i- g9 s/ J" n
  15.     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    4 o3 \4 k5 V) D3 Y  w9 L
  16.     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    4 r- h# c* b: T
  17.     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
    * r' }- @4 f1 L* P7 w, I- n
  18.     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)2 D, A$ m0 n& I0 n$ i. Y. _
  19.     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    1 t) t; }# D  r: {7 N( d
  20.     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
    5 u3 `2 i# D6 {- S6 H. H3 Z
  21.     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
    & m1 T& o& F6 W/ F- f( o
  22.     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
    . W0 k6 }1 K# L! j8 c& l/ ?4 ]
  23.     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)8 A- ?' n) f' C! W7 K4 w. u
  24.     at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)* s) x: [3 P' @7 V7 y* b/ j
  25.     at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
    ' D* D2 ~. \; [; I
  26.     at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
    " R0 L. {5 Z- M
  27.     at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)% w( [$ l7 T% K5 s, ]( D
  28.     at java.lang.Thread.run(Thread.java:595)
复制代码

& L% p/ y  \0 u我们看到在JSP页面释放资源的时候,调用了ServetResponse.getWriter()方法,之后程序即抛出异常了,查看Servlet的API发现问题:  # Y& m4 T0 J) S: x6 E* z! U3 k% z7 l
public java.io.PrintWriter getWriter()throws java.io.IOExceptionReturns 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.
# R7 _  I/ g. c/ R3 ?/ w, ^- HReturns: 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页面的内容,如果其中的一个方法被调用了,再调用另一个方法就会抛出异常。
7 E' g! X& J/ |6 p8 y1 M: t7 _& s8 [+ h
解决方法如下:
: c1 T; f3 m# C& P# q7 r/ F5 u
" h4 E' a% \9 N4 m. u 将JSP页面的最后两行代码的注释去掉,这两行代码的作用如下:3 C4 U6 h" r) c9 Q% }! a3 @6 ~9 o

2 F- X6 u; W5 _out.clear():清空缓存的内容。
. x9 d& O1 H: O1 z5 w' ~. y8 Q
$ L: L6 h  |+ V: ppageContext.pushBody():参考API2 t! t- D+ t. S
+ a; T& n. q& [' t6 B5 d5 Y
public BodyContent pushBody() . b/ }9 A; a. C8 t: }+ |
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. 2 @9 s9 u5 R4 {# Q
Returns: the new BodyContent·返回一个新的BodyContent(代表一个HTML页面的BODY部分内容)5 s! }- J  _: A. j/ n+ F( M" Q
·保存JspWriter实例的对象out
# J5 y  M4 u4 {" s& G: N ·更新PageContext的out属性的内容
$ j/ @0 D: g) Y' W, d" Y

科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关
2、本站所有主题由该帖子作者发表,该帖子作者与科帮网享有帖子相关版权
3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网的同意
4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
7、科帮网管理员和版主有权不事先通知发贴者而删除本文


JAVA爱好者①群:JAVA爱好者① JAVA爱好者②群:JAVA爱好者② JAVA爱好者③ : JAVA爱好者③

快速回复
您需要登录后才可以回帖 登录 | 立即注册

   

关闭

站长推荐上一条 /1 下一条

发布主题 快速回复 返回列表 联系我们 官方QQ群 科帮网手机客户端
快速回复 返回顶部 返回列表