How to display the reports on client side jsp?

2019-08-30 18:58发布

问题:

I am trying to display the result in jsp page. In my In dex.html has a link as , <a href="vehicleReport.jsp">Vehicle Report</a>

In vehicleReport.jsp I am displaying report as,

<body>
 <%
  Connection con=null;
  try{
 con=VehicleDetails.getConnection();
 InputStream inputStream = new FileInputStream ("E:/ReportFld/report4.jrxml");

Map parameters = new HashMap();

JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
@SuppressWarnings("unchecked")
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, con);
out.clear(); // where out is a JspWriter
out = pageContext.pushBody();
JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
}
catch(Exception e){e.printStackTrace();}
finally{
con.close();
}

%>
</body>

But problem is report is not visible. In jsp it displays some unreadable text. and I am getting error message as,

SEVERE: Servlet.service() for servlet [jsp] in context with path [/Reports] threw exception [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:636)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:213)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)

How to resolve this?

回答1:

Try using

JasperExportManager.exportReportToPdfFile(JasperPrint jsperPring, String fileName);

instead of

JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());

and then display your pdf in browser or anywhere you want.