简体   繁体   English

我可以关闭servlet中的包装输入流或输出流类吗?

[英]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教程-过滤请求和响应开始 ,在其中进行了详细说明,并举例说明了如何使用HttpServletResponseWrapperHttpServletRequestWrapper定制请求和响应。

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

相关问题 为什么我只能先关闭socket的输出流再关闭输入流? - Why I can only close socket's outputStream first and then close inputstream? 如果关闭BluetoothSocket,是否需要关闭InputStream / OutputStream? - do I need to close InputStream/OutputStream if I close BluetoothSocket? 如何将文件读取到 InputStream 然后将其写入 Scala 中的 OutputStream? - How can I read a file to an InputStream then write it into an OutputStream in Scala? 在类中使用装饰的OutputStream / InputStream字段 - Using decorated OutputStream/InputStream fields in your class 从InputStream到OutputStream - InputStream to OutputStream 我可以关闭基础输出流并让装饰器BufferedOutputStream处于未关闭状态吗 - Can I close underlying outputstream and let the decorator BufferedOutputStream unclosed DataInputStream/DataOutputStream 类与 InputStream/OutputStream 类的区别 - Difference Between DataInputStream/DataOutputStream Class & InputStream/OutputStream Class HTTP Status 500包装器找不到servlet类 - HTTP Status 500 wrapper can not find servlet class 是否可以配置Servlet不自动关闭响应的OutputStream? - Is it possible to configure a Servlet not to automatically close the response's OutputStream? 我需要刷新servlet输出流吗? - Do I need to flush the servlet outputstream?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM