• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Show images dynamically from DB

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to show images which are saved in database.
I am sending request using ajax.
How to respond using Action class.
& how to dynamically set the image as div background or img tag?
 
Marshal
Posts: 67447
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

kavin clain wrote:I want to show images which are saved in database.


No problem. Just write a servlet that streams the data from the DB and sets the appropriate response headers. Then you can reference the servlet from anywhere that you can reference an image (<img> tag, CSS, etc)

I am sending request using ajax.


Problem. Why are using Ajax to fetch an image? What would you do with it?

How to respond using Action class.


See answer above about servlets.

how to dynamically set the image as div background or img tag?


That's just a matter of CSS. You don't need to fetch the image data -- just set the styles to reference the image URL.
 
kavin clain
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to show 5 images every time a request is made without refreshing the page.

The problem is I can see the images full path in Temporary Internet Files.
Below I am providing action class & javascript snippet.







 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're trying to create an image in JavaScript from a java.awt.Image object. That won't work. Read what Bear wrote again. You need to stream the bytes from the server. In JavaScript you might end up with something like this....



Where ImageStreamer is your servlet/action/whatever and 23 is the ID of the image to fetch from the database. Then stream the bytes of the file (not a java.awt.Image object) back to the client.
 
Bear Bibeault
Marshal
Posts: 67447
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, one more time.

You can't pass image data as XML back as an Ajax request. And, you don't need to.

You reference images by their URL. And that URL can be a servlet that returns the image data from the database.

All you need to be doing on your page is changing image URLs -- not fetching image data that you can't use.
 
kavin clain
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to both of you...

Could you please tell me one more thing that how to stream the bytes of the file from the action?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

kavin clain wrote:Thanks to both of you...

Could you please tell me one more thing that how to stream the bytes of the file from the action?



You'll have better luck asking that question in the Servlets or Struts forum (not both please).
 
Bear Bibeault
Marshal
Posts: 67447
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the FAQ.
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic