Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2009
    Posts
    28
    Rep Power
    0

    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);
    			conn.close();
    
    			ByteArrayOutputStream baos = new ByteArrayOutputStream();
    
    			
    			response.setContentType("application/pdf");
    			response.setHeader("Content-Disposition"," attachment; filename="+OfficeName+".pdf\"");
    			export(jasperPrint,baos);
    			writeReportToResponseStream(response,baos);
    		}catch(Exception e) {
    			e.printStackTrace();
    		}
    			
    		
    		 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
    			baos.writeTo(outputStream);
    			// Flush the stream
    			outputStream.flush();
    			outputStream.close();
    
    		}catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    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);
            exporter.exportReport();
    }
    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 esponse.java:604)
    at org.apache.catalina.connector.ResponseFacade.getWr iter(ResponseFacade.java:198)
    at javax.servlet.ServletResponseWrapper.getWriter(Ser vletResponseWrapper.java:112)
    at org.apache.jasper.runtime.JspWriterImpl.initOut(Js pWriterImpl.java:125)
    at org.apache.jasper.runtime.JspWriterImpl.flushBuffe r(JspWriterImpl.java:118)
    at org.apache.jasper.runtime.PageContextImpl.release( PageContextImpl.java:178)
    at org.apache.jasper.runtime.JspFactoryImpl.internalR eleasePageContext(JspFactoryImpl.java:117)
    at org.apache.jasper.runtime.JspFactoryImpl.releasePa geContext(JspFactoryImpl.java:76)


    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
    Posts
    11,798
    Rep Power
    19

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

    Java Code:
    writeReportToResponseStream(response,baos); 
    ...
    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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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
  •