• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Showing image from servlet

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic