• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Showing image from servlet

 
asheesh talwar
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
asheesh talwar
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic