java - 我想将数据库中的多个图像显示到 jsp(我正在映射 servlet),所以在 jsp 中,我将显示在 img 标签的 src 中

标签 java mysql jsp servlets

我在表中使用longblob数据类型来存储图像,至少存储了五张图像,我想从数据库中检索所有图像并希望在由图像标签,图像标签的属性src组成的jsp上显示被分配给 servlet 名称为 src="./Serv1",这个 Serv1 包含从数据库检索的图像,但问题是我不知道如何显示多个图像,它只显示第一张图像,如果是的话我应该使用循环那怎么办

我在 JSP 页面中有这个

 while(r.next())

              {

      %>
      <img src="./Serv1" height="100" width="200"> 
      <p>Product <%=r.getInt(1)%>: <%=r.getString(2)%></p>

我的 servlet 的 url-pattern 是 Serv1 有这段代码

   ResultSet r=st.executeQuery("select prodimg from product;");

if(r.next()){

                img= r.getBlob(1);

              imgbyte=img.getBytes(1, (int)img.length());
              response.setContentType("image/jpg");
              oos=response.getOutputStream();

           }

            oos.write(imgbyte);
            con.close();

最佳答案

假设您有一个要检索图像的 jsp 页面。你可以做这样的事情来从数据库中检索任何图像。

 <% // dbconnection
          try {
                   Class.forName("com.mysql.jdbc.Driver");
                 java.sql.Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","");
                  Statement statement = conn.createStatement() ;
       resultSet=statement.executeQuery("select * from product") ; 
                %> 
    <!--this loop will get all images-->
       <% while(resultSet.next()){ %> 
    <!--I'm using id column of table,you can use any coulmn which is unique to all row-->
   Image - <img src="./Serv1?id=<%=resultSet.getString("id")%>" width="20%"/>
  < p>Product <%=r.getInt(1)%>: <%=r.getString(2)%></p>

    <% 
    }
    }catch(Exception e){}

    %>

在上面的代码中这个 -> <img src="./Serv1?id=<%=resultSet.getString("id")%>" />线路很重要,这里你路过parameter即:id到 servlet 以获得特定的 image

现在,在您的 servlet 中即./Serv1你必须检索 iddoGet并传入查询,最后将响应返回给jsp页面。

Blob image = null;
        byte[] imgData = null;
       String id= request.getParameter("id");//here you are getting id 
       int i;
       ResultSet rs =null;

 try {

            //loading drivers for mysql
           Class.forName("com.mysql.jdbc.Driver");
             Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","");


         String sql = "SELECT prodimg FROM product where id=?"; //here pass that id in query to get particular image 

           PreparedStatement ps = con.prepareStatement(sql);


               ps.setString(1, id);
              rs = ps.executeQuery();    
 while (rs.next()) {
                  image = rs.getBlob("image");//getting image from database 
                  imgData = image.getBytes(1,(int)image.length()); //extra info about image
                } 

response.setContentType("image/gif");//setting response type



OutputStream o = response.getOutputStream();

o.write(imgData);//sending the image to jsp page 
o.flush();
o.close();


 }
    catch(Exception e)
         {
             e.printStackTrace();

         }

此外,这不是完整的代码,请根据您的要求进行更改。另外不要忘记添加 jar's file

希望对您有所帮助!

关于java - 我想将数据库中的多个图像显示到 jsp(我正在映射 servlet),所以在 jsp 中,我将显示在 img 标签的 src 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56058942/

相关文章:

java - 如何运行作为映射上的值的 lambda 表达式

mysql - 防止apache释放80端口

jquery - 如何在 Bootstrap 模态对话框中显示 URL : On button click

javascript - 将 .js 文件添加到 JSP 页面

Java依赖优先级

java - 玩!框架路线。长数据类型

Java hibernate如何持久保存没有关系的实体列表?

java - 当我使用 RequestDispatcher 包含 html 文件时,如何修复字符集编码?

java - 当值为 "entered by user"时,循环中的函数第一次跳过,否则工作正常..为什么?

php - 从数据库中检索没有相同 ID 的记录