0

I am trying to retrieve multiple images from database and display them using JSP page, I tried but not getting perfect logic to retrieve multiple images from database.

The below is code of retrieving single image, please help in retrieve multiple images

I am using mysql database

my servlet code:

package com.Image;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.codec.binary.Base64;


@WebServlet("/ImageRetrieve")
public class ImageRetrieve extends HttpServlet {
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {

            e.printStackTrace();
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {   Connection con = null;
    PreparedStatement ps = null;
ResultSet rs = null;
        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/database1", "root", "vicky");
            ps = con.prepareStatement("select frontimage from album ");
            rs=ps.executeQuery();
                    if (rs.next()) {
                        byte[] fi = rs.getBytes("frontimage");

                        String FI = new String(Base64.encodeBase64(fi), "UTF-8");
                        request.setAttribute("FIS", FI);
                        RequestDispatcher rd=request.getRequestDispatcher("RetrieveImage.jsp");  
                        rd.forward(request, response);  
                    }

            } catch (SQLException e) {
                throw new ServletException("Something failed at SQL/DB level.", e);
            }
        }


    }

my jsp code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Display Image</title>
</head>
<body>
<div>THE DISPLAY</div>
<div >
<img src="data:image/png;base64,${requestScope['FIS']}" style="width:50px; height:50px"/>

</div>

</body>
</html>

2 Answers2

0

Your issue is in the if-statement if (rs.next()) which only executes a single time regardless of number of results in the ResultSet. Because the if-statement returns true if there are any number of results in the ResultSet, the first image of the ResultSet is stored and the code moves on because the if-condition has been satisfied. The code has done exactly what you have told it to do. Changing the if-statement to while (rs.next()) will iterate through the entire ResultSet, saving each image to your predefined array.

J.Wright
  • 31
  • 7
  • thanks for your suggestion, if I use "while(rs.next())" how can i fetch those images, like "byte[] fi = rs.getBytes("frontimage");" – VIKAS DUBBAKA Sep 13 '18 at 16:36
  • Capture the images like so: `List images = new ArrayList<>();`. Use this images List within what will now be your while-loop `while (rs.next())` in place of your byte[] array when storing table data from the ResultSet. – J.Wright Sep 13 '18 at 16:48
  • ,thanks, will u please send how to iterate List in jsp page, please i am new to IT sector i'm in learning stage – VIKAS DUBBAKA Sep 13 '18 at 16:55
  • Looks like @Andrew S beat me to it, but here's an example implementation from another SO thread: https://stackoverflow.com/questions/20789207/iterating-through-a-list-object-in-jsp – J.Wright Sep 13 '18 at 17:29
  • thank you, for the help – VIKAS DUBBAKA Sep 13 '18 at 17:32
0
if (rs.next()) {
    ...
}

will only get the first byte[] from rs. Change this to a while loop to get each of them:

while (rs.next()) {
    ...
}

But you need a container, such as a List to hold them:

List<String> images = new ArrayList<>();
while (rs.next()) {
    ... 
    images.add(FI)
}

Then after the loop, set the attribute of the List and forward on:

request.setAttribute("FIS", images);
RequestDispatcher rd= ...;  
rd.forward(request, response);

And in the JSP, iterate over FIS (which is now a List) to display each one.

Andrew S
  • 2,509
  • 1
  • 12
  • 14