10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Stateful session beans also may:<br />

■ Implement the business interface, a plain <strong>Java</strong> interface. It is good practice to implement the<br />

bean’s business interface.<br />

■ Implement any optional life cycle callback methods, annotated @PostConstruct,<br />

@PreDestroy, @PostActivate, and @PrePassivate.<br />

■ Implement any optional business methods annotated @Remove.<br />

<strong>The</strong> source code for the CartBean class follows.<br />

package com.sun.tutorial.javaee.ejb;<br />

import java.util.ArrayList;<br />

import java.util.List;<br />

import javax.ejb.Remove;<br />

import javax.ejb.Stateful;<br />

@Stateful<br />

public class CartBean implements Cart {<br />

String customerName;<br />

String customerId;<br />

List contents;<br />

public void initialize(String person) throws BookException {<br />

if (person == null) {<br />

throw new BookException("Null person not allowed.");<br />

} else {<br />

customerName = person;<br />

}<br />

}<br />

customerId = "0";<br />

contents = new ArrayList();<br />

public void initialize(String person, String id)<br />

throws BookException {<br />

if (person == null) {<br />

throw new BookException("Null person not allowed.");<br />

} else {<br />

}<br />

customerName = person;<br />

IdVerifier idChecker = new IdVerifier();<br />

if (idChecker.validate(id)) {<br />

customerId = id;<br />

} else {<br />

<strong>The</strong> cart Example<br />

Chapter 22 • Session Bean Examples 657

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!