Extract multiple images from a database using jsp-servlet

Take a look at the following code snippet that I use to extract images from a database:

response.setContentType("image/gif");
String url="jdbc:oracle:thin:@localhost:1521:xe";
String username="xyz";
String password="abc";

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection(url,username,password);
String sql="Select name,description,image from pictures";
PreparedStatement stmt=conn.prepareStatement(sql);
ResultSet resultSet=stmt.executeQuery();
ServletOutputStream sos=response.getOutputStream();

while(resultSet.next()) {
    byte[] buffer=new byte[1];

    InputStream is=resultSet.getBinaryStream(3);
    while(is.read(buffer)>0){
      sos.write(buffer);
}
sos.println();
sos.flush();
}

sos.close();
conn.close();

I am trying to have this code display images that are retrieved from the database. This code should extract multiple images from multiple lines that are stored in the database. But this code displays a single image.

+3
source share
3 answers

Why does it display multiple images? You specify the content type of the response to the image / gif, and this means that the browser expects a single image. However, you are passing multiple images to the response stream.

, . ( , ).

, , , ( SQL).

+3

. .

"", . , "".

, ( ).

+1

As already pointed out by Brian and Colin, you cannot have multiple images, and all of them are available at the same time. It seems your task is to have many images on one page. then one of the methods proposed by Brian (using JSP, which requires images). Another way is to split your page into several sets of frames, which themselves can cause images in the same way as JSP does.

+1
source

Source: https://habr.com/ru/post/1767645/


All Articles