Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet to display an image

 
Devarakonda Srikanth
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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.
 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is the image on the same server the servlet container is on?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"DNV Srikanth ", please check your private messages for an important administrative matter.
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"DNV Srikanth" please check your private messages for an important administrative matter. Again.
 
Zachary Anthony
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter's tip worked great on my Mac! Thanks, exactly what I needed!
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • 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
 
Udara Amarasinghe
Ranch Hand
Posts: 109
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic