1. Server 會丟出 java.lang.IllegalStateException getoutputstream has already been called for this response 的 Exception
AP Server:
Tomcat 5
其他Ap Server 是否會發生??…不明
發生原因:
當 JSP 被編成 servlet時,在_jspService會有如下的程式碼
finally {
if (_jspxFactory != null)
_jspxFactory.releasePageContext(_jspx_page_context);
}
此段程式碼會使用 response.getWriter(),但是因為我們為了輸出Excel 檔案串流
已經使用了 response.getOutputStream(), 如此會造成
java.lang.IllegalStateException。
finally {
if (_jspxFactory != null)
_jspxFactory.releasePageContext(_jspx_page_context);
}
此段程式碼會使用 response.getWriter(),但是因為我們為了輸出Excel 檔案串流
已經使用了 response.getOutputStream(), 如此會造成
java.lang.IllegalStateException。
解決方法:
在使用完輸出串流之後,再使用以下兩段程式碼即可
out.clear();
out = pageContext.pushBody();
另外要注意的是,如果程式的流向是 A.jsp 透過 getRequestDispatcher() 連到 B.jsp,
再由 B.jsp 負責輸出Excel檔;
此時在 A.jsp 中,在執行完 getRequestDispatcher() 之後,亦要加上out.clear(); 與
out = pageContext.pushBody();
以下為符合上述情節的程式片段
--- A.jsp ---
try {
request.getRequestDispatcher("B.jsp").forward(request, response);
} finally {
response.flushBuffer();
request.getRequestDispatcher("B.jsp").forward(request, response);
} finally {
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
}
out = pageContext.pushBody();
}
--- B.jsp ---
OutputStream os = null;
try {
os = response.getOutputStream();
wb.write(os);
} finally {
os.flush();
os.close();
os = null;
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
}
try {
os = response.getOutputStream();
wb.write(os);
} finally {
os.flush();
os.close();
os = null;
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
}
參考資料:
2. 使用 IE 執行時,會丟出『找不到檔案』的錯誤訊息,並且無法開啟或儲存檔案。
瀏覽器:
IE
(FireFox 不會發生此問題)
解決方法:
將 response 中有關 no cache 的 header 移除
全站熱搜