我的日常

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

动态微博

查看: 1230|回复: 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)7 Y/ V" q4 B: n( \
  2.     at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:196)! f$ d7 E- `/ [4 F7 b
  3.     at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
    1 s2 K7 R5 q/ f6 @4 ~  x8 S( B
  4.     at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)- N- v% y9 Q- H
  5.     at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:185)7 b# i% T! ^" }" @" y" N& v
  6.     at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:116)
    5 H: ?1 |" ?2 I$ \" R
  7.     at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:76)) x+ u2 m9 A$ M1 z8 x6 O! c& A. ^7 }
  8.     at org.apache.jsp.pages.drawImage_jsp._jspService(drawImage_jsp.java:84). ?6 Y5 A0 a8 B) J$ l) X! c) N) \
  9.     at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
    - Y$ s" q  M& @. {
  10.     at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)3 v& F. [2 b5 ?1 V. D# n& e
  11.     at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
    ) L) N" C! D; ?& d3 X
  12.     at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
    ( [! {. C1 _7 b. h
  13.     at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    % r! q, f: J! Z7 ~+ Y: N( F
  14.     at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)7 ?! W- q% k2 z" a
  15.     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269), z. h- \0 E7 l, m
  16.     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    8 I# y: c: r/ u9 e. g0 K' [
  17.     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210), \4 I* O) U6 N; a  J; }
  18.     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
    7 r" k, l/ S) l8 H
  19.     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    % {2 {  x, A$ l8 q0 _2 G) |9 B2 [
  20.     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)/ [  ]- P! P$ V3 e
  21.     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)9 K( A. B% N' U' G
  22.     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)% G$ A( F. O: z* t5 |* K+ I/ y: U; a
  23.     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)# V& V' W/ G' U3 R0 g
  24.     at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
    5 r& \" A% F, c' w& F4 w
  25.     at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
    ( u- Y1 t; p; v$ K7 D
  26.     at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
    5 q! T3 l' n& Y, ~; E. ?
  27.     at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
    $ r% l0 O5 a2 A  g# W" N
  28.     at java.lang.Thread.run(Thread.java:595)
复制代码

8 c5 w" M5 |/ p8 g我们看到在JSP页面释放资源的时候,调用了ServetResponse.getWriter()方法,之后程序即抛出异常了,查看Servlet的API发现问题:  / q& T! H0 O: V/ f; |5 d
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.
* a; |) A3 |7 S- YReturns: 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页面的内容,如果其中的一个方法被调用了,再调用另一个方法就会抛出异常。- C6 P4 H$ s: d# R3 O7 r

8 l( G$ [: p! m3 j# W9 P解决方法如下:
* g" i$ V0 v/ Y$ a7 f+ B
6 i: c' M6 h0 ~4 x* |& k 将JSP页面的最后两行代码的注释去掉,这两行代码的作用如下:
- K* H; t& B% X( k
* P8 j/ @  R5 Z8 s) W; ^7 `/ X( ~out.clear():清空缓存的内容。
4 p* K3 `! f! H9 i' D* X! p  t: N: B' Z+ I. Y# i, i7 P1 _. o0 J
pageContext.pushBody():参考API2 |3 y  f) F3 R" n
/ f# j  }. Z2 f  z: }" z4 O
public BodyContent pushBody()
& k5 \# A7 y* k! k: ^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.
5 _, f) E2 G# X7 P' }$ g/ lReturns: the new BodyContent·返回一个新的BodyContent(代表一个HTML页面的BODY部分内容)
1 K6 v+ [' N* [) U·保存JspWriter实例的对象out
7 Y  b6 j* ~" D0 K/ n1 ? ·更新PageContext的out属性的内容

+ H) N2 s7 o5 J: z( o4 r8 _) ^

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


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

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

   

关闭

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

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