0

I'm using JFreeChart to generate a dynamic chart depending on the user input. I have a JSP with some textbox and combobox, the user makes the input and submits it, and the Action process it, generating an image of a chart. I need to display this image on the same JSP as before, below the textbox/combobox.

If I use response.setContentType("image/jpeg"); etc... then I get a page with the image alone. I thought of saving the image to a file and then access it with <img >, but I'm not sure that will work (need to save it to WebContent and I may not be able to access it always?).

Is there a way to somehow cache the image and then access it inside the JSP through an <img> or something? Maybe JFreeChart has an easy way to do what I want?

If it matters, I'm also using struts and spring on my webapp.

Thanks in advance.

Julián Pagano
  • 79
  • 2
  • 15

3 Answers3

1

I've not tried it, but you might look into org.jfree.chart.imagemap and a suitable URL generator from org.jfree.chart.urls. An outline of implementing a PieURLGenerator is illustrated here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

Well, if you generate the image on the server side, you could always just store it in a temp directory using something like a UUID to generate a unique name for it, and concatenating the image file extension on the end of it.

Make sure that the directory the image is generated is accessible on the webserver, and then send the URL path to the image file on the server back to the JSP using ajax (Direct Web Remoting), for display using Javascript.

Just make sure you also have a chron job or service to clear the older files out of the directory now and again.

Community
  • 1
  • 1
leeand00
  • 25,510
  • 39
  • 140
  • 297
  • 2
    Note that accessing the file system in a web application make you vendor dependent. – Thorbjørn Ravn Andersen Mar 29 '12 at 14:34
  • @ThorbjørnRavnAndersen Good point! Tying yourself to a particular OS when writing a Java WebApp is a bad idea. – leeand00 Mar 29 '12 at 14:38
  • When you say a temp directory you mean a directory inside my WebContent? Or any other directory on the file system? I need to make sure I can access the file independently of the OS, etc. The UUID won't be necesary, I can just overwrite the file everytime, but I need to make sure I can access it from the JSP always. How can a get the real path to my WebContent folder? – Julián Pagano Mar 29 '12 at 14:46
  • 1
    The servlet API does not guarantee that a WebContent folder even exists. – Thorbjørn Ravn Andersen Mar 29 '12 at 15:14
  • @Julian Thorbjørn has a good point, you should listen to him. – leeand00 Mar 29 '12 at 15:28
  • Ok, I think I made it work. I get the servlet path with `request.getSession().getServletContext().getRealPath("/saved.png");`, use that path to create and write the image file, then on the JSP I use `"${pageContext.request.contextPath}/saved.png"` to get the URL where the image is stored, is this ok? Will I be able to access the image independently of the platform or how the web proyect is deployed? Thanks for all your responses. – Julián Pagano Mar 29 '12 at 15:34
  • @Julian if you are going to ignore Thorbjørn's advice (which I wouldn't, but that's what you're going to do) be sure that you use a UUID for the image because of what will happen if more than one person is using your web-app at the same time. – leeand00 Mar 29 '12 at 15:36
  • 1
    @leeand00 I'm trying both options. The app I'm working on is just for an univerity proyect that needs to display some reports, I only need to show it to my professor, deploying it localy, only one user will access it. I'm using java/webapps instead of windows-based because is what I know. I will try to implement Thorbjørn's solution though, but I still don't fully understand it. Thank you both. – Julián Pagano Mar 29 '12 at 15:46
0

You should have a servlet that can create the image you want solely from the URL. The URL can then contain an id, which maps back to an object in your program containing raw data in memory. The servlet then generates the image and returns it.

You can then simply set the url of the image in your current web page in Javascript, and it should be loaded.

This is because JSP's are character oriented which do not lend well to binary data so you need to have a servlet do it.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • Sorry, but I'm not sure I understand. You're saying I need two servlets? One to process the input from the user and another to generate the URL to the image? The image is created acording to the user input, if I then need to use another servlet with my ``, how can I access the image I created before? Sorry if I'm understanding this the wrong way. – Julián Pagano Mar 29 '12 at 14:52
  • No. Just one which generates the image. Your session probably holds the information that the servlet needs already. – Thorbjørn Ravn Andersen Mar 29 '12 at 15:12
  • Sorry, I've read your answers over and over again and I still don't understand your solution. What do you mean that I can "create my image solely from the URL"? The image (a chart) is created depending on the user input, submitted through a form. What do you mean that the servlet "returns" the image? It returns the URL where it's stored? I really want to understand your solution, sorry if these are kind of dumb questions. Check my responses to leeand00 in case it has actualy some relation to your solution. Thanks for your responses. – Julián Pagano Mar 29 '12 at 16:25