[英]Can I close wrapper inputstream or outputstream class in servlet?
I have a code like... 我有一个像...的代码
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.
}
}}
I know that close for httpRequest.getInputStream() and httpResponse.getOutputStream() is not required but is it right to close the Stream class which is wrapping the inputstream and outputstream.Is they create some issue or throw exception. 我知道不需要关闭httpRequest.getInputStream()和httpResponse.getOutputStream(),但是关闭包装输入流和输出流的Stream类是否正确,是因为它们会产生问题或引发异常。
The wrapper streams/readers/writers in the Java SE close the underlying streams (when they get closed), so if you want to keep them open, do not close the wrappers. Java SE中的包装器流/读取器/编写器会关闭基础流(当它们关闭时),因此,如果要保持打开状态,请不要关闭包装器。
Edit: The Reader 's close()
documentation probably explains better. 编辑: 读者的
close()
文档可能解释得更好。 The wrappers (like InputStreamReader ) usually extend this class for the interfaces without overriding the documentation. 包装程序(如InputStreamReader )通常在不覆盖文档的情况下为接口扩展此类。
In case of ObjectOutputStream the case is similar you can check the behaviour in source . 如果ObjectOutputStream的情况类似,则可以检查source中的行为。
让我们从Java EE 5教程-过滤请求和响应开始 ,在其中进行了详细说明,并举例说明了如何使用HttpServletResponseWrapper和HttpServletRequestWrapper定制请求和响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.