Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Display images from database to jsp in a grid format  RSS feed

 
Ann Joseph
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I am working on a JSP page and I am trying to display images from Postgres SQL database to JSP pages using the servlet.I wanted to display those images in a table format eg:  Name, address,city, image ( kind of this format) I have more than 100 records with an image in my database. I prefer to display images directly from the database to JSP rather than saving in a folder and put the path in src. I am able to write image using servlet on the jsp page.but then only image is writing and my other records are missing.
I tried to call servlet from< img src tag and its not working.
here is my trial
jsp page:

Servlet:

Also I am getting error "getOutputStream is already called for this response"

Servlet is calling twice if I remove RequestDispatcher I couldn't get response byte[] in servlet.
Any help will be appreciated!

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ann Joseph wrote:I tried to call servlet from< img src tag and its not working.

That is the correct approach and what you need to get working. What's not working about it?

Also, you should not be putting Java code in a JSP. That's a bad and obsolete practice from over 15 years ago! 
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your JSP looks like it is trying to send back text AND image data all in one response (via rd.include)
The thing about this approach is that you have to remember how HTTP works.
For the most part it will either send text, or it will send data.  You can't send both in the same response.
You can either getWriter() - which a JSP does by default, or you can getOutputStream.  But not both. That is the reason for the "getOuputStream is already called for this response" exception. 

So how do you do this then?

Your JSP should send back the HTML for the page, including image tags at the appropriate points, with a url to load the image from.
The browser loads and parses that HTML, and then makes HTTP requests for each image.

The trick is to be able to identify which image to load out of your database for each url.
Yes, this means you will have to make multiple servlet requests (one for each image), and probably multiple database calls (one for each servlet request).  Unless you want to retain ALL the images in the server memory (not recommended)
You definitely won't be able to use a request attribute in any case

So what I think it should look like is more like:



Servlet - this is a seperate request
 
Ann Joseph
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear Bibeault,
Thank you for your reply
Yes, I know that its a bad approach to writing java code inside jsp.but am hosting this page on LAN so not much concern about coding standards at this point.just wanna show this output to my manager  
I am trying to display around 100 records (with each record has an image) as a table in a JSP page.with my code, I can display either all records without image or servlet will write, just one image.But I need both my record and image.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ann Joseph wrote:but am hosting this page on LAN so not much concern about coding standards at this point.

I cannot agree on this point. Coding standards always matter.

But I need both my record and image.

The <img> element is the way to show images in a page.
 
Ann Joseph
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stefan,
Thank you for your time!

I understood the code you explained. I tried to pass byte array as query string just to avoid multiple DB calls, I know those are bad practices just was a trial <img src="ImageServlet/img*?image=<%=bytearray[] %>">  but it doesn't work as it corrupted when it reach servlet.
Of course, I can try with image Id, for now, to make it work as I have a finite number of records to display.
But I would like to know what is the right approach to displaying image from DB to page in the professional way as this situation is very basic and common.
It would be great if you could lead me to the right approach.
 
Ann Joseph
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Bibeault

Yes, I agree with you coding standards matters .As you said its 15 years back approach,Yes I worked with JSP may be 7 or 8 years back so am not updated with the new standards Just trying old school .appologies.
Thank you if you could lead me to the right way. Open to learn technologies.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ann Joseph wrote:
Of course, I can try with image Id, for now, to make it work as I have a finite number of records to display.
But I would like to know what is the right approach to displaying image from DB to page in the professional way as this situation is very basic and common.
It would be great if you could lead me to the right approach.


The image id is the way to do it.
You do have two calls to the db.
That should rarely be an issue.

And you will only ever have a finite number of images to display.
There's only so many you can fit on the screen...a hundred sounds like too many.  A Google image search on my MacBook makes around 50-60 image calls (yes, I just counted them).
 
Junilu Lacar
Sheriff
Posts: 11146
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear, correct me if I'm wrong here but if by "professional" we're talking about sites like Facebook and Google Images, where you'd scroll down through a page and display the (potentially hundreds or thousands) of images, then what's happening is a combination of Javascript that checks the user's scroll position on the page, correlates that to a HTML element's relative position on the page,  and then makes AJAX calls that retrieves the images and then modifies the DOM tree on the fly.

The JSP has very little involvement in this process. It's mostly JavaScript running on the client side.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Bear, correct me if I'm wrong here ...

The JSP has very little involvement in this process. It's mostly JavaScript running on the client side.

Yup.
 
Ann Joseph
Greenhorn
Posts: 5
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With imageId my code is working fine for my records. Printed 100 records on one page.
Thank you all for your help.!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!