2012-12-31 8:59:21 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
java.io.IOException: Stream closed
...
百思不得其解,翻出 jsp 转成 servlet 后的代码。如下(很很的醒目一下):
...
}catch(Exception e){
e.printStackTrace();
}finally{
out.flush(); //
out.close();// 此处为源始代码
DBHelper.freeConnection(connection);
}
out.write('\r'); // 如上我已经关了 out 对象,但此处还在使用,所以便产生了如开始所描述的异常
out.write('\n');
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try { out.clearBuffer(); } catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
...
解决办法:把程序中加红加粗的代码改成:
out.flush() ;
out = pageContext.pushBody(); // 关于该段程序的解释,doc中已经说的很清楚。
如下:(要特别注意一下flush()和clear()方法的区别,因为需求不同程序是不同的)
abstract void |
flush() Flush the stream. |
abstract void |
clear() Clear the contents of the buffer. |
PageContext 实现了抽象类 JspContext ,方法:pushBody(), 保存当前的out对象
BodyContent |
pushBody() 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. |
public abstract class BodyContent
extends JspWriter
out 内置对象