【发布时间】:2014-07-26 00:05:32
【问题描述】:
我有一个类似...的代码
class DemoServlet extends HttpServlet
{
doPost(HttpReqeust httpRequest,HttpResponse httpResponse)
{
try {
ObjectInputStream input=new ObjectInputStream(httpRequest.getInputStream());
Object ojb=input.readObject();
ObjectOutputStream output=new ObjectOutputStream(httpResponse.getOutputStream());
}
catch(Exception ex){
ex.printStackTrace();
}
finally{
input.close(); // it is necessary to close or it will handle by the servlet container to
output.close(); // the outputstream or inputstream.
}
}}
我知道关闭 httpRequest.getInputStream() 和 httpResponse.getOutputStream() 不是必需的,但是关闭包装输入流和输出流的 Stream 类是否正确。它们会产生问题还是抛出异常。
【问题讨论】:
-
查看 try with resources 模式。你可能会泄漏流,那里。捕获所有异常通常是不好的做法。
标签: java servlets inputstream outputstream