aspose file tools*
The moose likes Servlets and the fly likes Displaying an image in a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Displaying an image in a servlet" Watch "Displaying an image in a servlet" New topic
Author

Displaying an image in a servlet

jishnu dasgupta
Ranch Hand

Joined: Mar 11, 2011
Posts: 103

Hi,
I am trying to display an image in a servley, using the html <img src> tag.....my code is as follows



I am using Netbeans IDE for my application. The problem is, while the image is displaying fine in the localhost machine, when i am accessing the servlet through other systems in the network, the image file is not getting displayed.

P.S: the MIME type needs to be "text/html"

Please advice as I am at my wits end.

If debugging is the process of removing bugs, then programming must be the process of putting them in. -- Edsger Dijkstra

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Of course that's not going to work. Did you look at the HTML that was sent to the browser? Is the src URL one that can be understood on any but the server system?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

jishnu dasgupta wrote:P.S: the MIME type needs to be "text/html"

Huh? For an image?
jishnu dasgupta
Ranch Hand

Joined: Mar 11, 2011
Posts: 103

Hi Bear,

I tried the alternative, where i placed th image file in the root directory.

So while i am able to access the file with the url: http:\\ip:port\root\imageFile,

I am not able to display it in the servlet, although it works fine for the local host. And i am using the text/html, as i need to display some text along with the images, and i though using that mime type would make my life a lot more easier, if it wasnt for this problem!!!
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1031
    
    5

the MIME type needs to be "text/html"


I take it you mean for what the servlet is returning in the response and not the image?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

jishnu dasgupta wrote:So while i am able to access the file with the url: http:\\ip:port\root\imageFile,

Your code cannot possibly be generating that URL. What is the actual generated URL in the HTML?

if it wasnt for this problem!!!

Exclamatory sentences in English end with a single exclamation point.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1031
    
    5



This path may be fine on the server - say it equates to C:/images/image.gif. When the client receives this response however, it will look for the image in the same location.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1027
The getRealPath() method turns a website relative path into a real location on disk so you can load the file using File IO.

But the src of an image tag is supposed to be a website relative path.

So if the image is under your web context, then just put the path relative to your web context. : eg /image.gif

If the image file is NOT under your web context, then you need to write a servlet that will open the image file on disk (wherever it is) and then write it to the servlet output stream. Google "ImageServlet" for an example.

jishnu dasgupta
Ranch Hand

Joined: Mar 11, 2011
Posts: 103

Hi Stefan,


I tried keeping the image in the web context, but it did not work out. I might possibly me making an error on the same. could you please show an example of doing it. Regarding opening the file by a separate servlet and then writing in output stream, the challenge is I have a form with a group of buttons whose color change automatically depending on some back end data. So I am really confused, that every time i set the background of the button to some image file, so i need to call the servlet...and if i do so how to go about with it.




 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Displaying an image in a servlet