当前位置: 首页 > 知识库问答 >
问题:

jasperException:java.lang.IllegalStateException:已经为此响应调用了getOutputStream()

孙辰阳
2023-03-14
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)
<%@ 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";
    }
}

我已经查看了以下堆栈溢出帖子,仍然需要一些关于如何修复此问题的指导:

共有1个答案

云季萌
2023-03-14

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

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

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

 类似资料: