繁体   English   中英

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

[英]I want to display multiple images from database into jsp (i am mapping servlet) so in jsp m gonna display in src of img tag

我在表中使用longblob数据类型存储图像,至少存储了五张图像,我想从数据库中检索所有图像,并想在包含图像标签的jsp上显示,图像标签的属性src被分配给servlet名称为src =“ ./ Serv1”,此Serv1包含从数据库中检索到的图像,但问题是我不知道如何显示多个图像,它仅显示第一个图像,如果可以,我应该使用循环吗?

我在JSP页面中有这个

 while(r.next())

              {

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

而我的url模式为Serv1的servlet具有此代码

   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您必须在doGet检索id并传递查询,最后将响应发送回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

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM