This week's book giveaway is in the Other Open Source APIs forum. We're giving away four copies of Storm Applied and have Sean Allen, Peter Pathirana & Matthew Jankowski on-line! See this thread for details.
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
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!!!
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.
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.