using servlet to create a html page with embedded Image

Anybody can help me on how to create a html page with embedded image inside the html page. In the html page I can put some navigation buttons also diplay some parameters about the image. Thanks. Ruilin

JUNILU, Thanks a lots. Would you please give me more comments. My situation is using a servlet to generate .gif on fly. I do not have a .gif file stored somewhere. My servlet needs to get data from a calling page, then use these data to construct a "off-screen" image, then send to client. How to conduct this? Please give me more clarification.

I have not done this before, but i have some idea using C. I guess the idea work in Java too. I think you have to create the image pixel by pixel in 2D unsigned char arrays, then convert this image to .gif or jpg format and send it out using BinaryOutputStream(don't remember the exact class name) object. I am not sure java provide any ready class to produce .gif or .jpg image format. Check the documentation for this issue.

The standard java class libraries has some unsupported Sun libraries. Included in this is the package com.sun.image.codec.jpeg that includes classes for creating, well, jpeg images. I'm not aware of open source for creating gif, it might have something to do with the patent on the gif encoding algorithm. Help much? Dave.

Most books on servlets, including mine and Hunter & Crawford, have a section on creating images on the fly. Your img= tag will point to a servlet with a parameter or two to indicate which image is required. You need the Java 2D image package which includes GIF and JPG encoders. I think that is still at:http://java.sun.com/products/java-media Bill

SoonAnn, David, and William, I have generated .gif and sent to client and displayed inder the browser. It works fine for a bare graph and labbel, etc. This approach described in a few servlet books, as William said. My problem is how to embed the graph (image) into a html so that I can display extra parameters about the graph (image) and also some navigation buttons. Any comments on how to mix the servlet generated .gif with html. Thanks in advance Ruilin

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 13004

5

posted Jun 28, 2001 07:19:00

0

"Any comments on how to mix the servlet generated .gif with html." You have to "Think like a browser" when questions like this come up. A browser makes a separate request for every single resource that a HTML page refers to. A page does NOT mix data types. You have to figure out a way for your servlet generated gif to be served by a separate request as generated by the img tag URL - lots of approaches are possible here. Bill

ruilin yang
Ranch Hand

Joined: Feb 25, 2001
Posts: 334

posted Jun 28, 2001 08:35:00

0

William, Thanks, Could you give me more guidance on a approach on how to imlement this ? I stucked with passing x[], y[] (can be many as 200 pairs) to the GraphServlet to generate "off-screen" image to send to client to display. If I could pass all the required data x[],y[],etc. for ploting the graph in the following call <img src = "http://machineNamwe ortNum/appNmae/servlet/GraphServlet"> I can just use a wrapper html (.jsp) page to disply an "html embedded image". Please comments, that will help me a lots. Thanks in advance. Ruilin

One thing you might be able to do: In the servlet that generates the HTML page which houses the <img src="http:/servlet/GraphicServlet"> tag, before outputting this line, place an object which contains your x[] and y[] into the HttpSession. Your GraphicServlet may then retrieve the object from the HttpSession and have access to your values. To do this you will want to create some simple class which contains the desired attributes. Example:

Hope this helps. Jason

ruilin yang
Ranch Hand

Joined: Feb 25, 2001
Posts: 334

posted Jun 28, 2001 12:44:00

0

Thanks Jason, I am trying this technique. If it works it would be a great solution for the problem.

Thanks again Ruilin

SoonAnn Lim
Ranch Hand

Joined: Jun 21, 2001
Posts: 155

posted Jun 28, 2001 12:54:00

0

I have another idea. You have use your servlet to create an image file and save it on the disk with appropriate name determined by the users' session or cookies. After that, output your html code using regular System.out.println statement to include this generated image using <img src="UserImageXXX.jpg"> where XXX can be the id obtained from session. This way, you may end up many image files on disk, but you can delete the images from the disk once the user leave the page. This way, you can put other parameters as you want to and other buttons.