Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying an image in a servlet

 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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!!!
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • 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.
 
Stefan Evans
Bartender
Posts: 1722
10
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.




 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic