Search code examples
javahtmljasper-reportsjasperserver

Different reports get the same chart image in generated reports


I'm generating reports in java using Jasper Reports. I have things set up so that when a link to an item is clicked, a report based on that item is generated complete with chart image. The problem is, after generating the first report, any subsequent reports generated will use the same image as the first report. What I want is for each generated report to use it's own chart image. Not sure what I'm doing wrong.

NOTE: I know these methods are deprecated. I need to get things working on the images before I try to upgrade to new methods (which I could also use help with but will make a separate question when the time comes).

  else if (export_format != null && export_format.equalsIgnoreCase("HTML")) {
  jasperprint = JasperFillManager.fillReport(inFileStream, map, conn);
  JRHtmlExporter exporter = new JRHtmlExporter();
  HashMap imagesMap = new HashMap();
  request.getSession().setAttribute("IMAGES_MAP", imagesMap);
  request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperprint);

  exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperprint);
  exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
  exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, "");
  exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, "");
  exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
  exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
  exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "jasper/images?image=");
  exporter.exportReport();
}

Solution

  • The likely reason for which you see the first chart in subsequent reports is that the browser caches the image at jasper/images?image=...

    One easy way to avoid this is to add a random parameter to the images URLs:

      exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "jasper/images?random=" + java.util.UUID.randomUUID() + "&image=");