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 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64