JFreeChart Servlet / JSP Examples

The following examples show how to use some of the new facilities in JFreeChart (since version 0.9.3) that are designed for use in a servlet / JSP environment. The new facilities are:

  • Charts with tooltips (now work with the latest versions of Internet Explorer, Netscape, Mozilla and Opera)
  • Charts with drill down capabilities using custom URLs
  • Utility classs to simplify persisting charts into the temporary directory
  • Generic servlet to stream charts created in the temporary directory to the user that created them
  • Automatic deletion of charts that were created by users whose sessions have expired or have been explicitly abandoned

These examples make particular use of classes in the following packages:

  • org.jfree.chart.servlet
  • org.jfree.chart.labels
  • org.jfree.chart.urls

Refer to the JavaDoc for specifics of the API.

All the examples use the same dataset which is hit data from a fictious web site over the month of August 2002 categorized into areas of the site. The hit data is all hard coded in the WebHitDataSet class. This is to keep the setup for the examples as simple as possible. Normally I would keep data like this in a database for easier querying and updating.

There are a couple of key advantages to writing the charts to the temporary directory and then using the DisplayChart servlet to binary stream them to the client, rather than writing the charts into part of the file system that is directly accessible via HTTP.

  • The servlet engine does not have to be on the same machine as the web server. This is a big advantage to organisations that prefer to run a web server in the DMZ and have the servlet engine/app server in the intranet.
  • The DisplayChart servlet checks to make sure that the user who is requesting the chart was the user who actually created it in the first place, preventing unauthorised access to data requested by other clients. (Any files in the temporary directory beginning with "public" are permissible for any user to access. This is so that common images such as errors messages can be served up using the same mechanism.)

In addition, the servlet has been written to make sure that only files from the temporary directory can be served. Frequently servlets that perform binary streaming can inadvertently open up the entire file system to someone who doesn't mind hand-typing some URLs.