我有一个这样的代码......我可以关闭servlet中的包装器输入流或输出流类吗?
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()不是必需的,但是是不是要关闭其包装InputStream的Stream类和outputstream.Is他们创建一些问题或抛出异常。
查找到自定义的请求和响应尝试用资源模式一起解释了The Java EE 5 Tutorial - Filtering Requests and Responses。你可能会泄漏流。捕捉所有例外通常是不好的做法。 –