Discussions

Web tier: servlets, JSP, Web frameworks: Newbie - problem generating chart images on the Server

  1. Hi,

    I have created a bar chart which displays data relevant to the current session of the user. This Image has been made it into a .png image file (using a PngEncoder class).

    As far as I can tell, I have to save this file onto the Orion server before a <img> tag in a JSP page can see the image.

    Does this mean that I have to uniquely name the .png so that subsequent visits to the web page (by the either the same user or different users) can be assured of seeing the correct image?

    Since this will create a new image file every time the page is viewed, I must routinely delete all of these image files before they fill up the server.

    Is there any way that a Image can be displayed in a JSP or a Servlet without writing the file to the server?

    Any thoughts would be most welcome.

    Many thanks,
    Richard Payne.
  2. You can use File.File.createTempFile(...) to create a temporary file with desired extension - then ask for its name and put into some scope.
    The link on your page could actually lead to a servlet that will read this file and wirte its content into its output stream. Just don't forget to set cache headers on your response - so that browser caches the image.
  3. You might also want to use static images and use
    plain-old HTML to "scale" the dimensions of the image
    to match your values.

    <IMG SRC="./static_image.gif" WIDTH=10 HEIGHT=<%=getvalue()%> >

    // My apologies to the Java police if this is not
    // quite kosher syntax... :)

    Most browsers do support this and it's FAR easier than
    dynamic image creation.
  4. If you generate the image on-the-fly, why put it in a file? Seems to me it would make more sense to make a servlet that returns the generated image data to the browser. Then you could say:
    <IMG src="../myServlet" .../>

    This shouldn't be a problem, as long as you make sure to set the reponse content type to the correct type... I'm not sure what MIME type you need, but it should be hard to figure.

    Gal