This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Servlets and the fly likes Servlet to display an image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet to display an image" Watch "Servlet to display an image" New topic
Author

Servlet to display an image

Devarakonda Srikanth
Greenhorn

Joined: Aug 27, 2007
Posts: 25
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

Joined: Sep 10, 2008
Posts: 188
is the image on the same server the servlet container is on?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

"DNV Srikanth ", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5835
    
    7

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 ]

JBoss In Action
Devarakonda Srikanth
Greenhorn

Joined: Aug 27, 2007
Posts: 25
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

Joined: Jan 10, 2002
Posts: 61309
    
  66

"DNV Srikanth" please check your private messages for an important administrative matter. Again.
Zachary Anthony
Greenhorn

Joined: Mar 12, 2009
Posts: 1
Peter's tip worked great on my Mac! Thanks, exactly what I needed!
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Aug 17, 2009
Posts: 109

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.
 
Consider Paul's rocket mass heater.
 
subject: Servlet to display an image