4

I am attempting to dynamically generate charts using the JFreeChart library and display them to a user on the front-end. My project is using JSF 1.2 as its view technology and we are trying to determine a strategy to display a BufferedImage.

Thus far, the best option seems to be to generate the graph using a servlet and use h:graphicImage to point to that location. The primary question is, how can I pass an object from JSF to the servlet so that the graph generation is dynamic based on the values in the object?

rynmrtn
  • 3,371
  • 5
  • 28
  • 44

2 Answers2

4

Let JSF put it in session along an autogenerated and unique key, pass that key as request parameter or pathinfo to the servlet and finally let the servlet remove it from the session by the key and use it.

JSF bean (during init or action method):

this.key = UUID.randomUUID().toString();
externalContext.getSessionMap().put(key, object);

JSF view:

<h:graphicImage value="servleturl?key=#{bean.key}" />

Servlet:

String key = request.getParameter("key");
Object object = request.getSession().getAttribute(key);
request.getSession().removeAttribute(key);
// ...
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • This solution is likely the best for our scenario and worked very quickly after our first test. We need to hold a collection of data in an object that can be used to generate a graphical chart. This data would not be ideal to pass as part of URL parameters, so we'll need to remain mindful of what is being stored in the session so that we can keep things lean. – rynmrtn Jan 22 '11 at 03:25
2

Personally, I would prefer to pass the data as part of the URL as this avoids relying on server state and makes the chart service easier to externalize. However, you may run into some practical limitations if your data set is large.

Community
  • 1
  • 1
McDowell
  • 107,573
  • 31
  • 204
  • 267