怎样传Arraylist到jsp,且jsp怎样接收并输出

来源:互联网 发布:浙江大学软件学院专业 编辑:程序博客网 时间:2024/05/22 14:08

ProductManager.java

//将查询到的数据封装到ArrayList中,这是查询操作,
 public static ArrayList getProductList()
 {
  String query = "select * from product";
  
  Connection con = DBBean.getConnection();
  
  ArrayList el = new ArrayList();
  
  try{
   
   Statement stmt = con.createStatement();
   
   ResultSet rs = stmt.executeQuery(query);//查询结果放在结果集中
   
   while(rs.next())//结果集遍历
   {    
    int ID = rs.getInt("id");
    
    String productNO = rs.getString("productNO").trim();
    String productIntroduce = rs.getString("productIntroduce").trim();

    Product product = Product.getInstance();//product的javabean
    
    product.setId(ID);
    product.setProductNO(productNO);
    product.setProductIntroduce(productIntroduce);
        
    el.add(product);
   }
   
   rs.close();
   
   stmt.close();
   
   con.close();
   
   return el;
  
  }
  catch(Exception e)
  {
   e.printStackTrace();
   
   return null;
  }
  
 } 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

product.jsp

<%
ArrayList el = (ArrayList)ProductManager.getProductList();//将查询结果传到jsp
 Iterator iter = el.iterator();
 
 while(iter.hasNext())
 {
  Product product = (Product)iter.next();
  %>
       <%=product.getProductNO()%><p>//输出查询到的结果
     <%=product.getProductIntroduce() %>
       <a href=<%="/EC/Admin/product/edit_product.jsp?product_id="+product.getId()%>>[编辑]</a>
     <a href=<%="/EC/Admin/product/delete_product.jsp?product_id="+product.getId()%> onclick="{if(confirm('确定删除吗?')){return true;}return false;}">[删除]</a>
     <%
  }
 %>