zero_kill 2015-06-28 15:06 采纳率: 100%
浏览 4599
已采纳

如何在servlet中取到DAO中的list从而打印在jsp页面上

   public ProductBean searchinfom(String username){
          Connection conn = null;
          PreparedStatement pstmt = null;
          ResultSet rst = null;
         // ProductBean  product = new ProductBean();
          List<ProductBean> productList=new ArrayList<ProductBean>();

          try{
            conn = dataSource.getConnection();
            pstmt = conn.prepareStatement("SELECT * FROM userproduct WHERE username like ?"); 
            pstmt.setString(1,"%"+username+"%");
            rst = pstmt.executeQuery();
            while(rst.next()){  
                ProductBean product=new ProductBean();
                product.setUsername(rst.getString("username"));
                product.setProduct_id(rst.getString("product_id"));
                product.setFaddress(rst.getString("faddress"));
                product.setSaddress(rst.getString("saddress"));
                product.setWeight(rst.getDouble("weight"));
                product.setPrice(rst.getDouble("price"));
                productList.add(product);                                   
                //return product;
            }//else{
              //return  null;
            //}

         }catch(SQLException se){

             System.out.println("SQLException:"+se.getMessage());
             return null;

         }finally{
             try{
                conn.close();
             }catch(SQLException se){
             }
         }
        return null;
       }

}

public class QueryInformServlet extends HttpServlet{
       public void doPost(HttpServletRequest request,HttpServletResponse response)
                 throws ServletException,IOException{
        String username = request.getParameter("username");
        ProductDao productdao = new ProductDao();
        ProductBean product = productdao.searchinfom(username);
        ArrayList<ProductBean> productList=null;
          productList =new ArrayList<ProductBean>();
        //if(!productList.isEmpty()){

          request.getSession().setAttribute("productList",productList);

          RequestDispatcher view = request.getRequestDispatcher("/displayinform.jsp");
          view.forward(request, response);
       //}else{
       // RequestDispatcher view = request.getRequestDispatcher("/error.jsp");
       //   view.forward(request, response);
      //  }
      } 
    }

<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="java.util.* ,com.bean.ProductBean"%>

productList=(ArrayList)session.getAttribute("productList"); for(ProductBean product:productList) { %>
用户名 商品号 发货地 收货地 重量 价格


  • 写回答

1条回答

  • devmiao 2015-06-28 15:23
    关注
     <%
    for (ProductBean product:productList)
    {
        response.Write(product.get用户名());
            ...
    }
    %>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献