程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> dao-如何在servlet中取到DAO中的list從而打印在jsp頁面上

dao-如何在servlet中取到DAO中的list從而打印在jsp頁面上

編輯:編程綜合問答
如何在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) { %>


最佳回答:


 <%
for (ProductBean product:productList)
{
    response.Write(product.get用戶名());
        ...
}
%>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved