简体   繁体   English

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

[英]org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response

I get the following Tomcat error when I run my JSP. 运行JSP时出现以下Tomcat错误。 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: 在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"/>

Later in the same JSP I call a method that eventually calls jasper-reports: 稍后在同一JSP中,我调用了一个最终调用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() : 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";
    }
}

I have reviewed the following Stack Overflow posts and still need some guidance on how to fix this: 我已经查看了以下Stack Overflow帖子,但仍需要一些有关如何解决此问题的指导:

Using PrintWriter and OutputStream 使用PrintWriter和OutputStream

java.lang.IllegalStateException: getOutputStream() has already been called for this response when calling JasperReport java.lang.IllegalStateException:调用JasperReport时已为此响应调用getOutputStream()

getOutputStream() has already been called for this response 已为此响应调用getOutputStream()

The Tomcat error posted was a detractor. 张贴的Tomcat错误是不利因素。 My code had a try/catch block for exceptions that included a call to a custom error jsp page (RefReportCntrl_jsp.java). 我的代码有一个try / catch块,用于处理异常,其中包括对自定义错误jsp页面(RefReportCntrl_jsp.java)的调用。 When RefReportCntrl_jsp.java tried to push character data to the response stream, which was currently open as a bit stream, it caused a second error which is what was recorded in the error log. 当RefReportCntrl_jsp.java尝试将字符数据推送到当前作为位流打开的响应流时,它导致了第二个错误,该错误记录在错误日志中。

The real error was discovered by looking at the exception string to be displayed by RefReportCntrl_jsp.java prior to the second exception being thrown. 真正的错误是通过在抛出第二个异常之前查看RefReportCntrl_jsp.java显示的异常字符串发现的。

The real error was that the project was using an older version of Jasper with a newer version of iText. 真正的错误是该项目正在使用旧版本的Jasper和更新版本的iText。 When Jasper called a method in iText that no longer existied as that name it threw a method not found exception. 当Jasper在iText中调用一个不再以该名称存在的方法时,它抛出了一个方法未发现异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalStateException:已经为此响应调用了 getOutputStream() - java.lang.IllegalStateException: getOutputStream() has already been called for this response java.lang.IllegalStateException:已为此响应调用了getOutputStream() - java.lang.IllegalStateException: getOutputStream() has already been called for this response 原因:java.lang.IllegalStateException:此响应已经调用了getOutputStream()。 - Caused by: java.lang.IllegalStateException: getOutputStream() has already been called for this response Spring Boot Web应用程序:引发java.lang.IllegalStateException:此响应已调用getOutputStream() - Spring Boot Web Application: throwing java.lang.IllegalStateException: getOutputStream() has already been called for this response REST-java.lang.IllegalStateException:此响应已调用getOutputStream() - REST - java.lang.IllegalStateException: getOutputStream() has already been called for this response Spring:java.lang.IllegalStateException:已经为此响应调用了getOutputStream() - Spring: java.lang.IllegalStateException: getOutputStream() has already been called for this response java.lang.IllegalStateException:此响应已经被调用getOutputStream()-SpringBoot - java.lang.IllegalStateException: getOutputStream() has already been called for this response - SpringBoot 我得到一个异常:java.lang.IllegalStateException:已经为此响应调用了getOutputStream() - I am getting an exception: java.lang.IllegalStateException: getOutputStream() has already been called for this response 我正在获取java.lang.IllegalStateException:此响应已调用getOutputStream()异常 - I m getting java.lang.IllegalStateException: getOutputStream() has already been called for this response Exception java.lang.IllegalStateException:已为此响应调用getWriter() - java.lang.IllegalStateException: getWriter() has already been called for this response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM