java異常,java.lang.IllegalStateException,在進行JasperReports導出報表的時候出現的異常

問題描述:

我在進行JasperReports 導出報表的時候,拋出這個異常java.lang.IllegalStateException:Cannot call sendError() after the response has been committed

Action裏代碼:

public String exportexcel(){  
employeeList = employeeService.getAll();
        JasperHelper.exportmain("excel", "employee.jasper", employeeList, "employ.xls");  
   return success;
}

/**

報表工具類

*/

JasperHelper 裏代碼:

/**
* 導出excel
*/
public static void exportExcel(JasperPrint jasperPrint,
String defaultFilename, HttpServletRequest request,
HttpServletResponse response) throws IOException, JRException {
logger.debug("執行導出excel   The method======= exportExcel() start.......................");
/*
* 設置頭信息
*/
response.setContentType("application/vnd.ms-excel");
String defaultname = null;
if (defaultFilename.trim() != null && defaultFilename != null) {
defaultname = defaultFilename + ".xls";
} else {
defaultname = "export.xls";
}
String fileName = new String(defaultname.getBytes("gbk"), "utf-8");
response.setHeader("Content-disposition", "attachment; filename="
+ fileName);
ServletOutputStream ouputStream = response.getOutputStream();
JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
exporter.setParameter(
JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
Boolean.FALSE);
exporter.exportReport();
ouputStream.flush();
ouputStream.close();
}

網上查了查得到問題解答:

問題分析:

在生成驗證碼的代碼中:return SUCCESS;這代表着轉向,需要response的,而response.getOutputStream(); 時已經committed,不能再response一次,當把return SUCCESS;改成return null;所有問題都解決!


至於怎樣是committed,網上提供推薦看看這裏:http://jorton468.blog.163.com/blog/static/72588135201102441617287/ 原文如下:

問題描述:
錯誤類型大致爲以下幾種:

java.lang.IllegalStateException:Cannot   forward   a   response   that   is   already   committed 
IllegalStateException:response already commited 
IllegalStateException:getOutputStream() has already been called for this request
…………


 
錯誤原因:
該異常表示,當前對客戶端的響應已經結束,不能在響應已經結束(或說消亡)後再向
客戶端(實際上是緩衝區)輸出任何內容。


具體分析:
首先解釋下flush(),我們知道在使用讀寫流的時候數據先被讀入內存這個緩衝區中,
然後再寫入文件,但是當數據讀完時不代表數據已經寫入文件完畢,因爲可能還有
一部分仍未寫入文件而留在內存中,這時調用flush()方法就會把緩衝區的數據強行
清空輸出,因此flush()的作用就是保證緩存清空輸出。
response是服務端對客戶端請求的一個響應,其中封裝了響應頭、狀態碼、內容等,
服務端在把response提交到客戶端之前,會向緩衝區內寫入響應頭和狀態碼,然後
將所有內容flush。這就標誌着該次響應已經committed(提交)。對於當前頁面中
已經committed(提交)的response,就不能再使用這個response向緩衝區寫任何東西
(注:同一個頁面中的response.XXX()是同一個response的不同方法,只要其中一個
已經導致了committed,那麼其它類似方式的調用都會導致 IllegalStateException異常)。
【注意】能夠導致響應已經committed的操作包括:forward, redirect, flushBuffer。
JDK API:
         ①


flushBuffer


        public void flushBuffer()throws IOException
Forces any content in the buffer to be written to the client. A call to this method automatically commits the response, meaning the status code and headers will be written.  
           ②
                
sendRedirect
         public void sendRedirect(String location)throws IOException
Sends a temporary redirect response to the client using the specified redirect location URL. This method can accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.
If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.


            ③                


forward
public void forward(ServletRequest request,ServletResponse response)
                             throws ServletException,IOException
Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary processing of a request and another resource to generate the response.
For a RequestDispatcher obtained via getRequestDispatcher(), the ServletRequest object has its path elements and parameters adjusted to match the path of the target resource.


forward should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.
The request and response parameters must be either the same objects as were passed to the calling servlet's service method or be subclasses of the ServletRequestWrapper or ServletResponseWrapper classes that wrap them.


 
備    注:
                  注:在一次響應commit之前,所有的內容輸出都將寫入servlet引擎的緩衝區(tomcat或
                  weblogic的內容空間), 而在commit之後,上一次response向緩衝區寫入的內容,將清空。
                  由於servlet在沒有設置單線程的情況下(使用Single-Threaded Model,servlet實現
                  SingleThreadModel接口,jsp使用<%@ page isThreadSafe="false" %>),是多線程的,所以
                  上面所說的緩衝區,都將是該response所屬的線程私有的內存空間。有了這個概念,
                  將可以分析碰到的關於servlet多線程的很多問題。
                  如果不能確認response是否已經committed. 可以調用response.isCommitted()來判斷。
                  導致這個錯誤最普遍的原因是,jsp有編譯錯誤。

常見解決辦法:


                 ①在response.sendRedirect()方法後加return語句即可,如下:
                       response.sendRedirect("login.jsp");
                       return;
                 ②檢查提交的url是否有誤。
                 ③如果你的頁面中用了清緩存代碼response.flushbuffer();又用到了response.sendRedirect(url);
                     你可以把response.flushbuffer();去掉,或者用JS的window.location.href="url";來做轉向。
                 ④如果你用了OutputStream,而web容器生成的servlet代碼中有out.write(””),這個和JSP中調用的
                     response.getOutputStream()衝突。out.write()這個是字符流,而response.getOutputStream()
                    是字節流,你不能在同一個頁面中調用多個輸出流。無論先調用哪一個,在調用第二個時都會拋出
                      IllegalStateException,因爲在jsp中,out變量是通過response.getWriter得到的。在多個使用了
                     outputStream的<%%>語句之間不能有空格及多餘的字符。也就是頁面中除了使用了
                      outputStream的<%%>之外不能有空格或其它任何字符,在之內的語句可以有空格及回車。
                 在JSP頁面做輸出的時候有兩種方式.一是通過JspWriter,另一個是通過
                 OutputStream,但二者互相排斥.如果並存的話就會報告以上異常.                   
                     在不得不使用OutputStream的時候.我們必須要把JspWriter捨棄掉了。找到
                     請求異常的頁面所對應的Servlet..把其中所有使用JspWriter的語句全部去掉.
                     或者是到你的JSP文件裏把動態輸出的代碼註釋掉.這裏注意換行和空格製表符均
                     爲JspWriter輸出.應該一起去掉.保存文件重新啓動服務器你會發現上述異常
                     消失了。                    

         由於jsp container在處理完成請求後會調用releasePageContet方法釋放
          所用的PageContext object,並且同時調用getWriter方法,由於getWriter方法
          與在jsp頁面中使用流相關的getOutputStream方法衝突,所以會造成這種異常,
         解決辦法是:只需要在jsp頁面的最後加上兩條語句:   
                 out.clear(); 
                 out=pageContext.pushBody();
         即可(其中out,pageContext均爲jsp內置對象!) 。

發佈了28 篇原創文章 · 獲贊 21 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章