at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)7 Y+ a, p+ f8 S, ?( k2 [, q
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108) ( ~* O$ C7 o2 C3 h3 `0 q+ M
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151) - `; J" U! v" l+ |4 c0 z+ P9 w
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)- D: o7 X: E7 y
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)$ R9 |+ z g; n) A! V/ W& i5 A/ p
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528) 1 r O4 _* t) g$ G) }3 ?2 p( f3 i
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81) ! B9 S' \8 ^2 N7 I/ j1 L
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685) 2 [! X4 P8 V0 a. Y4 s
at java.lang.Thread.run(Thread.java:595)
复制代码
. u2 E- g7 ]( T1 u( ^8 k我们看到在JSP页面释放资源的时候,调用了ServetResponse.getWriter()方法,之后程序即抛出异常了,查看Servlet的API发现问题: 1 v1 z3 Q. q( D/ K/ p# Z* c3 `3 mpublic 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 o, t! l% ]$ S# W% V( L: SReturns: 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页面的内容,如果其中的一个方法被调用了,再调用另一个方法就会抛出异常。' d1 q: z. { Z& `1 Q, ]
( I- k) H; D) t8 s4 ~
解决方法如下: 7 _! d" P/ D/ A6 B9 Q! q0 r7 ^ # \- h6 q {% v9 i2 I+ t+ V3 @ 将JSP页面的最后两行代码的注释去掉,这两行代码的作用如下:+ M3 l9 x0 z3 @' w( ^8 O
% O: [; R8 n5 i2 V' D" ^9 P7 ?" N, ` out.clear():清空缓存的内容。 * \ Q2 }) j G1 G x" L, ^- ~$ q) h" Y) m1 ~# A pageContext.pushBody():参考API: J9 I2 l( |% K
* c8 q% I2 K1 t: o3 m( M; R
public BodyContentpushBody() * N/ a$ ^ i8 e5 h8 I7 [% a
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. # a9 f, G6 g/ l' J4 X6 IReturns: the new BodyContent·返回一个新的BodyContent(代表一个HTML页面的BODY部分内容) 9 f: E4 H+ p% P, ?2 ` g Y·保存JspWriter实例的对象out$ X0 W _( V" x! o
·更新PageContext的out属性的内容 " |" E" D) Z- p+ E