Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Tolls

Thread: getOutputStream() has already been called for this response

  1. #1
    Join Date
    Feb 2009
    Rep Power

    Default getOutputStream() has already been called for this response

    Java Code:
    try {
    			reportFile = request.getRealPath("/")+"WEB-INF/Documents/TestCases/ScenarioDetails.jrxml";
    			JasperDesign jasperDesign = JRXmlLoader.load(reportFile); 
    			JasperReport report = JasperCompileManager.compileReport(jasperDesign);
    			conn = dataSource.getConnection();
    			JasperPrint jasperPrint = JasperFillManager.fillReport(report,map, conn);
    			ByteArrayOutputStream baos = new ByteArrayOutputStream();
    			response.setHeader("Content-Disposition"," attachment; filename="+OfficeName+".pdf\"");
    		}catch(Exception e) {
    		 return new ModelAndView("top","model",model);
    private void writeReportToResponseStream(HttpServletResponse response,ByteArrayOutputStream baos) {		
    		System.out.println("Writing report to the stream");
    		ServletOutputStream outputStream = null;
    		try {
    			// Retrieve the output stream
    			outputStream = response.getOutputStream();
    			// Write to the output stream
    			// Flush the stream
    		}catch (Exception e) {
    public void export(JasperPrint jasperPrint, ByteArrayOutputStream baos)  throws JRException {
    		JRPdfExporter exporter=new JRPdfExporter();
    		exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
    For generating PDF reports I have used above code(jasper reports).Reports are generated properly but in server console it shows following exception.

    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(R
    at org.apache.catalina.connector.ResponseFacade.getWr iter(
    at javax.servlet.ServletResponseWrapper.getWriter(Ser
    at org.apache.jasper.runtime.JspWriterImpl.initOut(Js
    at org.apache.jasper.runtime.JspWriterImpl.flushBuffe r(
    at org.apache.jasper.runtime.PageContextImpl.release(
    at org.apache.jasper.runtime.JspFactoryImpl.internalR eleasePageContext(
    at org.apache.jasper.runtime.JspFactoryImpl.releasePa geContext(

    how to fix this problem..Thanks in advance
    Last edited by srinivasmallabathula; 07-17-2012 at 12:38 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: getOutputStream() has already been called for this response

    Java Code:
    return new ModelAndView("top","model",model);
    You've written to the output stream, but you then pass a model and view back up to Spring, presumably referencing a JSP page.
    Spring tells Tomcat to forward to that JSP, and so Tomcat attempts to write it out....which it can't do as you've already written to the response stream.

    So the solution is don't return the ModelAndView. Return 'null', which should tell Spring not to bother forwarding anywhere.
    zed420 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    zed420 is offline Member
    Join Date
    Dec 2009
    Leeds, UK
    Rep Power

    Default Re: getOutputStream() has already been called for this response

    This has solved my issue too, thank you

Similar Threads

  1. Using getOutputStream() while getWriter() is open
    By khkh82 in forum Advanced Java
    Replies: 1
    Last Post: 06-06-2012, 12:20 PM
  2. Replies: 2
    Last Post: 05-11-2012, 09:40 AM
  3. Replies: 0
    Last Post: 06-23-2011, 12:02 PM
  4. Replies: 0
    Last Post: 04-07-2011, 03:09 PM
  5. getOutputStream() exception
    By Freddie in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 05-15-2007, 04:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts