• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Servlet to display an image

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a requirement in which I need to display an image which resides on a remote machine using a servlet. I have tried using a servlet in which content -type of response is set to html and displaying the image using the img tag.

But that trial is in vein as this will not work when accessing the servlet from the client machine.

Can anyone please suggest me the way to achieve this??

Thanks,
DNV Srikanth.
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is the image on the same server the servlet container is on?
 
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"DNV Srikanth ", please check your private messages for an important administrative matter.
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you accessing the image on the remote machine? If the image available via http? If so then an img tag should work. If, on the other hand, the image is not http accessible but is only accessible as a file, your servlet would have to open the image file, read in the contents and place those in the response buffer. In addition, you would have to set the mime type to match the image type.

For example, something like this:



Warning - I transcribed the above from some Groovy code I have that does the same thing, so it has not been tested.

Now, this code cannot go in the same servlet that builds the page. Instead, the servlet that builds the page creates an img tag that references another servlet, and the above code goes into that servlet. (Or it could be the same servlet but with two different outputs based on the input.)
[ October 31, 2008: Message edited by: Peter Johnson ]
 
Devarakonda Srikanth
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for the solution. I've tried this too. This was successful in all the operating systems except in windows 2003 server.

Srikanth
 
Bear Bibeault
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"DNV Srikanth" please check your private messages for an important administrative matter. Again.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter's tip worked great on my Mac! Thanks, exactly what I needed!
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would finetune the code a bit more. It is not written quite efficient. It places the whole image in the memory instead of buffering it. If you have a lot of fairly big images and a lot of requests are been fired on the servlet concurrently, then the server may run out of memory sooner or later.

You can find here an example of an image servlet which does its task efficiently: http://balusc.blogspot.com/2007/04/imageservlet.html
 
Ranch Hand
Posts: 109
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bauke Scholtz wrote:You can find here an example of an image servlet which does its task efficiently: http://balusc.blogspot.com/2007/04/imageservlet.html


Thanks for Bauke. The the link you have posted is very helpful.
 
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic