• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Displaying an image in a servlet

 
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67747
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
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?
 
Bear Bibeault
Sheriff
Posts: 67747
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

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


Huh? For an image?
 
jishnu dasgupta
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67747
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

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
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 103
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.




 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic