Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sachin Deshpande
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. An AWT Image can only be displayed in an Applet or a Frame.
 
Sachin Deshpande
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic