我的日常

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

动态微博

查看: 1245|回复: 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)
    * a2 p! S# j( F( T7 k
  2.     at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:196)
    # t4 X; L$ B7 [: e
  3.     at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
    / V" D' U" X7 ]% w0 c8 w; `. `
  4.     at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118). [3 M& i; F+ M9 K' W- ]* M
  5.     at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:185)
    ' i$ P2 [: c& }/ {5 Z4 C4 M
  6.     at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:116)
    1 K/ {" o- W8 S7 [% i$ I
  7.     at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:76)
    , h9 J9 [9 I2 S8 B2 Z& l
  8.     at org.apache.jsp.pages.drawImage_jsp._jspService(drawImage_jsp.java:84)
    $ q, a; ^: U# Q3 X# [
  9.     at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)4 h1 t7 a+ A& ^( \; _8 q
  10.     at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    3 S3 X& s5 B& O0 l. o! X: R' O
  11.     at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
    # }: l) `) G0 G  `% Q
  12.     at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315), M: V8 m) s" l8 n/ Q; Q  B
  13.     at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)9 v% _# h3 E" e6 i" ~: W, @+ W% k
  14.     at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)0 C- N9 g4 {. @7 o+ v, e1 B
  15.     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    3 z, q7 ^5 q: ?: z' X
  16.     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    + Y, ~+ c6 i& \/ N2 b: c& f  Y
  17.     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)) q% J" j# S" P+ W
  18.     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
    - `9 u# S' v. Y$ l
  19.     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    : Y4 @6 G: [7 q; Y. \, q
  20.     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
    & |; N4 _! f) g( c3 E
  21.     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
    $ g; P; t; k. q. S
  22.     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)  s5 {2 x+ ]" s5 g6 d0 ~
  23.     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
    % r6 N, \/ q2 C7 H! C# m, ~: d
  24.     at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)2 _% \& F/ a) a8 A8 S0 b
  25.     at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)' \  }! Y( u% D/ R, L. g
  26.     at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
    ' s; ~9 H0 p# @$ y
  27.     at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
    . @$ a6 z6 k+ [1 _$ ~: o5 k
  28.     at java.lang.Thread.run(Thread.java:595)
复制代码

! z7 P( j) [) b我们看到在JSP页面释放资源的时候,调用了ServetResponse.getWriter()方法,之后程序即抛出异常了,查看Servlet的API发现问题:  
: H: F3 X& v, }% m0 M4 @1 R% T( ypublic 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. 7 C  ~" {' D/ i: P
Returns: 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页面的内容,如果其中的一个方法被调用了,再调用另一个方法就会抛出异常。+ q9 f$ |7 k0 L6 s& d+ F, h
& @- K0 w9 G8 H
解决方法如下:/ q1 }2 a1 G9 _

& N: A) v7 u, u! D  b. [ 将JSP页面的最后两行代码的注释去掉,这两行代码的作用如下:" |7 l" }3 c: S# Z( @+ ?3 }" E
4 I% e& l. T( M, Q
out.clear():清空缓存的内容。
1 Q# V% l$ w- Y  w
% p/ n" u6 M' Y/ ^pageContext.pushBody():参考API
6 {1 i2 b) b* B

; H) _: O9 R# S) [ public BodyContent pushBody()
0 B8 N! M5 l  K% [4 b+ zReturn 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.
8 B! u$ z5 D: XReturns: the new BodyContent·返回一个新的BodyContent(代表一个HTML页面的BODY部分内容)0 F/ D9 ]; ^0 X, v/ ^) i
·保存JspWriter实例的对象out
$ K5 O' V; E+ L3 O4 c ·更新PageContext的out属性的内容
. f4 R( E: ~/ T

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


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

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

   

关闭

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

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