File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes how to display an image in a jsp through ajax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "how to display an image in a jsp through ajax" Watch "how to display an image in a jsp through ajax" New topic
Author

how to display an image in a jsp through ajax

kunal vermaa
Ranch Hand

Joined: Jun 27, 2009
Posts: 51
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

Joined: Feb 23, 2009
Posts: 1183
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.


JDBCSupport - An easy to use, light-weight JDBC framework -
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

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.

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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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

Joined: Jun 27, 2009
Posts: 51
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

Joined: Feb 23, 2009
Posts: 1183
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

Joined: Jun 27, 2009
Posts: 51
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

Joined: Feb 23, 2009
Posts: 1183
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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

Joined: Jun 27, 2009
Posts: 51
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

http://simple.souther.us/ar01s10.html
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to display an image in a jsp through ajax