I get the following Tomcat error when I run my JSP. I know what the error is I just don't why I'm getting it:
type Exception report
message org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)
root cause
org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:465)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:746)
org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:716)
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:868)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
org.apache.jsp.Ref.RefReportCntrl_jsp._jspService(RefReportCntrl_jsp.java:630)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)
root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
org.apache.catalina.connector.Response.getWriter(Response.java:640)
org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:214)
org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:190)
org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:126)
org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:80)
org.apache.jsp.Ref.refverify_005ferr_jsp._jspService(refverify_005ferr_jsp.java:124)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:746)
org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:716)
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:868)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
org.apache.jsp.Ref.RefReportCntrl_jsp._jspService(RefReportCntrl_jsp.java:630)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)
At the top of the JSP I have these lines:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*,org.apache.log4j.Logger" %>
<%@ page isErrorPage="false" errorPage="refverify_err.jsp" %>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page session="true" %>
<jsp:useBean id="da" class="reports.DemographicAge"/>
Later in the same JSP I call a method that eventually calls jasper-reports:
if(sectionA.equalsIgnoreCase("Demographics - Age")) {
result = da.convertDtosToXml(host, parameters, response, pathToweb);
}
The HttpServletResponse object “response” is passed to the following method where the error occurs during the call to JasperExportManager.exportReportToPdfStream()
:
package reports;
import dao.DemographicsAgeDao;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
public class DemographicAge {
private String generateDemographicAgeReport(ResultDataSource ds, Map parameters,
HttpServletResponse response, String host,
String path) throws RefugeeException {
String relativePath = path + "/Demographics_Age.xml";
String logo_path = path + "/logo.jpg";
File fin = new File(relativePath);
JasperReport jasperReport = null;
JasperDesign jasperDesign;
try {
jasperDesign = JRXmlLoader.load(fin);
jasperReport = JasperCompileManager.compileReport(jasperDesign);
} catch (JRException e) {
e.printStackTrace();
}
parameters.put("Report Title", "Health Assessment Report");
parameters.put("Sub Title", "Reported by " + (String) parameters.get("colname"));
parameters.put("logo", logo_path);
JasperPrint jasperPrint = null;
try {
//Fill the JasperPrint object with the report data
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, ds);
//Create a unique filename using date/time
String date1 = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date());
String fileName = "attachment; filename=\"DemographicsAgeReport_" + date1 + ".pdf\"";
//Set the HTTP response header information
response.setHeader("Content-disposition", fileName);
response.setContentType("application/pdf");
try {
ServletOutputStream stream = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, stream);
stream.flush();
stream.close();
} catch (IOException e) {
throw new RefugeeException("IO Exception while writing the content into PDF.", e.getMessage());
}
} catch (JRException e) {
throw new RefugeeException("Jasper exception while filling the report", e.getMessage());
}
return "success";
}
}
I have reviewed the following Stack Overflow posts and still need some guidance on how to fix this: