0

I need to generate xls file from jsp.

this is the code for generating the xls.

 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
 java.io.File fn=new java.io.File(this.getServletContext().getRealPath("/")+"BillSummaryReport.xls");
 if(!fn.exists()){
 fn.createNewFile();
 }
System.out.println(this.getServletContext().getRealPath("/")+"BillSummaryReport.xls");
//FileOutputStream fileOut = new FileOutputStream(f);
java.io.FileInputStream fin = new java.io.FileInputStream(fn);
ServletOutputStream outStream =response.getOutputStream();
hwb.write(response.getOutputStream());
response.setHeader("Content-Disposition","attachment;filename=BillSummaryReport.xls");
byte[] buffer = new byte[1024];
int n = 0;
while ((n = fin.read(buffer)) != -1) {  
outStream.write(buffer, 0, n);
System.out.println(buffer);}
outStream.flush();
fin.close();
outStream.close();
} catch( Exception ex ) {
System.out.println(ex);
}

Error occurs like this when i am trying to run the application.

    >Sep 19, 2014 11:45:28 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/Midrest] threw exception   [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
    at org.apache.catalina.connector.Response.getWriter(Response.java:636)
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:213)
    at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
    at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
    at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:194)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Killer
  • 592
  • 1
  • 6
  • 24
Partha
  • 43
  • 1
  • 2
  • 12
  • check this.http://stackoverflow.com/questions/14951041/getting-exception-java-lang-illegalstateexception-getoutputstream-has-already – Killer Sep 19 '14 at 06:46

1 Answers1

0

You can't use a jsp file which contains (writes) text to the output to send back a file (xls for example). Jsp files (which are compiled into servlets) will attempt to write to the output using response.getWriter().

Since you called response.getOutputStream() to send your file, the jsp file (servlet) trying to call response.getWriter() to write HTML data will fail because only one of those 2 methods (getOutputStream() and getWriter()) can be used.

If you want to send back a file, use a simple Servlet for that and not a jsp file. Or if you must, you can use a jsp for this but then you can't write anything to the output, not even a line break. So best is to use a Servlet for this.

icza
  • 389,944
  • 63
  • 907
  • 827