HttpSession 属性:仅 getAttribute() passing/getting 循环中的最后一个值

HttpSession attribute : getAttribute() only passing/getting last value in the loop

我正在尝试使用 for 循环从数据库中检索多个图像,其中数据库存储了 4 个不同的图像,ID 列中的 ID 编号为 1 到 4。我已经使用会话传递 for 循环的每个增量值(为每个 select 元素选项检索所有 4 个图像),但只有 ID=4 的最后一个图像显示在 select 的所有 4 个选项中元素.

请查找数据库图片。

Jsp 页

<div class="menu">
                <form action="guestVenueDetails.jsp">
                <select id="name" name="eventName" >
                    <option value="" selected disabled hidden>Select an Event</option>
                    <option value="IKEA">IKEA</option>
                    <option value="Nike">Nike</option>
                    <option value="Adidas">Adidas</option>
                    <option value="Puma">Puma</option>
                </select>
            </div>

        <div class="content">
        <%
        for(int i =1; i<=4; i++){
   
        session = request.getSession(true);
        String id = ""+i;
        session.setAttribute("eventSelection",id);
        %>
           <p class="image"><img src="./DownloadImage" alt=""></p>

        <% } %>
        </div>    
</form>

/下载图片页面

@WebServlet("/DownloadImage")
public class DownloadImage extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    

    @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            // TODO Auto-generated method stub
        try {
              HttpSession session = req.getSession(true); 
              String id = (String)session.getAttribute("eventSelection");
              session.setAttribute("eventSelection", null);
              session.removeAttribute("eventSelection");
             
            
            byte[ ] img = null ;
            ServletOutputStream sos = null;
            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/event_booking","root","");
            
            String sql="select image_field from images where id ="+id;
            PreparedStatement ps= con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            if(rs.next()) {
                img= rs.getBytes(1);
            }
            sos = resp.getOutputStream();
            sos.write(img);
            
        
        }catch(Exception e) {
            e.printStackTrace();
        }
        }

}

数据库图像 Database Image

要完成这项工作,您需要将图像的 ID 放入用作 img 标签来源的 URL。
src="./DownloadImage?Id=1" 然后,您可以使用访问端点中的 id req.getParameter("身份证号");