简体   繁体   English

为什么HttpServlet抛出IOException?

[英]Why does HttpServlet throw an IOException?

I get why an HttpServlet would throw ServletException, but why IOException? 我知道为什么HttpServlet会抛出ServletException,但是为什么会出现IOException? What was the reasoning behind this? 这背后的原因是什么?

From the docs: 从文档:

IOException - if an input or output error is detected when the servlet handles the GET request IOException-如果在Servlet处理GET请求时检测到输入或输出错误

This can happen when you print the servlet output: 当您打印servlet输出时,可能会发生这种情况:

response.getWriter().print() - this method throws IOException response.getWriter()。print()-此方法抛出IOException

The socket can be closed before the response finishes to print the output. 可以在响应结束之前关闭插座,以打印输出。

我建议IOException指示处理输入/输出有问题,例如从请求输入读取或写入响应的问题,而ServletException与servlet特定的问题更多有关,例如有关servlet设置/初始化的错误。 ,并处理请求。

If the servlet writes to an output stream (ie the page) through I/O interfaces, any I/O error will be reported as an IOException . 如果servlet通过I / O接口写入输出流(即页面),则任何I / O错误都将报告为IOException

Perhaps the API shouldn't be using I/O interfaces directly. 也许API不应该直接使用I / O接口。 It's not uncommon for WebApps using RMI to allow the RMIException to be fall through the servlet interface (it's a subclass of IOException for some reason). 对于使用RMI的RMIException ,允许RMIException穿过servlet接口并不少见(出于某种原因,它是IOException的子类)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM