aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Show images dynamically from DB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Show images dynamically from DB " Watch "Show images dynamically from DB " New topic
Author

Show images dynamically from DB

kavin clain
Ranch Hand

Joined: Jan 19, 2010
Posts: 68
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?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61043
    
  66

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.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
kavin clain
Ranch Hand

Joined: Jan 19, 2010
Posts: 68
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.







Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.


GenRocket - Experts at Building Test Data
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61043
    
  66

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

Joined: Jan 19, 2010
Posts: 68
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61043
    
  66

See the FAQ.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Show images dynamically from DB