aspose file tools*
The moose likes Servlets and the fly likes Showing image from servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Showing image from servlet" Watch "Showing image from servlet" New topic
Author

Showing image from servlet

asheesh talwar
Ranch Hand

Joined: Dec 10, 2000
Posts: 31
Hi
In one of our module we have to show the pie chart to the browser(html page) by using servlet.(We can not use jsp)
I am able to make the pie chart in the servlet but could not find a way to diplay it back to html page.
Any pointers in this regard are most welcome.
Thanx!
Asheesh
chanoch wiggers
Author
Ranch Hand

Joined: May 24, 2001
Posts: 245
rather than returning the html from your servlet, have a html page that has a reference to the servlet for the image. SO you would write the following html code:
<html>
rest of html page. My image is:
<img src="myserver.com/myservlet" alt="myimage"/>
</html>
the browser will load the html page and will then ask for the image as a seperate request. You then need to create the image in your servlet, set the content type of the response to image/gif or whatever (response.setContentType("image/gif")) and then stream the image to the response.
(thanks to Craig McLanahan for the answer) I hope that gets you started, shout if you need more info


chanoch<p><a href="http://www.amazon.com/exec/obidos/ASIN/1861007736/" target="_blank" rel="nofollow">Author of Professional Apache Tomcat</a></p>
asheesh talwar
Ranch Hand

Joined: Dec 10, 2000
Posts: 31
Hi chanoch
Thanx for the reply
I was able to get he image but now I am thinking is there any way by which I can convert my pie chart to an image(gif file) & store it temporarily to some dir on the hard disk & then display to the user?
Thanx!
Asheesh

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Two options:
1) Follow chanoch's advice. In the HTML page, put it in a IMG tag with the SRC set to the servlet. The servlet can then dynamically generate the pie-chart (or stream a cached version stored in a file or an image buffer) and write it to the servlet's output stream.
2) Have a servlet (or JSP file) handle the original page request. The servlet/JSP should generate the pie-chart and write it to a file in the web app's doc tree. It should then generate the HTML containing a reference to the generated image.
psuedo-code:
String relativeImageUrl = generateUniqueUrl();
createPieChart(relativeImageUrl, ...);
...
out.println("<IMG SRC=\"" + relativeImageUrl + "\" ...>");
...
One disadvantage of approach 2: You introduce the maintainance issue of cleaning up the generated images.
asheesh talwar
Ranch Hand

Joined: Dec 10, 2000
Posts: 31
Hi Avery
I am able to get the pie chart on the html page(using the first approach).I was just curious to know your second approach.
Can you please give some more detail about that?
Thanx!
Asheesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Showing image from servlet