background image

Session Bean Class

<< The Business Interface | Life-Cycle Callback Methods >>
<< The Business Interface | Life-Cycle Callback Methods >>

Session Bean Class

Stateful session beans also may:
Implement the business interface, a plain Java interface. It is good practice to implement the
bean's business interface.
Implement any optional life cycle callback methods, annotated @PostConstruct,
@PreDestroy
, @PostActivate, and @PrePassivate.
Implement any optional business methods annotated @Remove.
The source code for the CartBean class follows.
package com.sun.tutorial.javaee.ejb;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
@Stateful
public class CartBean implements Cart {
String customerName;
String customerId;
List<String> contents;
public void initialize(String person) throws BookException {
if (person == null) {
throw new BookException(
"Null person not allowed.");
} else {
customerName = person;
}
customerId =
"0";
contents = new ArrayList<String>();
}
public void initialize(String person, String id)
throws BookException {
if (person == null) {
throw new BookException(
"Null person not allowed.");
} else {
customerName = person;
}
IdVerifier idChecker = new IdVerifier();
if (idChecker.validate(id)) {
customerId = id;
} else {
The cart Example
Chapter 22 · Session Bean Examples
659