Advanced Java Programming

Advanced Programming for the Java(TM) 2 Platform

Tra in in g In d e x

Ad v a n c e d P ro g ra m m in g fo r t h e
Ja v a 2 P la t fo rm
TM

By Ca lv in Au s t in a n d Mo n ic a P a w la n
No v e m b e r 1 9 9 9
[ CONTENTS ] [ NEXT> > ]

[ DOWNLOAD]
Re q u ire s lo g in

Ea rly Ac c e s s
D o w n lo a d s

Bu g D a t a b a s e
S u b m it a Bu g
Vie w D a t a b a s e


N e w s le t t e rs
Ba c k I s s u e s
S u b s c rib e

Le a rn in g Ce n t e rs
Art ic le s
Bo o k s h e lf
Co d e S a m p le s
N e w t o Ja v a
Qu e s t io n o f t h e W e e k
Qu iz z e s
Te c h Tip s
Tu t o ria ls

Fo ru m s

As a n e xp e rie n ce d d e ve lo p e r o n t h e Ja va TM p la t fo rm , yo u
u n d o u b t e d ly kn o w h o w fa s t m o vin g a n d co m p re h e n s ive t h e
p la t fo rm is . It s m a n y a p p lica t io n p ro g ra m m in g in t e rfa ce s ( APIs )

p ro vid e a we a lt h o f fu n ct io n a lit y fo r a ll a s p e ct s o f a p p lica t io n a n d
s ys t e m - le ve l p ro g ra m m in g . Re a l- wo rld d e ve lo p e rs n e ve r u s e o n e
o r t wo APIs t o s o lve a p ro b le m , b u t b rin g t o g e t h e r ke y
fu n ct io n a lit y s p a n n in g a n u m b e r o f APIs . Kn o win g wh ich APIs yo u
n e e d , wh ich p a rt s o f wh ich APIs yo u n e e d , a n d h o w t h e APIs wo rk
t o g e t h e r t o cre a t e t h e b e s t s o lu t io n ca n b e a d a u n t in g t a s k.
To h e lp yo u n a vig a t e t h e Ja va APIs a n d fa s t - t ra ck yo u r p ro je ct
d e ve lo p m e n t t im e , t h is b o o k in clu d e s t h e d e s ig n , d e ve lo p m e n t ,
t e s t , a n d d e p lo ym e n t p h a s e s fo r a n e n t e rp ris e - wo rt h y a u ct io n
a p p lica t io n . Wh ile t h e e xa m p le a p p lica t io n d o e s n o t co ve r e ve ry
p o s s ib le p ro g ra m m in g s ce n a rio , it e xp lo re s m a n y co m m o n
s it u a t io n s a n d t h e d is cu s s io n s le a ve yo u wit h a s o lid m e t h o d o lo g y
fo r d e s ig n in g a n d b u ild in g yo u r o wn s o lu t io n s .
Th is b o o k is fo r d e ve lo p e rs wit h m o re t h a n a b e g in n in g le ve l o f
u n d e rs t a n d in g o f writ in g p ro g ra m s in t h e Ja va p ro g ra m m in g
la n g u a g e . Th e e xa m p le a p p lica t io n is writ t e n wit h t h e Ja va ® 2
p la t fo rm APIs a n d e xp la in e d in t e rm s o f fu n ct io n a l h o ws a n d wh ys ,
s o if yo u n e e d h e lp in s t a llin g t h e Ja va p la t fo rm , s e t t in g u p yo u r
e n viro n m e n t , o r g e t t in g yo u r firs t a p p lica t io n t o wo rk, yo u s h o u ld
firs t re a d a m o re in t ro d u ct o ry b o o k s u ch a s Es s e n t ia ls o f t h e

Ja v a P ro g ra m m in g La n g u a g e : A Ha n d s - On Gu id e o r Th e Ja v a
Tu t o ria l.

http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/ (1 of 4) [2001-6-13 8:07:37]

Advanced Programming for the Java(TM) 2 Platform

N o t e : Th is
t u t o ria l is
a va ila b le a s a
b o o k fro m o n lin e
b o o k s e lle rs Als o ,
s e n d yo u r
co m m e n t s a n d
thoughts to
jd cb o o k@s u n . co m

Te c h n o lo g y Ce n t e rs
SELECT


Co n t e n t s
Ch a p t e r 1 : Ma t ch in g Pro je ct Re q u ire m e n t s wit h Te ch n o lo g y

Advanced Programming for the Java(TM) 2 Platform

S p e cia l t h a n ks t o e xp e rt s Is a a c Elia s , Da n ie l Liu , a n d Ma rk
Ho rwa t h fo r t h e ir co n t rib u t io n s t o t h e a d va n ce d e xa m p le s in t h e
b o o k.

Re a d e r Fe e d b a c k
Te ll u s wh a t yo u t h in k o f t h is b o o k.
[ TOP]

[ Th is p a g e wa s u p d a t e d : 4 - Ju n - 2 0 0 1 ]
Pro d u ct s & APIs | De ve lo p e r Co n n e ct io n | Do cs & Tra in in g | On lin e S u p p o rt
Co m m u n it y Dis cu s s io n | In d u s t ry Ne ws | S o lu t io n s Ma rke t p la ce | Ca s e S t u d ie s
Glo s s a ry | Fe e d b a ck | A- Z In d e x
Fo r m o re in fo rm a t io n o n Ja va t e ch n o lo g y
a n d o t h e r s o ft wa re fro m S u n Micro s ys t e m s , ca ll:


(800) 786-7638
Ou t s id e t h e U. S . a n d Ca n a d a , d ia l yo u r co u n t ry's
AT&T Dire ct Acce s s Nu m b e r firs t .

Co p yrig h t © 1 9 9 5 - 2 0 0 1 S u n Micro s ys t e m s , In c.
All Rig h t s Re s e rve d . Te rm s o f Us e . Priva cy Po licy.

http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/ (4 of 4) [2001-6-13 8:07:37]

Writing Advanced Applications, Chapter 1: Matching Project Requirements with Technology

Tra in in g In d e x

W rit in g Ad v a n c e d Ap p lic a t io n s
Ch a p t e r 1 : Ma t c h in g P ro je c t Re q u ire m e n t s w it h Te c h n o lo g y
[ < < BACK] [ CONTENTS ] [ NEXT> > ]

On e ch a lle n g e in writ in g a b o o k o n a d va n ce d a p p lica t io n
d e ve lo p m e n t fo r t h e Ja va TM p la t fo rm is t o fin d a p ro je ct s m a ll
e n o u g h t o writ e a b o u t , wh ile a t t h e s a m e t im e , co m p le x e n o u g h t o

wa rra n t a d va n ce d p ro g ra m m in g t e ch n iq u e s .
Re q u ire s lo g in

Ea rly Ac c e s s
D o w n lo a d s

Bu g D a t a b a s e
S u b m it a Bu g
Vie w D a t a b a s e

N e w s le t t e rs
Ba c k I s s u e s
S u b s c rib e

Le a rn in g Ce n t e rs
Art ic le s
Bo o k s h e lf
Co d e S a m p le s
N e w t o Ja v a
Qu e s t io n o f t h e W e e k

Qu iz z e s
Te c h Tip s
Tu t o ria ls

Fo ru m s

Th e p ro je ct p re s e n t e d in t h is b o o k is a we b - b a s e d a u ct io n h o u s e .
Th e a p p lica t io n is in it ia lly writ t e n fo r t h e En t e rp ris e Ja va Be a n s TM
p la t fo rm . La t e r ch a p t e rs e xp a n d t h e co re e xa m p le d e s crib e d h e re
b y a d d in g a d va n ce d fu n ct io n a lit y, im p ro ve m e n t s , a n d a lt e rn a t ive
s o lu t io n s t o d o s o m e o f t h e t h in g s yo u g e t fo r fre e wh e n yo u u s e
t h e En t e rp ris e Ja va Be a n s p la t fo rm .
To ke e p t h e d is cu s s io n s im p le , t h e e xa m p le a p p lica t io n h a s o n ly a
b a s ic s e t o f t ra n s a ct io n s fo r p o s t in g a n d b id d in g o n a u ct io n it e m s .
Ho we ve r, t h e a p p lica t io n s ca le s t o h a n d le m u lt ip le u s e rs , p ro vid e s
a t h re e - t ie re d t ra n s a ct io n - b a s e d e n viro n m e n t , co n t ro ls s e cu rit y,
a n d in t e g ra t e s le g a cy- b a s e d s ys t e m s . Th is ch a p t e r co ve rs h o w t o
d e t e rm in e p ro je ct re q u ire m e n t s a n d m o d e l t h e
a p p lica t io n —im p o rt a n t s t e p s t h a t s h o u ld a lwa ys co m e b e fo re
co d in g b e g in s .


Writing Advanced Applications, Chapter 1: Matching Project Requirements with Technology

Te c h n o lo g y Ce n t e rs
SELECT

Pro je ct Re q u ire m e n t s

In t e rvie w Us e r Ba s e
Mo d e l t h e Pro je ct

Mo d e lin g

Ho u s e Id e n t ifie s Bu ye rs a n d S e lle rs
Ho u s e De t e rm in e s Hig h e s t Bid d e r
Ho u s e No t ifie s Bu ye rs a n d S e lle rs
An yo n e S e a rch e s fo r a n It e m
An yo n e Vie ws It e m s fo r S a le
An yo n e Vie ws It e m De t a ils
S e lle r Po s t s It e m s fo r S a le

Bu ye r Bid s o n It e m s
Act ivit y Dia g ra m

Ch o o s in g S o ft wa re

Ja va TM APIs
[ TOP]

[ Th is p a g e wa s u p d a t e d : 4 - Ju n - 2 0 0 1 ]
Pro d u ct s & APIs | De ve lo p e r Co n n e ct io n | Do cs & Tra in in g | On lin e S u p p o rt
Co m m u n it y Dis cu s s io n | In d u s t ry Ne ws | S o lu t io n s Ma rke t p la ce | Ca s e S t u d ie s
Glo s s a ry | Fe e d b a ck | A- Z In d e x
Fo r m o re in fo rm a t io n o n Ja va t e ch n o lo g y
a n d o t h e r s o ft wa re fro m S u n Micro s ys t e m s , ca ll:

(800) 786-7638
Ou t s id e t h e U. S . a n d Ca n a d a , d ia l yo u r co u n t ry's
AT&T Dire ct Acce s s Nu m b e r firs t .

Co p yrig h t © 1 9 9 5 - 2 0 0 1 S u n Micro s ys t e m s , In c.

All Rig h t s Re s e rve d . Te rm s o f Us e . Priva cy Po licy.

http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/proj.html (2 of 2) [2001-6-13 8:07:50]

Writing Advanced Applications, Chapter 1: Project Requirements and Modeling

Tra in in g In d e x

W rit in g Ad v a n c e d Ap p lic a t io n s
Ch a p t e r 1 Co n t in u e d : P ro je c t Re q u ire m e n t s a n d Mo d e lin g
[ < < BACK] [ CONTENTS ] [ NEXT> > ]

Th e firs t s t e p in d e t e rm in in g p ro je ct re q u ire m e n t s is t o in t e rvie w
t h e u s e r b a s e t o fin d o u t wh a t t h e y wa n t in a n o n lin e a u ct io n . Th is
is a n im p o rt a n t s t e p , a n d o n e t h a t ca n n o t b e o ve rra t e d b e ca u s e a
s o lid b a s e o f u s e r- o rie n t e d in fo rm a t io n h e lp s yo u d e fin e yo u r ke y
a p p lica t io n ca p a b ilit ie s .
Re q u ire s lo g in

Ea rly Ac c e s s

D o w n lo a d s

Bu g D a t a b a s e
S u b m it a Bu g
Vie w D a t a b a s e

N e w s le t t e rs
Ba c k I s s u e s
S u b s c rib e

Le a rn in g Ce n t e rs
Art ic le s
Bo o k s h e lf
Co d e S a m p le s
N e w t o Ja v a
Qu e s t io n o f t h e W e e k
Qu iz z e s
Te c h Tip s
Tu t o ria ls

Fo ru m s

Ch a p t e r 2 wa lks t h ro u g h t h e a p p lica t io n co d e , e xp la in s h o w t h e
En t e rp ris e Ja va Be a n s p la t fo rm wo rks , a n d t e lls yo u h o w t o ru n a
live d e m o n s t ra t io n . If yo u h a ve n e ve r s e e n o r u s e d a n o n lin e
a u ct io n , h e re a re m o cku p s o f t h e e xa m p le a u ct io n a p p lica t io n HTML
pages.

Writing Advanced Applications, Chapter 1: Project Requirements and Modeling

Te c h n o lo g y Ce n t e rs Aft e r a n a lyzin g t h e re q u ire m e n t s , yo u ca n b u ild a u s e ca s e
SELECT
d ia g ra m fo r t h e a p p lica t io n t o g a in a b e t t e r u n d e rs t a n d in g o f t h e
e le m e n t s n e e d e d in t h e a p p lica t io n a n d h o w t h e y in t e ra ct .
A u s e ca s e d ia g ra m s h o ws t h e re la t io n s h ip s a m o n g a ct o rs a n d u s e
ca s e s wit h in t h e s ys t e m . A u s e ca s e is a u n iq u e fu n ct io n in a
s ys t e m , a n d a n a ct o r is t h e p e rs o n o r s o ft wa re t h a t p e rfo rm s t h e
a ct io n o r u s e ca s e . Fo r e xa m p le , a b u ye r is t h e a ct o r t h a t p e rfo rm s
t h e fu n ct io n ( u s e ca s e ) o f b id d in g o n a n a u ct io n it e m , a n d t h e
s e lle r is t h e a ct o r t h a t p e rfo rm s t h e u s e ca s e o f p o s t in g a n it e m fo r
a u ct io n .
No t a ll a ct o rs a re p e o p le , t h o u g h . Fo r e xa m p le , t h e s o ft wa re is t h e
a ct o r t h a t d e t e rm in e s wh e n a n it e m h a s clo s e d , fin d s t h e h ig h e s t
b id d e r, a n d n o t ifie s t h e b u ye r a n d s e lle r o f t h e s a le .
Th e Un ifie d Mo d e lin g La n g u a g e ( UML) is t h e t o o l o f ch o ice fo r
cre a t in g u s e ca s e d ia g ra m s . Th e Us e Ca s e d ia g ra m b e lo w u s e s
UML t o d e s crib e t h e b u ye r a n d s e lle r u s e ca s e s fo r t h e o n lin e
a u ct io n a p p lica t io n .
In UML, s ys t e m s a re g ro u p e d in t o s q u a re s , a ct o rs a re re p re s e n t e d
b y s t ick fig u re s , u s e ca s e s a re d e n o t e d b y o va ls , a n d t h e lin e s
s h o w h o w a ct o rs u s e t h e s ys t e m .

http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/proj2.html (2 of 6) [2001-6-13 8:08:07]

Th e fo llo win g d e s crip t io n s fu rt h e r d e fin e t h e p ro je ct . Th e s e
d e s crip t io n s a re n o t p a rt o f UML, b u t a re a h e lp fu l t o o l in p ro je ct
d e fin it io n .
Ho u s e I d e n t ifie s Bu y e rs a n d S e lle rs
An a u ct io n a p p lica t io n is u s e d b y b u ye rs a n d s e lle rs . A b u ye r n e e d s
t o kn o w wh o t h e s e lle r is t o p a y h im o r h e r, a n d t h e s e lle r n e e d s t o
kn o w wh o t h e b u ye rs a re t o a n s we r p ro d u ct q u e s t io n s a n d t o
fin a lize t h e s a le . S o , t o p o s t o r b id o n a n a u ct io n it e m , b u ye rs a n d
s e lle rs a re re q u ire d t o re g is t e r. Re g is t ra t io n n e e d s t o g e t t h e
fo llo win g in fo rm a t io n fro m b u ye rs a n d s e lle rs :

