Monday, May 23, 2011

java.lang.IllegalStateException: getOutputStream() has already been called for this response

STATUS : Closed

Scenario :
jsp to generate a report and on click of a button download the report xls from the server. Code in the jsp is as follows.

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment; filename=Report.xls");

OutputStream out1 =response.getOutputStream();

WritableWorkbook workbook = Workbook.createWorkbook(out1);

Resolution :
Moved the code in the jsp to a servelet


Possible RootCause :
You cannot get a handle to the multiple output stream objects. When the jsp code gets compiled to plain java it adds code " out.write() " to write the jsp content to the outputstream. Since you already have got a handle to the ouputstream the out.write() system code throws the exception. Hence moving the code to a servelet solves the issue.

No comments:

Post a Comment