+ All Categories
Home > Documents > Bai 03 - Stateful Session Bean EJB2.1 - Shopping Cart

Bai 03 - Stateful Session Bean EJB2.1 - Shopping Cart

Date post: 05-Apr-2018
Category:
Upload: hailuagiao
View: 215 times
Download: 0 times
Share this document with a friend

of 13

Transcript
  • 7/31/2019 Bai 03 - Stateful Session Bean EJB2.1 - Shopping Cart

    1/13

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    Trong bi ny chng ta sc 1 v d v Stateful session bean vi yu cu l to 1 gihang ngin.

    Phn 1: To EJB

    Bc 1: To EJB project

  • 7/31/2019 Bai 03 - Stateful Session Bean EJB2.1 - Shopping Cart

    2/13

  • 7/31/2019 Bai 03 - Stateful Session Bean EJB2.1 - Shopping Cart

    3/13

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    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.

  • 7/31/2019 Bai 03 - Stateful Session Bean EJB2.1 - Shopping Cart

    4/13

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    Nhn chut phi ln project va to, chn New->other.

    Cu trc project sau bc ny:

  • 7/31/2019 Bai 03 - Stateful Session Bean EJB2.1 - Shopping Cart

    5/13

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    Mfile 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 vngnh cho gi hang:

    public void ejbCreate() {

    cart=new ArrayList();

    }

    By githm 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 gihng.

  • 7/31/2019 Bai 03 - Stateful Session Bean EJB2.1 - Shopping Cart

    6/13

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    Code ca n :

    public void Add2Cart(Item item) {

    //Nu trong gihng c mt hang cn thm th cng dn slngif (cart.contains(item)) {

    Item it = (Item) cart.get(cart.indexOf(item));

    it.setQuantity(it.getQuantity() + item.getQuantity());

    } else {

    cart.add(item);

    }

    }

    Tng tta thm phng thc RemoveFromCart(String itemName) dung xa 1 mn hang khigihng

  • 7/31/2019 Bai 03 - Stateful Session Bean EJB2.1 - Shopping Cart

    7/13

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    public void RemoveFromCart(String itemName) {

    Item it = new Item(itemName, 0d);

    if (!cart.contains(it)) {return;

    }

    cart.remove(it);

    }

    Tng thon 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

  • 7/31/2019 Bai 03 - Stateful Session Bean EJB2.1 - Shopping Cart

    8/13

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    ShoppingCartSBShoppingCartBeanvovanhai.wordpress.com.ShoppingCartRemoteHomevovanhai.wordpress.com.ShoppingCartRemotevovanhai.wordpress.com.ShoppingCartBeanStatefulContainer

    OK. Mi thngon lnh, trin khai n thi.

    Bc 4: Trin khai

    Nhn chut phi ln project, chn Deploy. Kt qutrn JBossAS nh sau:

    Phn 2: To Client

    To 1 project dng Web Application, t tn l EJB21_StatefulShoppingCart_client .

  • 7/31/2019 Bai 03 - Stateful Session Bean EJB2.1 - Shopping Cart

    9/13

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    Thm tham chiu n project EJB21_StatefulShoppingCart v JBossClient_Library

    Thit k trang index.jsp

  • 7/31/2019 Bai 03 - Stateful Session Bean EJB2.1 - Shopping Cart

    10/13

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    Code ca trang index.jsp nh sau:

    JSP Page

    CHN HNG MUA

    Tn hng:

    Xoi tng

    Me dti khng htCc dm chua ngtMt t nLi Thiu

  • 7/31/2019 Bai 03 - Stateful Session Bean EJB2.1 - Shopping Cart

    11/13

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    S-ri G Cng

    S lng

    n gi

    GIHNG

    STTTn hngSlngn gi

    Tng tin:

    Thit k Servlet c tn Shopping dng xlnh sau :

  • 7/31/2019 Bai 03 - Stateful Session Bean EJB2.1 - Shopping Cart

    12/13

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    Hm xl nh sau :

    Kt qu thc thi chng trnh :

  • 7/31/2019 Bai 03 - Stateful Session Bean EJB2.1 - Shopping Cart

    13/13

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    Yu cu thm :

    Bn hy vit thm 1 Filter xl cho font unicode sao cho hin th ting Vit ng.


Recommended