S e lle r P o s t s I t e m s fo r S a le
To p o s t a n it e m fo r s a le , a s e lle r n e e d s t o id e n t ify h im s e lf o r
h e rs e lf a n d d e s crib e t h e it e m fo r s a le , a s fo llo ws :

Writing Advanced Applications, Chapter 1: Project Requirements and Modeling

[ TOP]

[ Th is p a g e wa s u p d a t e d : 4 - Ju n - 2 0 0 1 ]
Pro d u ct s & APIs | De ve lo p e r Co n n e ct io n | Do cs & Tra in in g | On lin e S u p p o rt
Co m m u n it y Dis cu s s io n | In d u s t ry Ne ws | S o lu t io n s Ma rke t p la ce | Ca s e S t u d ie s
Glo s s a ry | Fe e d b a ck | A- Z In d e x
Fo r m o re in fo rm a t io n o n Ja va t e ch n o lo g y
a n d o t h e r s o ft wa re fro m S u n Micro s ys t e m s , ca ll:

(800) 786-7638
Ou t s id e t h e U. S . a n d Ca n a d a , d ia l yo u r co u n t ry's
AT&T Dire ct Acce s s Nu m b e r firs t .

Co p yrig h t © 1 9 9 5 - 2 0 0 1 S u n Micro s ys t e m s , In c.
All Rig h t s Re s e rve d . Te rm s o f Us e . Priva cy Po licy.

http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/proj2.html (6 of 6) [2001-6-13 8:08:07]

Writing Advanced Applications, Chapter 1: Choosing the Software

Tra in in g In d e x

W rit in g Ad v a n c e d Ap p lic a t io n s
Ch a p t e r 1 Co n t in u e d : Ch o o s in g t h e S o ft w a re
[ < < BACK] [ CONTENTS ] [ NEXT> > ]

Re q u ire s lo g in

Wit h t h e a p p lica t io n m o d e le d a n d t h e p ro je ct re q u ire m e n t s
d e fin e d , it is t im e t o t h in k a b o u t wh ich Ja va TM APIs t o u s e . Th e
a p p lica t io n is cle a rly clie n t a n d s e rve r b a s e d b e ca u s e yo u will wa n t
t o a cco m m o d a t e 1 t o n b u ye rs , s e lle rs , a n d vie we rs a t a n y o n e
t im e . Be ca u s e re g is t ra t io n a n d a u ct io n it e m d a t a m u s t b e s t o re d
a n d re t rie ve d fro m s o m e wh e re , yo u will n e e d a n API fo r d a t a b a s e
a cce s s .

Ea rly Ac c e s s
D o w n lo a d s

Ja v a TM AP I s

Bu g D a t a b a s e
S u b m it a Bu g
Vie w D a t a b a s e

Th e co re a p p lica t io n ca n b e cre a t e d in a n u m b e r o f wa ys u s in g a n y
o f t h e fo llo win g APIs :

N e w s le t t e rs

1 . S o cke t s , m u lt it h re a d in g , a n d JDBC TM APIs .
2 . Re m o t e Me t h o d In vo ca t io n ( RMI) a n d JDBC APIs .
3 . En t e rp ris e Ja va Be a n s TM p la t fo rm .
En t e rp ris e Ja va Be a n s p ro vid e s a n e a s y wa y t o cre a t e t h in - clie n t
m u lt it ie re d a p p lica t io n s b e ca u s e it h a n d le s t ra n s a ct io n a n d s t a t e
m a n a g e m e n t , m u lt it h re a d in g , re s o u rce p o o lin g , a n d o t h e r co m p le x
lo w- le ve l d e t a ils . Th e s im p le s t wa y t o co d e t h e a u ct io n a p p lica t io n
is wit h t h e En t e rp ris e Ja va Be a n s p la t fo rm .

Ba c k I s s u e s
S u b s c rib e

Le a rn in g Ce n t e rs
Art ic le s
Bo o k s h e lf
Co d e S a m p le s
N e w t o Ja v a
Qu e s t io n o f t h e W e e k
Qu iz z e s
Te c h Tip s
Tu t o ria ls

Fo ru m s

Ch a p t e r 2 e xp la in s t h e co re a p p lica t io n co d e a n d h o w t o s e t u p a n d
ru n t h e e xa m p le . Wit h t h e a p p lica t io n m o d e le d a n d t h e p ro je ct
re q u ire m e n t s d e fin e d , it is t im e t o t h in k a b o u t wh ich Ja va TM APIs t o
u s e . Th e a p p lica t io n is cle a rly clie n t a n d s e rve r b a s e d b e ca u s e yo u
will wa n t t o a cco m m o d a t e 1 t o n b u ye rs , s e lle rs , a n d vie we rs a t
a n y o n e t im e . Be ca u s e re g is t ra t io n a n d a u ct io n it e m d a t a m u s t b e
s t o re d a n d re t rie ve d fro m s o m e wh e re , yo u will n e e d a n API fo r
d a t a b a s e a cce s s .
[ TOP]

http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/proj3.html (1 of 2) [2001-6-13 8:08:15]

Writing Advanced Applications, Chapter 1: Choosing the Software

Te c h n o lo g y Ce n t e rs
SELECT

[ Th is p a g e wa s u p d a t e d : 4 - Ju n - 2 0 0 1 ]
Pro d u ct s & APIs | De ve lo p e r Co n n e ct io n | Do cs & Tra in in g | On lin e S u p p o rt
Co m m u n it y Dis cu s s io n | In d u s t ry Ne ws | S o lu t io n s Ma rke t p la ce | Ca s e S t u d ie s
Glo s s a ry | Fe e d b a ck | A- Z In d e x
Fo r m o re in fo rm a t io n o n Ja va t e ch n o lo g y
a n d o t h e r s o ft wa re fro m S u n Micro s ys t e m s , ca ll:

(800) 786-7638
Ou t s id e t h e U. S . a n d Ca n a d a , d ia l yo u r co u n t ry's
AT&T Dire ct Acce s s Nu m b e r firs t .

Co p yrig h t © 1 9 9 5 - 2 0 0 1 S u n Micro s ys t e m s , In c.
All Rig h t s Re s e rve d . Te rm s o f Us e . Priva cy Po licy.

http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/proj3.html (2 of 2) [2001-6-13 8:08:15]

Writing Advanced Applications, Chapter 2: Auction Application Code

Tra in in g In d e x

W rit in g Ad v a n c e d Ap p lic a t io n s
Ch a p t e r 2 : Au c t io n Ho u s e Ap p lic a t io n
[ < < BACK] [ CONTENTS ] [ NEXT> > ]

Re q u ire s lo g in

Th e e xa m p le a p p lica t io n is a we b - b a s e d a u ct io n h o u s e writ t e n fo r
t h e En t e rp ris e Ja va Be a n s TM p la t fo rm . Th e u s e r in t e rfa ce is a s e t o f
HTML p a g e s t h a t g e t in p u t fro m a n d s h o w in fo rm a t io n t o t h e u s e r.
Be h in d t h e HTML p a g e s is a s e rvle t t h a t p a s s e s d a t a b e t we e n t h e
b ro ws e r a n d t h e En t e rp ris e Ja va Be a n s s e rve r. Th e En t e rp ris e
Ja va Be a n s s e rve r h a n d le s re a d in g fro m a n d writ in g t o t h e
da ta ba se .

Ea rly Ac c e s s
D o w n lo a d s

Bu g D a t a b a s e
S u b m it a Bu g
Vie w D a t a b a s e

N e w s le t t e rs
Ba c k I s s u e s
S u b s c rib e

Le a rn in g Ce n t e rs
Art ic le s
Bo o k s h e lf
Co d e S a m p le s
N e w t o Ja v a
Qu e s t io n o f t h e W e e k
Qu iz z e s
Te c h Tip s
Tu t o ria ls

Fo ru m s

Th is ch a p t e r d e s crib e s t h e a p p lica t io n co d e , h o w it wo rks wit h t h e
En t e rp ris e Ja va Be a n s s e rve r, a n d wh e re t o g e t a En t e rp ris e
Ja va Be a n s s e rve r t o ru n t h e e xa m p le . Or, if yo u p re fe r, h e re is a n
e xa m p le m o cku p fo r t h e a u ct io n a p p lica t io n .

