Chart caching issues with JasperReports web output? A solution!

Just a quick posting about an issue I was running in to that was solved pretty easily with a minimal amount of work.  Providing it here as a tip for people new to JasperReports and using it to deliver HTML reports via the web.

The issue I was running in to was caching of report charts when outputting to HTML format.  When JRHtmlExporter creates links to images in the report output it uses the format img_x_y_z[_z_z] where x, y, and z are as follows:

  • x is the index of a document within the list of documents to export in batch mode.
  • y is the page index.
  • z values are a series of one or more element indexes that locate the image on the page,
    looping through nested frames if needed.

(taken from the JasperReports Definitive Guide – pg. 197)

The issue is if you run a report which generates a chart and then rerun the report with similar parameters which doesn’t result in a change to the image name you will end up with a cached copy of the chart image…probably not what you want.

The solution is simple:  when you define your IMAGES_URI paramter for the JRHtmlExporter tack on the current system time as follows:

exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "/jasper/image?time="+System.currentTimeMillis()+"&image=");

In this example I’m using the system time to “randomize” the image url which makes sure that the images are always displayed properly. Note too that I’m using the image servlet that’s included as part of JasperReports.

I hope this helps someone. I was scratching my head for a bit over this one but the solution is pretty simple.

6 thoughts on “Chart caching issues with JasperReports web output? A solution!

  1. Thanks in deed. This soultion has helped us to resolve the same issue which we were facing


  2. I’m facing the same problem but I can’t solve it as you said…

    That’s because I have this:
    exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE);
    exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR_NAME, controller.getServletContext().getRealPath(“/images/report”));
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, sbuffer);

    If I put “image?image=” on IMAGES_URI the browser doesn’t show the image… The report image is saved on “/images/report/” folder so I have to put it on IMAGES_URI. With this I can’t put a random url…


  3. I have got the same problem as Gonçalo has.
    Please reply me as easrly as possible


  4. Not sure how the other guy solved his problem (looks like he wasnt doing web-based reports but hard to tell from his code) but this assumes (and since its a web-based report I think the assumption is safe) that you’re using the jasper image servlet to serve up the images used in the report. If you’re using that servlet my solution will work.


Comments are closed.

%d bloggers like this: