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

JSP page only loads one image from database  RSS feed

 
Ben Coleman
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JSP page where I am loading data from a database in a loop. One of the database columns holds images stored as Blob. I am able to load all data from the database successfully when I don't try to load the image column.

When I load all data inclusive of images, only one image shows up on the page and no other data is shown on the page. Why is that so. As you can see from code, I have 5 columns of strings and 1 column of Blob. I should be getting each of this for 5 rows.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

I'll start with the customary admonition to not be putting Java code in a JSP page. That's a bad practice that's been obsolete since 2001; 15 years! You should be following modern practices and using only the JSTL and EL in JSP pages.

That said (and I reserve the right to say it again), the first step to debugging something like this is always to do a View Source at the browser to see how the generated HTML differs from what is expected.

 
Ben Coleman
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Welcome to the Ranch!

I'll start with the customary admonition to not be putting Java code in a JSP page. That's a bad practice that's been obsolete since 2001; 15 years! You should be following modern practices and using only the JSTL and EL in JSP pages.

That said (and I reserve the right to say it again), the first step to debugging something like this is always to do a View Source at the browser to see how the generated HTML differs from what is expected.



Thanks but not the first time I am hearing that this is bad practice. Having said that, I am new to using Java for Web applications and literally everywhere I turn to, the examples have Java code in JSP. It seems to be the most popular. Also this is the last part of my application. I do not want to restart my coding all over to maintain good practice.

Regards to issue - When only a single image appears when I try to call image and all Strings from database, the view source is greyed out. So I tried with inspect and everything is missing. Even my navbar which comes right at the start of the body before the java code goes missing. On inspect, I only have the following inside the body.


 
Paul Clapham
Sheriff
Posts: 22504
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I don't know what the heck that's supposed to do... the "src" attribute is supposed to contain a URL from which the browser can download the image. It looks like you've tried to put the binary code of the image into that attribute instead.

At any rate when you're writing a JSP you should start by writing the HTML it's supposed to generate. You should really do that in this case, and decide what the "src" attribute of that tag should contain.
 
Ben Coleman
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:

I don't know what the heck that's supposed to do... the "src" attribute is supposed to contain a URL from which the browser can download the image. It looks like you've tried to put the binary code of the image into that attribute instead.

At any rate when you're writing a JSP you should start by writing the HTML it's supposed to generate. You should really do that in this case, and decide what the "src" attribute of that tag should contain.


That line ends up giving me one image. resultset.getBlob(1) retrieves the stored Blob from the database right... How can I convert this into a proper url then, since the way I am doing it seems wrong?
 
Paul Clapham
Sheriff
Posts: 22504
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll want the URL in that attribute to send a request, including the image key, to something which will then return the image. The whole image and nothing but the image. And don't forget to set the content type of the response to indicate that it's an image and not HTML.

A JSP is not the right tool to return an image, either. JSP is for HTML, or rarely for other text formats. You'll need a servlet for that. If you google for "java download servlet" you should get sample text into which you'll need to insert your code to get your binary data from the database (you already have something like that) and return it as the response.
 
Ben Coleman
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have resolved this somewhat but have a different issue. Since it is a different question from what I raised here, I will create a new one.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!