org.apache.jasper.JasperException:java.lang.IllegalStateException:已为此响应调用了getOutputStream()

时间:2012-12-28 20:13:30

标签: java jsp jasper-reports

运行JSP时出现以下 Tomcat 错误。我知道错误是什么我只是不知道为什么我得到它:

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)

在JSP的顶部,我有以下几行:

<%@ 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"/>

稍后在同一个JSP中,我调用一个最终调用jasper-reports的方法:

if(sectionA.equalsIgnoreCase("Demographics - Age")) {
     result = da.convertDtosToXml(host, parameters, response, pathToweb);
} 

HttpServletResponse对象“response”被传递给以下方法,在调用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";
    }
}

我查看了以下Stack Overflow帖子,但仍需要一些如何解决此问题的指导:

Using PrintWriter and OutputStream

java.lang.IllegalStateException: getOutputStream() has already been called for this response when calling JasperReport

getOutputStream() has already been called for this response

1 个答案:

答案 0 :(得分:0)

发布的Tomcat错误是一个detractor。我的代码有一个try / catch块用于包含对自定义错误jsp页面(RefReportCntrl_jsp.java)的调用的异常。当RefReportCntrl_jsp.java尝试将字符数据推送到当前作为位流打开的响应流时,它会导致第二个错误,这是错误日志中记录的错误。

通过查看RefReportCntrl_jsp.java在抛出第二个异常之前显示的异常字符串,发现了真正的错误。

真正的错误是该项目使用旧版本的Jasper和更新版本的iText。当Jasper在iText中调用一个不再存在该名称的方法时,它会抛出一个未找到异常的方法。