Writing Advanced Applications, Chapter 2: Auction Application Code

Te c h n o lo g y Ce n t e rs
SELECT

A Mu lt i- Tie re d
Ap p lica t io n s wit h
En t e rp ris e Be a n s

En t e rp ris e Be a n s De fin e d
En t it y a n d S e s s io n Be a n s
Au ct io n Ho u s e Wo rkin g s
De ve lo p in g a n d Ru n n in g Ap p lica t io n s
Ho w Mu lt it ie re d Ap p lica t io n s Wo rk

En t it y a n d S e s s io n
Be a n s

Au ct io n S e rvle t
En t it y Be a n s
S e s s io n Be a n s
Co n t a in e r Cla s s e s

Exa m in in g a Co n t a in e r- Me m b e r Va ria b le s
Ma n a g e d Be a n
Cre a t e Me t h o d
En t it y Co n t e xt Me t h o d s
Lo a d Me t h o d
S t o re Me t h o d
Co n n e ct io n Po o lin g
De p lo ym e n t De s crip t o r
Co n t a in e r- Ma n a g e d
fin d e r Me t h o d s

Au ct io n S e rvle t . s e a rch It e m s
Bid d e rBe a n . g e t Ma t ch in g It e m s Lis t
Au ct io n It e m Ho m e . fin d AllMa t ch in g It e m s
Au ct io n It e m Be a n De p lo ym e n t De s crip t o r
[ TOP]

[ Th is p a g e wa s u p d a t e d : 4 - Ju n - 2 0 0 1 ]
Pro d u ct s & APIs | De ve lo p e r Co n n e ct io n | Do cs & Tra in in g | On lin e S u p p o rt
Co m m u n it y Dis cu s s io n | In d u s t ry Ne ws | S o lu t io n s Ma rke t p la ce | Ca s e S t u d ie s
Glo s s a ry | Fe e d b a ck | A- Z In d e x
Fo r m o re in fo rm a t io n o n Ja va t e ch n o lo g y
a n d o t h e r s o ft wa re fro m S u n Micro s ys t e m s , ca ll:

(800) 786-7638
Ou t s id e t h e U. S . a n d Ca n a d a , d ia l yo u r co u n t ry's
AT&T Dire ct Acce s s Nu m b e r firs t .

Co p yrig h t © 1 9 9 5 - 2 0 0 1 S u n Micro s ys t e m s , In c.
All Rig h t s Re s e rve d . Te rm s o f Us e . Priva cy Po licy.

http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/code.html (2 of 2) [2001-6-13 8:08:55]

Duke's Auction

Duke's Auction
Need to clean out that old office, garage, or
closet? or looking for something so unique you
cannot find it anywhere—or at least not at a price
you are willing to pay?
Look no further. At Duke's Auction you can post
items for sale and bid what you want to pay for
the items you want.
Registration
To bid on or list an item for auction, you must
register first. Registration gives buyers a way to
pay you and us a way to contact buyers and
sellers. You only need register once, and
registration is not required to browse items on
the auction floor.
Auction Floor
The auction floor is open to anyone for browsing, but to bid on an item, you must be
registered.
● New auction items today
● Items closing today
● All items (current and closed)
● Search for Items
Post Items for Auction
Once you register, you can post items for sale at auction any time you want.

Register | New Items | Closing Items | All Items | Sell Items

http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/Auction/index.html [2001-6-13 8:09:03]

Writing Advanced Applications, Chapter 2: A Multi-Tiered Application with Enterprise Beans

Tra in in g In d e x

W rit in g Ad v a n c e d Ap p lic a t io n s
Ch a p t e r 2 Co n t in u e d : A Mu lt i- Tie re d Ap p lic a t io n w it h
En t e rp ris e Be a n s
[ < < BACK] [ CONTENTS ] [ NEXT> > ]

Re q u ire s lo g in

Th e p ro life ra t io n o f in t e rn e t - a n d in t ra n e t - b a s e d a p p lica t io n s h a s
cre a t e d a g re a t n e e d fo r d is t rib u t e d t ra n s a ct io n a l a p p lica t io n s t h a t
le ve ra g e t h e s p e e d , s e cu rit y, a n d re lia b ilit y o f s e rve r- s id e
t e ch n o lo g y. On e wa y t o m e e t t h is n e e d is t o u s e a m u lt it ie re d
m o d e l wh e re a t h in - clie n t a p p lica t io n in vo ke s b u s in e s s lo g ic t h a t
e xe cu t e s o n t h e s e rve r.

Ea rly Ac c e s s
D o w n lo a d s

Bu g D a t a b a s e
S u b m it a Bu g
Vie w D a t a b a s e

No rm a lly, t h in - clie n t m u lt it ie re d a p p lica t io n s a re h a rd t o writ e
b e ca u s e t h e y in vo lve m a n y lin e s o f in t rica t e co d e t o h a n d le
t ra n s a ct io n a n d s t a t e m a n a g e m e n t , m u lt it h re a d in g , re s o u rce
p o o lin g , a n d o t h e r co m p le x lo w- le ve l d e t a ils . An d t o co m p o u n d t h e
d ifficu lt ie s , yo u h a ve t o re wo rk t h is co d e e ve ry t im e yo u writ e a n
a p p lica t io n b e ca u s e t h e co d e is s o lo w- le ve l it is n o t re u s a b le .

N e w s le t t e rs
Ba c k I s s u e s
S u b s c rib e

Le a rn in g Ce n t e rs
Art ic le s
Bo o k s h e lf
Co d e S a m p le s
N e w t o Ja v a
Qu e s t io n o f t h e W e e k
Qu iz z e s
Te c h Tip s
Tu t o ria ls

Fo ru m s

If yo u co u ld u s e s o m e o n e 's p re b u ilt a n d p re t e s t e d t ra n s a ct io n
m a n a g e m e n t co d e o r e ve n re u s e s o m e o f yo u r o wn co d e , yo u
wo u ld s a ve a lo t o f t im e a n e n e rg y t h a t yo u co u ld b e t t e r s p e n d
s o lvin g t h e b u s in e s s p ro b le m . We ll, En t e rp ris e Ja va Be a n s TM
t e ch n o lo g y ca n g ive yo u t h e h e lp yo u n e e d . Th e En t e rp ris e
Ja va Be a n s t e ch n o lo g y m a ke s d is t rib u t e d t ra n s a ct io n a l a p p lica t io n s
e a s y t o writ e b e ca u s e it s e p a ra t e s t h e lo w- le ve l d e t a ils fro m t h e
b u s in e s s lo g ic. Yo u co n ce n t ra t e o n cre a t in g t h e b e s t b u s in e s s
s o lu t io n a n d le a ve t h e re s t t o t h e u n d e rlyin g a rch it e ct u re .
Th is ch a p t e r d e s crib e s h o w t o cre a t e t h e e xa m p le a u ct io n
a p p lica t io n u s in g t h e s e rvice s p ro vid e d b y t h e En t e rp ris e
Ja va Be a n s p la t fo rm . La t e r ch a p t e rs will s h o w h o w yo u ca n
cu s t o m ize t h e s e s e rvice s a n d in t e g ra t e t h e s e fe a t u re s in t o e xis t in g
n o n - EJB a p p lica t io n s .

Writing Advanced Applications, Chapter 2: A Multi-Tiered Application with Enterprise Beans

Te c h n o lo g y Ce n t e rs
SELECT

Writing Advanced Applications, Chapter 2: A Multi-Tiered Application with Enterprise Beans

