3

I'm trying to use Servlet to download data as CSV, and as the JSP is included with flush=true. The stack trace is provided below. The report is getting saved as expected but I can see an exception in the back. Can you please help me figure out why? I have closed the OutputStream and also tried flushing the buffer, but still no luck.

1:36:38,304 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
    at org.apache.catalina.connector.Response.getWriter(Response.java:619)
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
    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.JspWriterImpl.flush(JspWriterImpl.java:173)
    at atg.taglib.dspjsp.IncludeTag.flushOut(IncludeTag.java:873)
    at atg.taglib.dspjsp.IncludeTag.doEndTag(IncludeTag.java:773)

pResponse.setContentType(MS_EXCEL);
pResponse.setHeader(CONTENT,FILENAME);

outputStream = pResponse.getOutputStream();
pResponse.flushBuffer();
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
Adam
  • 727
  • 4
  • 11
  • 21

1 Answers1

0

You are calling pResponse.getOutputStream(); which is illegal. You should use either ServletResponse.getOutpustStream() for ServletResponse.getWriter(). You cannot use both in same response.

Since JSP's use ServletResponse.getWriter() by default. You should write to ServletResponse.getWriter() instead ServletResponse.getOutpustStream()

This is what Java Doc says :

getOutputStream...

ServletOutputStream getOutputStream() throws IOException

Returns a ServletOutputStream suitable for writing binary data in the response. The servlet container does not encode the binary data.

Calling flush() on the ServletOutputStream commits the response. Either this method or getWriter() may be called to write the body, not both.

Returns: a ServletOutputStream for writing binary data Throws: IllegalStateException - if the getWriter method has been called on this response

Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
  • I am able to write work book using output stream only not using the print writer, work book do not have any support printwriter, could you please let me know how to write workbook using the print writter? – Adam Mar 30 '13 at 23:10