Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

how to display an image in a jsp through ajax

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

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

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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67651
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67651
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
http://simple.souther.us/ar01s10.html
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic