Originally posted by Thomas Paul:
You can't. An AWT Image can only be displayed in an Applet or a Frame.
Bzzzt.
Images work just as well for drawing offline images. Let's start at the client -- you incorporate an image using an <IMG> tag. Point this tag at a servlet. This servlet will generate the binary stream that represents the image as output.
The best thing is to communicate all the
parameters necessary to generate the image to your servlet, rather than pre-generate the image and leaving it to consume resources on your server until the client requests it. These parameters can sit either in image URL parameters or in your session context.
The servlet then generates the image. Make sure you have a javax.awt.image.BufferedImage. Use the graphics context or whatever to draw on it what you want.
The next step is to convert the image to something the browser understands. Sun's JDK only ships with a JPEG encoder afaik (com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder()). This encoder takes your BufferedImage, encodes it and writes it to an OutputStream. The response.getOutputStream() will do fine. Be sure to set your response headers
before writing the image
especially response.setContentType().
- Peter