• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to display an image in a jsp through ajax

 
kunal vermaa
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hii, i want to display an image in a jsp on a button click.. the image path is stored in the database.. i m retreiving the path through database.. but when i m putting the string in <img/> html tag it is not displaying the image.. it is only displaying images placed in the Web-Content folder of my project..

can the <img/> html tag display any image placed in my system..? please explain
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you already got the image path from the database using ajax? Then the rest is a piece of cake.

As long as your image is not somewhere under WEB-INF, your browser can freely access it using the direct path.

What you need to do is simply accessing the image object using javascript and then changing the source of the image. The following code would do that.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take the time to choose the correct forum for your posts. This forum is for questions on JSP, not HTML.

For more information, please read this.

This post has been moved to a more appropriate forum.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kunal vermaa wrote:can the <img/> html tag display any image placed in my system..?

No. The file must be within a web application for it to be able to be addressed.
 
kunal vermaa
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have retreived the image path using a "Resultset" object.. the path is C:\Documents and Settings\hp\Desktop\New Folder\image6.bmp

then i did this.. and its not displaying the image..



i am new to javascript..please explain..
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I think it should work this way.

Anyway, this is not the way to go. Set up an Apache Webserver, this is where your project goes.

If your Apache directory for you web app is let's say

c:/program files/apache/htdocs/myWebApp

you have an index.html in this directory,

and the image would be under

c:/program files/apache/htdocs/myWebApp/img/

you would do it like this


 
kunal vermaa
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but i dont want to put the image in the apache server directory.. i just want a way to fetch an image from anywhere in my system..i.e from any path.. how can i do that through javascript....??
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
kunal vermaa wrote:can the <img/> html tag display any image placed in my system..?

No. The file must be within a web application for it to be able to be addressed.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Repeat: you cannot address files directly that are not in a web app. Period.

Using a file path like "C:\whatever" will allow the reference to work on your machine, and your machine only.

Just move the file into a web app. or write a servlet to read the file from disk and serve it indirectly. The latter approach has been discussed hundreds of times in these forums, but should only be used if you absolutely cannot move the files.
 
kunal vermaa
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANKS a lot for putting me right.. can you send me a link to a similar thread..? i am searching but cant find one.. Thanks..
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic