![](/img/trans.png)
[英]Why I can only close socket's outputStream first and then close inputstream?
[英]Can I close wrapper inputstream or outputstream class in 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(),但是關閉包裝輸入流和輸出流的Stream類是否正確,是因為它們會產生問題或引發異常。
Java SE中的包裝器流/讀取器/編寫器會關閉基礎流(當它們關閉時),因此,如果要保持打開狀態,請不要關閉包裝器。
編輯: 讀者的close()
文檔可能解釋得更好。 包裝程序(如InputStreamReader )通常在不覆蓋文檔的情況下為接口擴展此類。
如果ObjectOutputStream的情況類似,則可以檢查source中的行為。
讓我們從Java EE 5教程-過濾請求和響應開始 ,在其中進行了詳細說明,並舉例說明了如何使用HttpServletResponseWrapper和HttpServletRequestWrapper定制請求和響應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.