You are on page 1of 13

Series: EJB 2.x vi Netbeans Trong bi ny chng ta s c 1 v d v Stateful session bean vi yu cu l to 1 gi hang n gin.

Phn 1: To EJB
Bc 1: To EJB project

By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans

Bc 2: To Stateful Session Bean u tin ta cn 1 i tng c tn l Item ch 1 mn hang c trong gi hang. i tng ny c 2 thuc tnh l name-tn mn hang v price-gi mn hang. Gi s y cc mn hang c phn bit bng tn. Nhn phi chut ln project, chn New->Java Class.

Code ca n nh sau: package vovanhai.wordpress.com; /** * * @author VoVanHai */ import java.io.Serializable; public class Item implements Serializable { private String name; private double quantity; private double price; By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans public Item() { this("",0d,0d); } public Item(String name, double quantity, double price) { this.name = name; this.quantity = quantity; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public double getQuantity() { return quantity; } public void setQuantity(double quantity) { this.quantity = quantity; } public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Item other = (Item) obj; if ((this.name == null) ? (other.name != null) : !this.name.equalsIgnoreCase(other.name)) { return false; } return true; } public int hashCode() { int hash = 7; hash = 41 * hash +(this.name != null ? this.name.hashCode():0); return hash; } public String toString() { return name; } } Sau khi c i tng, by gi ta to Session Bean. By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans Nhn chut phi ln project va to, chn New->other.

Cu trc project sau bc ny:

By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans

M file ShoppingCartBean ln. Khai bo 1 bin ton cc nh sau: private ArrayList cart; dng lm gi cha cc mt hang mua c. Trong hm constructor default ta cp pht vng nh cho gi hang: public void ejbCreate() { cart=new ArrayList(); } By gi thm cc Business logic methods. Nhn chut phi ln ca s code, chn Insert Code.

Phng thc Add2Cart(Item item) dng thm 1 mn hng vo gi hng.

By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans

Code ca n : public void Add2Cart(Item item) { //Nu trong gi hng c mt hang cn thm th cng dn s lng if (cart.contains(item)) { Item it = (Item) cart.get(cart.indexOf(item)); it.setQuantity(it.getQuantity() + item.getQuantity()); } else { cart.add(item); } } Tng t ta thm phng thc RemoveFromCart(String itemName) dung xa 1 mn hang khi gi hng

By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans

public void RemoveFromCart(String itemName) { Item it = new Item(itemName, 0d); if (!cart.contains(it)) { return; } cart.remove(it); } Tng t hon ton cho cc phng thc sau public ArrayList GetCart() { return cart; } public double GetTotalAmount() { double total = 0; if (cart == null || cart.size() == 0) return 0; for (int i = 0; i < cart.size(); i++) { Item it=(Item)cart.get(i); total+=it.getQuantity() * it.getPrice(); } return total; }

Xem qua file ejb-jar.xml ta thy <enterprise-beans> <session> By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans <display-name>ShoppingCartSB</display-name> <ejb-name>ShoppingCartBean</ejb-name> <home>vovanhai.wordpress.com.ShoppingCartRemoteHome</home> <remote>vovanhai.wordpress.com.ShoppingCartRemote</remote> <ejb-class>vovanhai.wordpress.com.ShoppingCartBean</ejb-class> <session-type>Stateful</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> OK. Mi th ngon lnh, trin khai n thi. Bc 4: Trin khai Nhn chut phi ln project, chn Deploy. Kt qu trn JBossAS nh sau:

Phn 2: To Client
To 1 project dng Web Application, t tn l EJB21_StatefulShoppingCart_client.

By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans

Thm tham chiu n project EJB21_StatefulShoppingCart v JBossClient_Library

Thit k trang index.jsp

By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans

Code ca trang index.jsp nh sau: <%-Document : index Created on : 14-12-2009, 08:37:03 Author : VoVanHai --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@page import="vovanhai.wordpress.com.*" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h2>CHN HNG MUA</h2> <form action="Shopping"> <table> <tr> <td>Tn hng:</td> <td> <select name="tenhang"> <option value="Xoi tng">Xoi tng</option> <option value="Me dt">Me dt</option> <option value="i khng ht">i khng ht</option> <option value="Cc dm chua ngt">Cc dm chua ngt</option> <option value="Mt t n Li Thiu">Mt t n Li Thiu</option> By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans <option value="S-ri G Cng">S-ri G Cng</option> </select> </td> </tr> <tr> <td>S lng</td> <td><input name="soluong" value="0"/></td> </tr> <tr> <td>n gi</td> <td><input name="dongia" value="0"/></td> </tr> <tr> <td><input type="submit" value="Mua hng"/></td> <td><input type="reset" value="Reset"/></td> </tr> </table> </form> <h2 align="Center">GI HNG</h2> <table border="1" width="75%" align="center"> <tr> <th>STT</th> <th>Tn hng</th> <th>S lng</th> <th>n gi</th> </tr> <% java.util.ArrayList lst = (java.util.ArrayList) session.getAttribute("giohang"); if (lst != null) { for (int i = 0; i < lst.size(); i++) { Item it = (Item) lst.get(i); out.println("<tr><td>"+(i+1)+"</td><td>" +it.getName()+"</td><td>"+it.getQuantity()+"</td><td>" +it.getPrice()+"</td></tr>"); } } %> <tr> <th colspan="4" align="right"> Tng tin:<%= session.getAttribute("tongtien")%> </th> </tr> </table> </body> </html> Thit k Servlet c tn Shopping dng x l nh sau :

By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans

Hm x l nh sau :

Kt qu thc thi chng trnh :

By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans

Yu cu thm : Bn hy vit thm 1 Filter x l cho font unicode sao cho hin th ting Vit ng.

By V Vn Hi http://vovanhai.wordpress.com

You might also like