Mu lt it ie re d Arc h it e c t u re
Mu lt it ie r a rch it e ct u re o r t h re e - t ie r a rch it e ct u re
e xt e n d s t h e s t a n d a rd t wo - t ie r clie n t a n d s e rve r
m o d e l b y p la cin g a m u lt it h re a d e d a p p lica t io n
s e rve r b e t we e n t h e clie n t a n d t h e d a t a b a s e .
Clie n t p ro g ra m s co m m u n ica t e wit h t h e d a t a b a s e
t h ro u g h t h e a p p lica t io n s e rve r u s in g h ig h - le ve l
a n d p la t fo rm in d e p e n d e n t ca lls . Th e a p p lica t io n
s e rve r re s p o n d s t o t h e clie n t re q u e s t s , m a ke s
d a t a b a s e ca lls a s n e e d e d in t o t h e u n d e rlyin g
d a t a b a s e , a n d re p lie s t o t h e clie n t p ro g ra m a s
a p p ro p ria t e .
Th e t h re e t ie rs in t h e we b - b a s e d a u ct io n h o u s e
e xa m p le co n s is t s o f t h e t h in - clie n t s e rvle t , t h e
En t e rp ris e Ja va Be a n s s e rve r ( t h e a p p lica t io n
s e rve r) , a n d t h e d a t a b a s e s e rve r a s s h o wn in t h e
fig u re .

En t it y a n d S e s s io n Be a n s
Th e re a re t wo t yp e s o f En t e rp ris e Be a n s : e n t it y
Be a n s a n d s e s s io n Be a n s . An En t e rp ris e Be a n t h a t
im p le m e n t s a b u s in e s s e n t it y is a n e n t it y Be a n , a n d a n En t e rp ris e
Be a n t h a t im p le m e n t s a b u s in e s s t a s k is a s e s s io n Be a n .
Typ ica lly, a n e n t it y Be a n re p re s e n t s o n e ro w o f p e rs is t e n t d a t a
s t o re d in a d a t a b a s e t a b le . In t h e a u ct io n h o u s e e xa m p le ,
RegistrationBean is a n e n t it y Be a n t h a t re p re s e n t s d a t a fo r o n e
re g is t e re d u s e r, a n d AuctionItemBean is a n e n t it y Be a n t h a t
re p re s e n t s t h e d a t a fo r o n e a u ct io n it e m . En t it y Be a n s a re
t ra n s a ct io n a l a n d lo n g - live d . As lo n g a s t h e d a t a re m a in s , t h e
e n t it y Be a n ca n a cce s s a n d u p d a t e t h a t d a t a . Th is d o e s n o t m e a n
yo u n e e d a Be a n ru n n in g fo r e ve ry t a b le ro w. In s t e a d , En t e rp ris e
Be a n s a re lo a d e d a n d s a ve d a s n e e d e d .
A s e s s io n Be a n m ig h t e xe cu t e d a t a b a s e re a d s a n d writ e s , b u t it is
http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/code2.html (3 of 8) [2001-6-13 8:09:17]

n o t re q u ire d . A s e s s io n Be a n m ig h t in vo ke t h e JDBC ca lls it s e lf o r it
m ig h t u s e a n e n t it y Be a n t o m a ke t h e ca ll, in wh ich ca s e t h e
s e s s io n Be a n is a clie n t t o t h e e n t it y Be a n . A s e s s io n Be a n 's fie ld s
co n t a in t h e s t a t e o f t h e co n ve rs a t io n a n d a re t ra n s ie n t . If t h e
s e rve r o r clie n t cra s h e s , t h e s e s s io n Be a n is g o n e . A s e s s io n Be a n
is o ft e n u s e d wit h o n e o r m o re e n t it y Be a n s a n d fo r co m p le x
o p e ra t io n s o n t h e d a t a .
S e s s io n Be a n s

En t it y Be a n s

Fie ld s co n t a in co n ve rs a t io n
sta te .

Re p re s e n t s d a t a in a d a t a b a s e .

Ha n d le s d a t a b a s e a cce s s fo r
clie n t .

S h a re s a cce s s fo r m u lt ip le u s e rs .

Life o f clie n t is life o f Be a n .

Pe rs is t s a s lo n g a s d a t a e xis t s .

Ca n b e t ra n s a ct io n a wa re .

Tra n s a ct io n a l.

Do e s n o t s u rvive s e rve r
cra s h e s .

S u rvive s s e rve r cra s h e s .

No t fin e - g ra in e d d a t a h a n d lin g

Fin e - g ra in e d d a t a h a n d lin g

N o t e : In t h e En t e rp ris e Ja va Be a n s s p e cifica t io n ,
En t e rp ris e Ja va Be a n s S e rve r s u p p o rt fo r s e s s io n Be a n s is
m a n d a t o ry. En t e rp ris e Ja va Be a n s s e rve r s u p p o rt fo r
e n t it y Be a n s wa s o p t io n a l, b u t is m a n d a t o ry fo r ve rs io n
2 . 0 o f t h e s p e cifica t io n .

Au c t io n Ho u s e W o rk in g s
Th e d ia g ra m s h o ws t h e En t e rp ris e Be a n s fo r t h e a u ct io n h o u s e
a p p lica t io n a n d t h e ir re la t io n s h ip t o t h e En t e rp ris e Ja va Be a n s
s e rve r. Th e t h in - clie n t s e rve r in vo ke s b u s in e s s lo g ic in t h e fo u r
En t e rp ris e Be a n s t h ro u g h t h e ir h o m e a n d re m o t e in t e rfa ce s . Th e
En t e rp ris e Ja va Be a n s s e rve r in t h is e xa m p le h a n d le s t h e lo w- le ve l
d e t a ils in clu d in g d a t a b a s e re a d a n d writ e o p e ra t io n s .
Th e fo u r En t e rp ris e Be a n s in t h e e xa m p le a re :

t o clo s e , a n d it e m s wh o s e s u m m a ry m a t ch e s a s e a rch s t rin g
fro m t h e d a t a b a s e . It a ls o ch e cks t h e u s e r ID a n d p a s s wo rd
wh e n s o m e o n e p la ce s a b id , a n d s t o re s n e w b id s in t h e
da ta ba se .

in t e rfa ce .

Writing Advanced Applications, Chapter 2: A Multi-Tiered Application with Enterprise Beans

s e rvice s t o lo ca t e re m o t e s e rve r o b je ct s ( t h e Be a n 's re m o t e
in t e rfa ce o b je ct ) , a n d d a t a co m m u n ica t io n s e rvice s t o m o ve d a t a
fro m t h e clie n t , t h ro u g h t h e re m o t e s e rve r o b je ct , t o it s fin a l
d e s t in a t io n in a s t o ra g e m e d iu m .
Lo o k u p S e rv ic e
To fin d re m o t e s e rve r o b je ct s a t ru n t im e , t h e clie n t p ro g ra m n e e d s
a wa y t o lo o k t h e m u p . On e wa y t o lo o k re m o t e s e rve r o b je ct s u p
a t ru n t im e is t o u s e t h e Ja va Na m in g a n d Dire ct o ry In t e rfa ce TM
( JNDI) API. JNDI is a co m m o n in t e rfa ce t o e xis t in g n a m in g a n d
d ire ct o ry in t e rfa ce s . Th e En t e rp ris e Ja va Be a n s co n t a in e rs u s e JNDI
a s a n in t e rfa ce t o t h e Re m o t e Me t h o d In vo ca t io n ( RMI) n a m in g
s e rvice .
At d e p lo ym e n t t im e , t h e JNDI s e rvice re g is t e rs ( b in d s ) t h e re m o t e
in t e rfa ce wit h a n a m e . As lo n g a s t h e clie n t p ro g ra m u s e s t h e
s a m e n a m in g s e rvice a n d a s ks fo r t h e re m o t e in t e rfa ce b y it s
re g is t e re d n a m e , it will b e a b le t o fin d it . Th e clie n t p ro g ra m ca lls
t h e lookup m e t h o d o n a javax.naming.Context o b je ct t o a s k fo r t h e
re m o t e in t e rfa ce b y it s re g is t e re d n a m e . Th e javax.naming.Context
o b je ct is wh e re t h e b in d in g s a re s t o re d a n d is a d iffe re n t o b je ct
fro m t h e En t e rp ris e Ja va Be a n s co n t e xt , wh ich is co ve re d la t e r.
D a t a Co m m u n ic a t io n
On ce t h e clie n t p ro g ra m g e t s a re fe re n ce t o a re m o t e s e rve r
o b je ct , it m a ke s ca lls o n t h e re m o t e s e rve r o b je ct 's m e t h o d s .
Be ca u s e t h e clie n t p ro g ra m h a s a re fe re n ce t o t h e re m o t e s e rve r
o b je ct , a t e ch n iq u e ca lle d d a t a m a rs h a llin g is u s e d t o m a ke it
a p p e a r a s if t h e re m o t e s e rve r o b je ct is lo ca l t o t h e clie n t p ro g ra m .
Da t a m a rs h a llin g is wh e re m e t h o d s ca lle d o n t h e re m o t e s e rve r
o b je ct a re wra p p e d wit h t h e ir d a t a a n d s e n t t o t h e re m o t e s e rve r
o b je ct . Th e re m o t e s e rve r o b je ct u n wra p s ( u n m a rs h a lls ) t h e
m e t h o d s a n d d a t a , a n d ca lls t h e En t e rp ris e Be a n . Th e re s u lt s o f
t h e ca ll t o t h e En t e rp ris e Be a n a re wra p p e d a g a in , p a s s e d b a ck t o
t h e clie n t t h ro u g h t h e re m o t e s e rve r o b je ct , a n d u n m a rs h a lle d .
Th e En t e rp ris e Ja va Be a n s co n t a in e rs u s e RMI s e rvice s t o m a rs h a l
d a t a . Wh e n t h e Be a n is co m p ile d , stub a n d skeleton file s a re
cre a t e d . Th e stub file p ro vid e s t h e d a t a wra p p in g a n d u n wra p p in g
co n fig u ra t io n o n t h e clie n t , a n d t h e skeleton p ro vid e s t h e s a m e
in fo rm a t io n fo r t h e s e rve r.
Th e d a t a is p a s s e d b e t we e n t h e clie n t p ro g ra m a n d t h e s e rve r
u s in g s e ria liza t io n . S e ria liza t io n is a wa y t o re p re s e n t a t Ja va TM
http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/code2.html (7 of 8) [2001-6-13 8:09:17]

