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