aspose file tools*
The moose likes Servlets and the fly likes java.awt.Image to a HTML file via a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "java.awt.Image to a HTML file via a servlet" Watch "java.awt.Image to a HTML file via a servlet" New topic
Author

java.awt.Image to a HTML file via a servlet

Sachin Deshpande
Greenhorn

Joined: Jan 31, 2001
Posts: 25
I have a java.awt.Image objbect. I want to display it in a dynamic generated HTML page that I am creating in a Servlet. How am I supposed to do it?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You can't. An AWT Image can only be displayed in an Applet or a Frame.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Sachin Deshpande
Greenhorn

Joined: Jan 31, 2001
Posts: 25
I am thinking of trying servlet chaining. show the image in an html page and call that servlet via another servlet using the servlet chaining mechanism. hopefully that should work
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
How is that supposed to work? The only images that will display on an HTML page are images created with an img tag unless you add the Image object to an applet.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
of course you can do it! You can do it in Perl so why shouldn't you be able to do it in Java?
Here is a sample program:

Here's the HTML:
<HTML>
<HEAD>
<TITLE>Image Display HTML</TITLE>
</HEAD>
<BODY bgcolor="black">
<br>
<br>
<br>
<center><img src="/servlet/ImageDisplayer"></center>
</BODY>
</HTML>
[This message has been edited by Thomas Paul (edited February 16, 2001).]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java.awt.Image to a HTML file via a servlet