getOutputStream() has already been called for this response... + Jasper

0

Hello Experts,
                        I have integrated Jasper Reports with Struts 1.2.9.The below code in written my action class.At runtime I am getting this error.I am a student , learning Jasper reports for my Project.Please help me out.I am pretty new to this.I am trying this sample code from Book "Jasper Reports for Java Developer" by David R Heffelfinger.

ApplicationDispatcher[/TGMC_DB1-2009] PWC1231: Servlet.service() for servlet jsp threw exception

java.lang.IllegalStateException: PWC3991: getOutputStream() has already been called for this response
at org.apache.coyote.tomcat5.CoyoteResponse.getWriter(CoyoteResponse.java:700)
at org.apache.coyote.tomcat5.CoyoteResponseFacade.getWriter(CoyoteResponseFacade.java:210)
at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:135)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:171)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:164)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:221)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:157)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:108)
at org.apache.jsp.LeftMenu_jsp._jspService(LeftMenu_jsp.java from :152)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:93)

This is my code in action class.The line which is generating error is in bold... one more thing after exception it is going in 2nd catch block & not in first catch block where ex.getMessage() is printing null.

Code:
package com.myapp.struts;
 
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import java.io.InputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.*;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import java.util.HashMap;
import java.sql.ResultSet;
import net.sf.jasperreports.engine.JRException;
import java.io.PrintWriter;
import java.io.StringWriter;
 
public class ViewReportAction extends Action {
 
    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
 
 
            throws IOException, ServletException {
 
        String target = new String("success");
 
 
 
        try {
              ServletOutputStream servletOutputStream =
              response.getOutputStream();
 
 
             ResultSet rs = null;
 
             InputStream reportStream = getServlet().getServletConfig().getServletContext().getResourceAsStream("F:\\project\\tgmc next year ibm\\04-oct-2008\\dynamicjasper\\src\\java\\test\\report1.jrxml");
 
 
 
            rs = Reports.View_Ementor_list(getDataSource(request));
 
            JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs);
 
 
*//THIS LINE IS GENERATING ERROR*
            *JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, new HashMap(), resultSetDataSource);*
 
 
            response.setContentType("application/pdf");
 
            servletOutputStream.flush();
 
            servletOutputStream.close();
 
 
 
        }
         catch (JRException e) {
 
 
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
 
 
            e.printStackTrace(printWriter);
            response.setContentType("text/plain");
            response.getOutputStream().print(stringWriter.toString());
 
 
        }
        catch (Exception ex) {
 
 
            System.out.println(ex.getMessage());
 
        }
        return mapping.getInputForward();
 
 
    }
}
 
 
</td></tr></tbody></table>
nehaagrawal30's picture
Joined: Oct 7 2008 - 11:50pm
Last seen: 10 years 5 months ago

2 Answers:

0

It says getOutputStream() has already been called for this response so what do you do? You check to see how often you call the method getOutputStream().

If you look carefully at your code in the try and catch section you call response.getOutputStream(). So the answer is simple, don't call getOutputStream() twice.

martynhiemstra's picture
Joined: Jan 25 2007 - 4:31pm
Last seen: 12 years 1 month ago
0

Put the outputstream in a variable above the try block and replace all the instances of response.getOutputStream() with the local variable.

martynhiemstra's picture
Joined: Jan 25 2007 - 4:31pm
Last seen: 12 years 1 month ago
Feedback