Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java.lang.IllegalStateException: Response has already been committed

843829Aug 25 2003 — edited Dec 13 2006
Hi,

I am using HttpServletResponse and ServletOutputStream. The code is as follows:

protected void onRunReport (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = null;
ServletOutputStream os = null;
long reportid;

try {
reportid = Integer.parseInt(request.getParameter("rqname"));
String reportname = QReportSession.getReportName(reportid);
byte[] reportbyte = QReportSession.runReport(reportid);
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment; filename=" + reportname + ".pdf");
response.setContentLength(reportbyte.length);
os = response.getOutputStream();
os.write(reportbyte);
os.flush();
os.close();
System.out.println (os);
os = null;
response.sendRedirect("qtweb.qtReportServ?command=options");
} catch (qRemoteException rex) {
rex.printStackTrace();
if (os != null) {
os.close();
}
out = response.getWriter();
qtMainServ.errorPage (out, "Run Report Failed: " + rex.getMessage());
} catch (IllegalStateException ise) {
ise.printStackTrace();
if (os != null) {
os.close();
}
out = response.getWriter();
qtMainServ.errorPage (out, "Run Report Failed: " + ise.getMessage());
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
if (os != null) {
os.close();
}
out = response.getWriter();
qtMainServ.errorPage (out, "Run Report Failed: " + nfe.getMessage());
} catch (NullPointerException npe) {
if (os != null) {
os.close();
}
npe.printStackTrace();
out = response.getWriter();
qtMainServ.errorPage (out, "Run Report Failed: " + npe.getMessage());
} catch (Exception ex) {
ex.printStackTrace();
if (os != null) {
os.close();
}
out = response.getWriter();
qtMainServ.errorPage (out, "Run Report Failed: " + ex.getMessage());
}
}

Actually when I click "Run Report" button in a html form, this method will be invoked. The actual requirement is after I click the "Run Report", .pdf should get downloaded and goto a different page. To achieve this I used response.sendRedirect() method. I got the following error:

java.lang.IllegalStateException: Response has already been committed, be sure not to write to the OutputStream or to trigger a commit due to any other action before calling this method.

Is there a way to redirect from a page to another when I used response.getOutputStream().

Also, if any errors occured, how do I show it. Actually in my code I am trying to get Writer object to show error messages. This will not work. Any ideas how to show error messages when response.getOutputStream() is used.

regds
-raju
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 10 2007
Added on Aug 25 2003
12 comments
2,371 views