Writing Advanced Applications, Chapter 2: A Multi-Tiered Application with Enterprise Beans

o b je ct s a s b yt e s t h a t ca n b e s e n t o ve r t h e n e t wo rk a s a s t re a m
a n d re co n s t ru ct e d o n t h e o t h e r s id e in t h e s a m e s t a t e t h e y we re in
we n t o rig in a lly s e n t .
[ TOP]

[ Th is p a g e wa s u p d a t e d : 4 - Ju n - 2 0 0 1 ]
Pro d u ct s & APIs | De ve lo p e r Co n n e ct io n | Do cs & Tra in in g | On lin e S u p p o rt
Co m m u n it y Dis cu s s io n | In d u s t ry Ne ws | S o lu t io n s Ma rke t p la ce | Ca s e S t u d ie s
Glo s s a ry | Fe e d b a ck | A- Z In d e x
Fo r m o re in fo rm a t io n o n Ja va t e ch n o lo g y
a n d o t h e r s o ft wa re fro m S u n Micro s ys t e m s , ca ll:

(800) 786-7638
Ou t s id e t h e U. S . a n d Ca n a d a , d ia l yo u r co u n t ry's
AT&T Dire ct Acce s s Nu m b e r firs t .

Co p yrig h t © 1 9 9 5 - 2 0 0 1 S u n Micro s ys t e m s , In c.
All Rig h t s Re s e rve d . Te rm s o f Us e . Priva cy Po licy.

http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/code2.html (8 of 8) [2001-6-13 8:09:17]

Advanced Programming for the Java(TM) 2 Platform

Tra in in g In d e x

Ad v a n c e d P ro g ra m m in g fo r t h e
Ja v a 2 P la t fo rm
TM

By Ca lv in Au s t in a n d Mo n ic a P a w la n
No v e m b e r 1 9 9 9
[ CONTENTS ] [ NEXT> > ]

[ DOWNLOAD]
Re q u ire s lo g in

Ea rly Ac c e s s
D o w n lo a d s

Bu g D a t a b a s e
S u b m it a Bu g
Vie w D a t a b a s e

N e w s le t t e rs
Ba c k I s s u e s
S u b s c rib e

Le a rn in g Ce n t e rs
Art ic le s
Bo o k s h e lf
Co d e S a m p le s
N e w t o Ja v a
Qu e s t io n o f t h e W e e k
Qu iz z e s
Te c h Tip s
Tu t o ria ls

Fo ru m s

As a n e xp e rie n ce d d e ve lo p e r o n t h e Ja va TM p la t fo rm , yo u
u n d o u b t e d ly kn o w h o w fa s t m o vin g a n d co m p re h e n s ive t h e
p la t fo rm is . It s m a n y a p p lica t io n p ro g ra m m in g in t e rfa ce s ( APIs )
p ro vid e a we a lt h o f fu n ct io n a lit y fo r a ll a s p e ct s o f a p p lica t io n a n d
s ys t e m - le ve l p ro g ra m m in g . Re a l- wo rld d e ve lo p e rs n e ve r u s e o n e
o r t wo APIs t o s o lve a p ro b le m , b u t b rin g t o g e t h e r ke y
fu n ct io n a lit y s p a n n in g a n u m b e r o f APIs . Kn o win g wh ich APIs yo u
n e e d , wh ich p a rt s o f wh ich APIs yo u n e e d , a n d h o w t h e APIs wo rk
t o g e t h e r t o cre a t e t h e b e s t s o lu t io n ca n b e a d a u n t in g t a s k.
To h e lp yo u n a vig a t e t h e Ja va APIs a n d fa s t - t ra ck yo u r p ro je ct
d e ve lo p m e n t t im e , t h is b o o k in clu d e s t h e d e s ig n , d e ve lo p m e n t ,
t e s t , a n d d e p lo ym e n t p h a s e s fo r a n e n t e rp ris e - wo rt h y a u ct io n
a p p lica t io n . Wh ile t h e e xa m p le a p p lica t io n d o e s n o t co ve r e ve ry
p o s s ib le p ro g ra m m in g s ce n a rio , it e xp lo re s m a n y co m m o n
s it u a t io n s a n d t h e d is cu s s io n s le a ve yo u wit h a s o lid m e t h o d o lo g y
fo r d e s ig n in g a n d b u ild in g yo u r o wn s o lu t io n s .
Th is b o o k is fo r d e ve lo p e rs wit h m o re t h a n a b e g in n in g le ve l o f
u n d e rs t a n d in g o f writ in g p ro g ra m s in t h e Ja va p ro g ra m m in g
la n g u a g e . Th e e xa m p le a p p lica t io n is writ t e n wit h t h e Ja va ® 2
p la t fo rm APIs a n d e xp la in e d in t e rm s o f fu n ct io n a l h o ws a n d wh ys ,
s o if yo u n e e d h e lp in s t a llin g t h e Ja va p la t fo rm , s e t t in g u p yo u r
e n viro n m e n t , o r g e t t in g yo u r firs t a p p lica t io n t o wo rk, yo u s h o u ld
firs t re a d a m o re in t ro d u ct o ry b o o k s u ch a s Es s e n t ia ls o f t h e
Ja v a P ro g ra m m in g La n g u a g e : A Ha n d s - On Gu id e o r Th e Ja v a
Tu t o ria l.

http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/index.html (1 of 4) [2001-6-13 8:09:32]

Advanced Programming for the Java(TM) 2 Platform

N o t e : Th is
t u t o ria l is
a va ila b le a s a
b o o k fro m o n lin e
b o o k s e lle rs Als o ,
s e n d yo u r
co m m e n t s a n d
thoughts to
jd cb o o k@s u n . co m

Te c h n o lo g y Ce n t e rs
SELECT

Co n t e n t s
Ch a p t e r 1 : Ma t ch in g Pro je ct Re q u ire m e n t s wit h Te ch n o lo g y

Advanced Programming for the Java(TM) 2 Platform

S p e cia l t h a n ks t o e xp e rt s Is a a c Elia s , Da n ie l Liu , a n d Ma rk
Ho rwa t h fo r t h e ir co n t rib u t io n s t o t h e a d va n ce d e xa m p le s in t h e
b o o k.

