at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)/ [ ]- P! P$ V3 e
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)9 K( A. B% N' U' G
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)% G$ A( F. O: z* t5 |* K+ I/ y: U; a
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)# V& V' W/ G' U3 R0 g
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665) 5 r& \" A% F, c' w& F4 w
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528) ( u- Y1 t; p; v$ K7 D
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81) 5 q! T3 l' n& Y, ~; E. ?
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685) $ r% l0 O5 a2 A g# W" N
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 BodyContentpushBody() & 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 _) ^