/
[email protected]/:
I think there is no image file generated at the server side. What
happened was that:
If you have had paid more attention to my example you would find no
file (image file on the file system) is created, just what you
describe below:
1) A Image java class was generated. With all graphics in it.
int width = 120, height = 160;
BufferedImage img =
new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
g.setColor(Color.BLACK);
g.drawRect(0, 0, width, height);
2) A stream was generated.
3) The stream was sent to the client and rendered.
OutputStream out = response.getOutputStream();
ImageIO.write(img, "png", out); /* see the API for the options */
g.dispose(); /* free up resources */
The ImageIO.write() method generates the image stream and sends it
to the specified output stream.
In the book "Core servlet and jsp", there is a chapter about how to
generated a gif image with servlet. The idea is to set the content
type to "image/gif". I think it works only if you display the image
but nothing else. If an image is just a part of a page, it won't work.
Yeah, you should set the corresponding response headers before
sending the data, but that's a separate issue:
resonse.setContentType("image/png"); /* if you generate PNG
* stream as I've used
* in my example */
Basically you should create a separate servlet to serve the image
charts. So you could get a "chart.jsp" which serves the HTML:
<html>
....
<img src="chart_image?whateverparam=neededvalue" ...>
....
</html>
and servlet mapped to "chart_image" URI which determines what
exactly to render from the supplied request params (in the URL query
part), for example.
Hope this helps,