Re a d e r Fe e d b a c k
Te ll u s wh a t yo u t h in k o f t h is b o o k.
[ TOP]

[ Th is p a g e wa s u p d a t e d : 4 - Ju n - 2 0 0 1 ]
Pro d u ct s & APIs | De ve lo p e r Co n n e ct io n | Do cs & Tra in in g | On lin e S u p p o rt
Co m m u n it y Dis cu s s io n | In d u s t ry Ne ws | S o lu t io n s Ma rke t p la ce | Ca s e S t u d ie s
Glo s s a ry | Fe e d b a ck | A- Z In d e x
Fo r m o re in fo rm a t io n o n Ja va t e ch n o lo g y
a n d o t h e r s o ft wa re fro m S u n Micro s ys t e m s , ca ll:

(800) 786-7638
Ou t s id e t h e U. S . a n d Ca n a d a , d ia l yo u r co u n t ry's
AT&T Dire ct Acce s s Nu m b e r firs t .

Co p yrig h t © 1 9 9 5 - 2 0 0 1 S u n Micro s ys t e m s , In c.
All Rig h t s Re s e rve d . Te rm s o f Us e . Priva cy Po licy.

http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/index.html (4 of 4) [2001-6-13 8:09:32]

Writing Advanced Applications, Chapter 2: Entity and Session Beans

Tra in in g In d e x

W rit in g Ad v a n c e d Ap p lic a t io n s
Ch a p t e r 2 Co n t in u e d : En t it y a n d S e s s io n Be a n s
[ < < BACK] [ CONTENTS ] [ NEXT> > ]

Th e e xa m p le u s e s t wo e n t it y Be a n s a n d t wo s e s s io n Be a n s . Th e
e n t it y Be a n s , AuctionItemBean a n d RegistrationBean, re p re s e n t
p e rs is t e n t it e m s t h a t co u ld b e s t o re d in a d a t a b a s e , a n d t h e
s e s s io n Be a n s , SellerBean a n d BidderBean, re p re s e n t s h o rt - live d
o p e ra t io n s wit h t h e clie n t a n d d a t a .
Re q u ire s lo g in

Ea rly Ac c e s s
D o w n lo a d s

Bu g D a t a b a s e
S u b m it a Bu g
Vie w D a t a b a s e

N e w s le t t e rs
Ba c k I s s u e s
S u b s c rib e

Le a rn in g Ce n t e rs
Art ic le s
Bo o k s h e lf
Co d e S a m p le s
N e w t o Ja v a
Qu e s t io n o f t h e W e e k
Qu iz z e s
Te c h Tip s
Tu t o ria ls

Fo ru m s

Th e s e s s io n Be a n s a re t h e clie n t in t e rfa ce t o t h e e n t it y b e a n s . Th e
S e lle rBe a n p ro ce s s e s re q u e s t s t o a d d n e w a u ct io n it e m s fo r s a le .
Th e Bid d e rBe a n p ro ce s s e s re q u e s t s t o re t rie ve a u ct io n it e m s a n d
p la ce b id s o n t h o s e it e m s . Ch a n g in g a n d a d d in g t o t h e d a t a b a s e
d a t a in a co n t a in e r- m a n a g e d Be a n is le ft t o t h e e n t it y Be a n s .

Writing Advanced Applications, Chapter 2: Entity and Session Beans

Te c h n o lo g y Ce n t e rs
SELECT

Th e AuctionServlet m e t h o d s s h o wn a b o ve in vo ke b u s in e s s lo g ic
t h a t e xe cu t e s o n t h e s e rve r b y lo o kin g u p a n En t e rp ris e Be a n a n d
ca llin g o n e o r m o re o f it s m e t h o d s . Wh e n t h e s e rvle t a d d s HTML
co d e s t o a p a g e fo r d is p la y t o t h e u s e r, t h a t lo g ic e xe cu t e s o n t h e
clie n t .
Fo r e xa m p le , t h e listAllItems(out) m e t h o d e xe cu t e s co d e o n t h e
clie n t t o d yn a m ica lly g e n e ra t e a n HTML p a g e t o b e vie we d b y t h e
clie n t in a b ro ws e r. Th e HTML p a g e is p o p u la t e d wit h t h e re s u lt s o f
a ca ll t o BidderBean t h a t e xe u ct e s lo g ic o n t h e s e rve r t o g e n e ra t e a
lis t o f a ll a u ct io n it e m s .
private void listAllItems(ServletOutputStream out)
throws IOException{
//Put text on HTML page
setTitle(out, "Auction results");
String text = "Click Item number for description
and to place bid.";
try{
addLine(""+text, out);
//Look up Bidder bean home interface.
BidderHome bhome=(BidderHome) ctx.lookup("bidder");
//Create Bidder bean remote interface.
Bidder bid=bhome.create();
//Call Bidder bean method through remote interface.
Enumeration enum=(Enumeration)bid.getItemList();

http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/code3.html (2 of 7) [2001-6-13 8:09:42]

if(enum != null) {
//Put retrieved items on servlet page.
displayitems(enum, out);
addLine("", out);
}
} catch (Exception e) {
//Pring error on servlet page.
addLine("AuctionServlet List All Items error",out);
System.out.println("AuctionServlet :"+e);
}
out.flush();
}

En t it y Be a n s
AuctionItemBean a n d RegistrationBean a re e n t it y Be a n s .
AuctionItemBean a d d s n e w a u ct io n it e m s t o t h e d a t a b a s e a n d
u p d a t e s t h e b id a m o u n t a s u s e rs b id o n t h e it e m . RegistrationBean

a d d s in fo rm a t io n t o t h e d a t a b a s e o n re g is t e re d u s e rs . Bo t h Be a n s
co n s is t o f t h e cla s s e s d e s crib e d h e re .
Au c t io n I t e m En t it y Be a n
He re a re t h e AuctionItemBean cla s s e s . Re m e m b e r t h a t t h e s e
En t e rp ris e Be a n s a re d is t rib u t e d o b je ct s t h a t u s e t h e Re m o t e
Me t h o d In vo ca t io n ( RMI) API, s o wh e n a n e rro r o ccu rs , a n RMI
re m o t e e xce p t io n is t h ro wn .

AuctionItemPK is t h e p rim a ry ke y cla s s . Th e En t e rp ris e Ja va Be a n s

s e rve r re q u ire s a co n t a in e r- m a n a g e d e n t it y Be a n t o h a ve a
p rim a ry ke y cla s s wit h a p u b lic p rim a ry ke y fie ld ( o r fie ld s , if u s in g
co m p o s it e p rim a ry ke ys ) . Th e Be a n d e ve lo p e r im p le m e n t s t h is
cla s s . Th e ID fie ld is t h e p rim a ry ke y in t h e AUCTIONITEMS t a b le
s h o wn ju s t b e lo w, s o t h e id fie ld is a p u b lic fie ld in t h is cla s s . Th e
id fie ld is a s s ig n e d a va lu e wh e n t h e p rim a ry ke y cla s s is
co n s t ru ct e d .
Yo u ca n re q u e s t t h e co n t a in e r m a n a g e d a t a b a s e p e rs is t e n ce fo r
a n En t e rp ris e Be a n o r writ e t h e co d e t o m a n a g e t h e p e rs is t e n ce
yo u rs e lf. In t h is ch a p t e r, a ll b e a n s ( e n t it y a n d s e s s io n ) a re
co n t a in e r- m a n a g e d . Wit h co n t a in e r- m a n a g e d Be a n s , a ll yo u d o is
s p e cify wh ich fie ld s a re co n t a in e r m a n a g e d a n d le t t h e En t e rp ris e
Ja va Be a n s s e rve r d o t h e re s t . Th is is g re a t fo r s im p le a p p lica t io n s ,
b u t if yo u a re co d in g s o m e t h in g t h a t is fa irly co m p le x, yo u m ig h t
n e e d m o re co n t ro l.
Ho w t o o ve rrid e t h e u n d e rlyin g En t e rp ris e Ja va Be a n s s e rvice s t o
g a in m o re co n t ro l o r p ro vid e s im ila r s e rvice s fo r n o n - En t e rp ris e
Ja va Be a n a p p lica t io n s is co ve re d in Ch a p t e r 3 .
Au c t io n I t e m s Ta b le
He re is t h e AUCTIONITEMS t a b le .
create table AUCTIONITEMS (SUMMARY VARCHAR(80) ,
ID INT ,
COUNTER INT ,
DESCRIPTION VARCHAR(1000) ,
STARTDATE DATE ,
ENDDATE DATE ,
STARTPRICE DOUBLE PRECISION ,
INCREMENT DOUBLE PRECISION ,
SELLER VARCHAR(30) ,
MAXBID DOUBLE PRECISION,
BIDCOUNT INT,
HIGHBIDDER VARCHAR(30) )

Re g is t ra t io n En t it y Be a n
RegistrationBean co n s is t s o f t h e s a m e kin d s o f cla s s e s a n d
d a t a b a s e t a b le a s t h e AuctionItem Be a n , e xce p t t h e a ct u a l

b u s in e s s lo g ic, d a t a b a s e t a b le fie ld s , a n d p rim a ry ke y a re
s o m e wh a t d iffe re n t . Ra t h e r t h a n d e s crib e t h e cla s s e s , yo u ca n
b ro ws e t h e m a n d re fe r b a ck t o t h e AuctionItem Be a n d is cu s s io n if
yo u h a ve q u e s t io n s .

b u s in e s s lo g ic fo r t h is Be a n . Th e s e m e t h o d s a re t h e o n e s t h a t t h e
clie n t ca lls re m o t e ly.
BidderHome is t h e h o m e in t e rfa ce . It d e s crib e s h o w t h e Be a n is

cre a t e d in , fo u n d in , a n d re m o ve d fro m it s co n t a in e r.
BidderBean is t h e En t e rp ris e Be a n . It im p le m e n t s SessionBean,

p ro vid e s t h e b u s in e s s lo g ic fo r t h e d e ve lo p e r- d e fin e d m e t h o d s ,
a n d im p le m e n t s SessionBean m e t h o d s fo r cre a t in g t h e Be a n a n d
s e t t in g t h e s e s s io n co n t e xt .
Auction.java co n t a in s a s m a ll cla s s t h a t d e cla re s va ria b le s u s e d b y
BidderBean.

S e lle r S e s s io n Be a n
SellerBean co n s is t s o f t h e s a m e kin d s o f cla s s e s a s BidderBean,

e xce p t t h e b u s in e s s lo g ic is d iffe re n t . Ra t h e r t h a n d e s crib e t h e
cla s s e s , yo u ca n b ro ws e t h e m a n d re fe r b a ck t o t h e BidderBean
d is cu s s io n if yo u h a ve q u e s t io n s .

Writing Advanced Applications, Chapter 2: Entity and Session Beans

t h e s e rve r.
[ TOP]

[ Th is p a g e wa s u p d a t e d : 4 - Ju n - 2 0 0 1 ]
Pro d u ct s & APIs | De ve lo p e r Co n n e ct io n | Do cs & Tra in in g | On lin e S u p p o rt
Co m m u n it y Dis cu s s io n | In d u s t ry Ne ws | S o lu t io n s Ma rke t p la ce | Ca s e S t u d ie s
Glo s s a ry | Fe e d b a ck | A- Z In d e x
Fo r m o re in fo rm a t io n o n Ja va t e ch n o lo g y
a n d o t h e r s o ft wa re fro m S u n Micro s ys t e m s , ca ll:

(800) 786-7638
Ou t s id e t h e U. S . a n d Ca n a d a , d ia l yo u r co u n t ry's
AT&T Dire ct Acce s s Nu m b e r firs t .

Co p yrig h t © 1 9 9 5 - 2 0 0 1 S u n Micro s ys t e m s , In c.
All Rig h t s Re s e rve d . Te rm s o f Us e . Priva cy Po licy.

http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/code3.html (7 of 7) [2001-6-13 8:09:42]

Writing Advanced Applications, Chapter 2: Examining a Container-managed Bean

Tra in in g In d e x

W rit in g Ad v a n c e d Ap p lic a t io n s
Ch a p t e r 2 Co n t in u e d : Ex a m in in g a Co n t a in e r- Ma n a g e d Be a n
[ < < BACK] [ CONTENTS ] [ NEXT> > ]

Th is s e ct io n wa lks t h ro u g h t h e Re g is t ra t io n Be a n . ja va co d e t o s h o w
h o w e a s y it is t o h a ve t h e co n t a in e r m a n a g e p e rs is t e n t d a t a
s t o ra g e t o a n u n d e rlyin g m e d iu m s u ch a s a d a t a b a s e ( t h e d e fa u lt ) .
Ch a p t e r 3 m o d ifie s RegistrationBean t o u s e Be a n - m a n a g e d
p e rs is t e n ce t o h a n d le d a t a b a s e a cce s s a n d m a n a g e t ra n s a ct io n s .
Re q u ire s lo g in

Ea rly Ac c e s s
D o w n lo a d s

Bu g D a t a b a s e
S u b m it a Bu g
Vie w D a t a b a s e

N e w s le t t e rs
Ba c k I s s u e s
S u b s c rib e

Le a rn in g Ce n t e rs
Art ic le s
Bo o k s h e lf
Co d e S a m p le s
N e w t o Ja v a
Qu e s t io n o f t h e W e e k
Qu iz z e s
Te c h Tip s
Tu t o ria ls

Fo ru m s

Writing Advanced Applications, Chapter 2: Examining a Container-managed Bean

Te c h n o lo g y Ce n t e rs
SELECT

public String theuser, password, creditcard,
emailaddress;
public double balance;

Cre a t e Me t h o d
Th e Be a n 's ejbCreate m e t h o d is ca lle d b y t h e co n t a in e r a ft e r t h e
clie n t p ro g ra m ca lls t h e create m e t h o d o n t h e remote in t e rfa ce a n d
p a s s e s in t h e re g is t ra t io n d a t a . Th is m e t h o d a s s ig n s t h e in co m in g
va lu e s t o t h e m e m b e r va ria b le s t h a t re p re s e n t u s e r d a t a . Th e
co n t a in e r h a n d le s s t o rin g a n d lo a d in g t h e d a t a , a n d cre a t in g n e w
e n t rie s in t h e u n d e rlyin g s t o ra g e m e d iu m .
public RegistrationPK ejbCreate(String theuser,
String password,
String emailaddress,
String creditcard)
throws CreateException, RemoteException {
this.theuser=theuser;
this.password=password;
this.emailaddress=emailaddress;
this.creditcard=creditcard;
this.balance=0;

En t it y Co n t e x t Me t h o d s
An e n t it y Be a n h a s a n a s s o cia t e d EntityContext in s t a n ce t h a t g ive s
t h e Be a n a cce s s t o co n t a in e r- m a n a g e d ru n t im e in fo rm a t io n s u ch
a s t h e t ra n s a ct io n co n t e xt .
public void setEntityContext(
javax.ejb.EntityContext ctx)
throws RemoteException {
this.ctx = ctx;
}
public void unsetEntityContext()
throws RemoteException{
ctx = null;
}

Lo a d Me t h o d
Th e Be a n 's ejbLoad m e t h o d is ca lle d b y t h e co n t a in e r t o lo a d d a t a
fro m t h e u n d e rlyin g s t o ra g e m e d iu m . Th is wo u ld b e n e ce s s a ry
wh e n BidderBean o r SellerBean n e e d t o ch e ck a u s e r's ID o r
p a s s wo rd a g a in s t t h e s t o re d va lu e s .
N o t e : No t a ll Be a n o b je ct s a re live a t a n y o n e t im e . Th e
En t e rp ris e Ja va Be a n s TM s e rve r m ig h t h a ve a co n fig u ra b le

http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/code4.html (2 of 5) [2001-6-13 8:09:47]

Writing Advanced Applications, Chapter 2: Examining a Container-managed Bean

n u m b e r o f Be a n s t h