2014-02-14 35 views
0
A Product class representing a real world entity of a product, it should be just a Javabean. 

public class Product { 
     private Long id; 
     private String name; 
     private String description; 
     private BigDecimal price; 

     // Add/generate getters/setters/c'tors/equals/hashcode boilerplate. 
    } 

一個ProductDao的:添加服務層的Web APP上一個jsp顯示DB內容

public class ProductDAO { 

    public List<Product> list() throws SQLException { 
     Connection connection = null; 
     PreparedStatement statement = null; 
     ResultSet resultSet = null; 
     List<Product> products = new ArrayList<Product>(); 

     try { 
      connection = database.getConnection(); 
      statement = connection.prepareStatement("SELECT id, name, description, price FROM product"); 
      resultSet = statement.executeQuery(); 

      while (resultSet.next()) { 
       Product product = new Product(); 
       product.setId(resultSet.getLong("id")); 
       product.setName(resultSet.getString("name")); 
       product.setDescription(resultSet.getString("description")); 
       product.setPrice(resultSet.getBigDecimal("price")); 
       products.add(product); 
      } 
     } finally { 
      if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) {} 
      if (statement != null) try { statement.close(); } catch (SQLException ignore) {} 
      if (connection != null) try { connection.close(); } catch (SQLException ignore) {} 
     } 

     return products; 
    } 

} 

Servlet來獲得列表:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    try { 
     List<Product> products = productDAO.list(); 
     request.setAttribute("products", products); // Will be available as ${products} in JSP 
     request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response); 
    } catch (SQLException e) { 
     throw new ServletException("Cannot obtain products from DB", e); 
    } 
} 

最後在jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
... 
<table> 
    <c:forEach items="${products}" var="product"> 
     <tr> 
      <td>${product.name}</td> 
      <td>${product.description}</td> 
      <td>${product.price}</td> 
     </tr> 
    </c:forEach> 
</table> 

夥計們我從Stackoverflow本身得到了這段代碼....它的類似t o我的項目,我正在...我只想知道如果我想要添加服務層到這個應用程序我必須做什麼改變...我試圖把列表放在另一個方法,但它給了一些錯誤...所以一些一個請指導我這個....應該有一個方法從servlet到服務,然後從服務到DAO。請指導我.....

回答

1

您必須創建一個新的服務類是這樣的:

public class ProductService { 
    ProductDAO productDAO = new ProductDAO(); 

    public List<Product> list() throws SQLException{ 
     return productDAO.list(); 
    } 
} 

到位DAO實例到你的servlet添加服務實例是這樣的:

ProductService productService = new ProductService(); 

然後修改您的doGet,而不是使用DAO的服務:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     try { 
      List<Product> products = productService.list(); 
      request.setAttribute("products", products); // Will be available as ${products} in JSP 
      request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response); 

     } catch (SQLException e) { 
      throw new ServletException("Cannot obtain products from DB", e); 
     } 
} 

這是一個樣本讓你開始。您應該將SQLException處理移至服務類而不是servlet。

+0

謝謝soooo答案.....我做到了這一點,它的工作....謝謝你的指導..... – user3222718

相關問題