This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Show images dynamically from DB

 
kavin clain
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64980
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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.







 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64980
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64980
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the FAQ.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic