java - 如何从多个图像中获取图像ID,以使用它来获取有关图像的完整数据并在单击图像时将其显示在不同页面上

标签 java html mysql jsp servlets

我的代码用于显示多个图像并尝试获取它们的 id[currency.jsp]

<div class="container-1" style="background-color:white">
<div style="color:Red;font-size:1.1em;font-weight:bolder;">${err}</div>
<%
try
{
    DBConnector obj2=new DBConnector();
    obj2.createConn();
    Statement st=null;
    st=obj2.getStatement();
    ResultSet rs=null;
    String query="select itemid,itemname,itemprice from currency";
    rs=st.executeQuery(query);
    int i=0;
    %>
    <table>
    <%    
    while(rs.next())
    {
       int price=Integer.parseInt(rs.getString("itemprice"));
       int id=Integer.parseInt(rs.getString("itemid"));
       String name=rs.getString("itemname");
       if(i==0)
       {
       %>
       <tr>
       <%
       }
       %>   
       <td style="width:220px;height:250px">
       <form id="formimgid" action="Getdetailinfo" method="post">
       <input type="hidden" name="getimgid" value="<%=id%>">
       <a href="#" onclick="document.getElementById('formimgid').submit()">
       <div class="cont">
       <img src="Getimage.jsp?id=<%=id%>" width="220" height="150">
       <div><%=name%></div><br>
       <div>Price = <%=price%></div>
       </div>
       </a>
       </form>
       </td>
       <%
       if(i==3)
       {
           i=0;
       %>
       </tr>
       <%
       }
       else
       {
       i++;
       }
       }
       %>
    </table>
   <%
    rs.close();
    st.close();
    obj2.closeConn();
}
    catch(SQLException e)
        {
           System.out.println(e);

        }

%>
</div>    
</div>

Getimage.jsp 代码

<%
   try
   {
    DBConnector obj1=new DBConnector();

    obj1.createConn();
    Statement st=obj1.getStatement();
    ResultSet rs=null;
    int id=Integer.parseInt(request.getParameter("id"));
    String filename="image"+id+".jpg";
    String query="select img from currency where itemid='"+id+"'";
    { 
    rs=st.executeQuery(query);
     String imglen="";
    if(rs.next())
    {   
        imglen=rs.getString("img");    
    }
    rs=st.executeQuery(query);
    if(rs.next())
    {
        int len=imglen.length();
        byte rb[]=new byte[len];
        InputStream readimg=rs.getBinaryStream("img");
        int index=readimg.read(rb,0, len);
        rs.close();
        st.close();
        obj1.closeConn();
        response.setContentType("image/jpg");
        response.setHeader("Content-disposition","attachment; filename=" +filename);
        response.getOutputStream().write(rb,0,len);
        response.getOutputStream().flush();
    }
   }
   }
   catch(SQLException e)
   {
       System.out.println(e);
   }
%> 

我现在能够获取并显示我想要的多个图像,当我单击图像时,它将打开一个包含有关该图像的信息的新页面,但我从任何地方都得不到帮助。我在货币中使用的表单标签.jsp 是无用的,它只存储第一个图像的 id。这里我试图在用户单击它时获取图像 id,然后使用该 id 从数据库获取数据并将其显示在另一个页面上。任何帮助或替代方案表示感谢,谢谢..

我使用 Mysql 作为数据库,使用 Jsp servlet 作为前端。

最佳答案

如果我理解正确的话,当用户单击图像、名称或价格时,您希望将图像 ID 传递到下一页。

因此,在您的currency.jsp页面中,用此替换您的td标签

<td style="width:220px;height:250px">
    <a href="Getdetailinfo.jsp?id=<%=id%>">
        <div class="cont">
            <img src="Getimage.jsp?id=<%=id%>" width="220" height="150">
            <div><%=name%></div><br>
            <div>Price = <%=price%></div>
        </div>
    </a>
</td>

在我的代码中,我假设包含图像详细信息的页面称为 Getdetailinfo.jsp,如果不是,则在 a 标记的 href 属性中更改它。

在 Getdetailinfo.jsp 页面中,您可以使用以下方式获取图像 id

 <%
 int image_id = Integer.parseInt(request.getParameter("id"));
 out.println(image_id);
 %>

我没有 jsp 经验,但我假设这就是您从查询字符串中获取 id 的方法,就像您在 getimage.jsp 页面中所做的那样。

关于java - 如何从多个图像中获取图像ID,以使用它来获取有关图像的完整数据并在单击图像时将其显示在不同页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43814059/

相关文章:

html - 如何在flexbox中让div不收缩,而其他的增长收缩

jquery - "before and after"交互式图像 mask

mysql - 如何在数据库 (MySQL) 中的所有表中运行查询以查找大于今天日期的日期字符串值?

java - JPA在MySQL数据库中保存错误的日期

php - sql按最近日期排序

java - 这是多态性吗?

java - 避免 Iterator ConcurrentModificationException 的方法

html - CSS3 不适用于 HTML

java - 无法从连接到 EC2 上 Cassandra 的 EMR 运行 Spark 作业

java - 将监听器添加到 SpanElement