atelier-ejb-2.0
DESCRIPTION
atelier-ejb-2.0atelier-ejb-2.0atelier-ejb-2.0atelier-ejb-2.0TRANSCRIPT
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 1/32
Intergiciel et Construction d’Applications Reparties
c2007, Cedric JOFFROY, Sebastien MOSSER, Michel RIVEILL
(version du 14 novembre 2008 - 11:13)
Chapitre9
Premiers pas J2EE
L’objectif de cette annexe est de permettre a chacun de se familiariser un peu avec uneplate-forme respectant les specifications J2EE (Java 2 Enterprise Edition). Pour cela nousallons developper une petite application permettant a chacun de gere ses cartes de visite(ajout, suppression et consultation) et de les rendre accessibles aux autres utilisateurs (uni-quement consultation). Pour atteindre ce but, l’application se compose de deux parties :le service d’annuaire qui doit pouvoir etre utilisee a distance et differentes applicationsclientes de type client leger ou client lourd graphique ou en ligne de commande. Pour des
raisons de simplicite nous ne nous interesserons pas aux problemes lies a l’authentificationdes differentes classes d’utilisateurs.
Pour mettre en œuvrecette application nous allons utiliser :
– un container managed entity bean pour gerer la persistance des cartes de visites,– un statefull session bean pour l’ajout et la suppression des cartes de visites,– un stateless session bean pour la consultation des cartes stockees,– un client lourd pour administrer l’application et permettre l’ajout et la suppression
de cartes de visites– un client leger de consultation en utilisant une (servlet ).
Une version html de ce texte est disponible a l’adresse http://rangiroa.polytech.
unice.fr/riveill/enseignement/tp/j2ee.html . Elle contiendra les evolutions futuresdu sujet.
9.1 Les outils a utiliser
Parmis toutes les plates-formes J2EE disponible, nous avons fait le choix d’utiliserJ2EE Jonas developpe par des equipes francaises dans le cadre du consortium Object-
Web qui peut etre recupere sur le site web du consortium1.
1www.objectweb.com
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 2/32
2 CHAPITRE 9. PREMIERS PAS J2EE
9.1.1 Installation de Jonas
L’installation proposee ici concerne la version 4.7.7 et s’adresse aux utilisation d’unemachine de type Linux ou Mac Os X. On laisse au lecteur attentif la modification decette procedure pour installer Jonas sur une plateforme Windows ou pour installer uneautre version de Jonas.
Nous allons installer Jonas dans le repertoire /opt/.
L I N UX : ~ > t a r x v f z j o n a s4 . 7 . 7 - t o m c a t 5 . 5 . 1 5 . t g z
L I NU X : ~ > s ud o m v J O N AS _ 4_ 7 _ 7 / o p t /.
Pour fonctionner correctement, Jonas utiliser trois variables d’environnement quidoivent etre correctement positionnee :
– JONAS ROOT : chemin d’acces au repertoire de Jonas,– PATH : chemin d’acces aux binaires de Jonas,– CLASSPATH : doit contenir les deux archives necessaire au bon deroulement de ces
ateliers : ejb-2 1-api.jar et servlet-2 4.jar.– selon les versions Jonas, il peut y avoir d’autres variables d’environneent : SPS=’ :’
et JONAS BASE=JONAS ROOT
9.1.2 Installation d’ANT
L’installation Ant est recommandee si l’on veut tester les exemples fournis avec Jonas
et qui se trouve dans le repertoire /opt/JONAS 4 7 7/examples/src/. Ant est par exempledisponible a l’url http://ant.apache.org/bindownload.cgi et s’installe tres aisement.
L I N UX : ~ > u n z ip a p ac h e - a n t - 1 . 6. 5 - b i n . z i pL I N UX : ~ > s u d o m v a p ac h e - a nt - 1 . 6 .5 / o p t / .
Comme pour Jonas, il est necessaire que la variable d’environnement ANT HOME
contienne le repertoire d’installation de Ant.
9.1.3 Mise a jour des variables d’environnement
Pour etre certain de ne pas oublier une mise a jour lors d’une utilisation ulterieure,nous vous invitons a regrouper celles-ci dans le fichier environnement.sh.
# ! / b i n / s h
# M ise a j ou r d es v a ri a bl e s d ’ e n v ir o n ne m n ts p ou r J o na s
J O N A S _ R O O T = / o p t / J O N A S _ 4 _ 7 _ 7
echo " J O N A S _ R O O T s e t t o $ J O N A S _ R O O T "
P A T H = $ J O N A S _ R O O T / b i n / u n i x : $ P A T H
echo " P A T H s e t t o $ P A T H "
C L A S S PA T H = $ C L A S S P A T H : $ J O N A S _R O O T / l i b / c o m m o n s / j 2 e e / ej b - 2 _ 1 - a p i . j a r
CLASSPATH=$CLASSPATH:$JONAS_ROOT/lib/commons/j2ee/servlet -2_4.jar
echo " C L A S S P A T H s e t t o $ C L A S S P A T H "
# M ise a j ou r d es v a ri a bl e s d ’ e n v ir o n ne m e nt p ou r A nt
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 3/32
9.2. GESTION DE LA PERSISTANCE - ENTITY BEAN 3
A N T _ H OM E = / o p t / a p a c h e - a n t - 1 .6 . 5
echo " A N T _ H O M E s e t t o $ A N T _ H O M E "
export J O NA S _ R O O T A N T _ H OM E P A T H C L A S S PA T H
Listing 9.1 – Fichier environment.sh
Il ne reste plus qu’a executer ce shell a l’aide de la commande source pour s’affranchirdu dialecte utilise (zsh, tcsh ou bash) :
LINUX:~> source environnement.sh
9.1.4 Pour tester l’installation
Avant toutes autres manipulation, un test de l’installation s’impose. Suivez attentive-ment les differents messages d’erreurs.
L I NU X : ~ > j o na s c h ec k
L I NU X : ~ > j o na s s t ar t
1. Le check verifie l’etat de l’installation, et peut renvoyer des warnings, rien de biengrave.
2. Le start lance le serveur Jonas. Lorsque le lancement est termine, il rend la mainen indiquant qu’il a demarre.
9.2 Gestion de la persistance - entity bean
Les objets persistants sont les cartes de visites . Nous allons donc les implementer sousla forme d’entity bean dont la persistance est directement geree par le container. Avantd’ecrire l’entity bean, construisons la base de donnees.
9.2.1 La base de donnees
Pour conserver les cartes de visite nous allons utiliser une base de donnee qui contiendraune seule table CARTEDEVISITE structuree de la maniere suivante :
– un id permettant d’identifier de maniere unique chacune des cartes de visite,
– un nom,– un prenom,– une adresse mail,– un numero de telephone.Tous ces champs sont de type varchar et sont non nuls, a l’exception du champ id qui
est de type identity (shortcut pour un integer primary key dans le SGBD utilise).Nous utiliserons le micro–moteur de base de donnees HSQLDB, livre avec Jonas. Les
informations de connexion sont les suivantes
urlid localhost -dbjonas
u r l j d b c : h s q ld b : h s q l : / / l o c a l h o st : 9 0 0 1 / d b _ j o n a s
u s e r n am e j o n as
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 4/32
4 CHAPITRE 9. PREMIERS PAS J2EE
p a s s w or d j o n as
Listing 9.2 – Fichier sql-rc.conf
Description du schema relationnel
1 d ro p t a bl e i f e x is t s C A R T E D E V I S I T E ;
c r e a te t a b l e C A R T E DE V I S I T E (
ID IDENTITY ,
NO M varchar (255) n o t n ul l ,
PRENOM varchar (255) n o t n ul l ,
6 EMAIL varchar (255) n o t n ul l ,
PHONE varchar (255) n o t n ul l
);
Listing 9.3 – Fichier database.sql
Chargement de la relation dans le moteur
L I N UX : ~ > j a v a - j a r $ J O N A S _R O O T / l i b / c o m m o n s / j o n as / h s q l d b . j a r \
2 - - a u t o C o mm i t - - c o n t i n u eO n E r r \
- - r c f i le . / s ql - r c . c o n f l o c al h o st - d b j o n a s d a t a b as e . s q l
Script de chargement automatique
Pour simplifier l’initialisation de la base de donnees, on met en place un script quipermet d’executer cette ligne de commande :
# ! / b i n / s h
2 echo " D a t a b a s e i n i t i a l i z a t i o n "
j a v a - j a r $ J O N A S _R O O T / l i b / c o m m o n s / j o n as / h s q l d b . j a r \
- - a u t o C o mm i t - - c o n t i n u eO n E r r \
- - r c f i le . / s ql - r c . c o n f l o c al h o st - d b j o n a s d a t a b as e . s q l
Listing 9.4 – Fichier initDatabase.sh
Pour executer ce script (apres l’avoir rendu executable . . .) :
L I N UX : ~ > . / i n i t D a t a ba s e . s h
On dispose maintenant d’une relation vide dans le moteur de base de donnees HSQL.Si vous n’etes pas convaincu, lancez le client graphique Hsql Manager sans toucher aumot de passe :
L I N UX : ~ > j a v a - c p $ J O N A S _R O O T / l i b / c o m m o n s / j o n as / h s q l d b . j a r o r g . h s q ld b . u t i l . D a t a b a s e M a n a ge r
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 5/32
9.2. GESTION DE LA PERSISTANCE - ENTITY BEAN 5
9.2.2 Ecriture du container managed entity bean
Le code d’un entity bean est reparti dans plusieurs classes Java et descripteurs :– Remote Interface : l’interface qui definit les differentes methodes du Bean qui seront
accessibles pour les clients,– Home Interface : l’interface qui est visible par le client pour creer, retrouver puis
manipuler des instances du Bean ,– la classe d’implementation du Bean ,– deux descripteurs de deploiement indiquant entre autre le mapping objet ⇔
relationnel a utiliser pour garantir la persistance des objets, et les requetes per-mettant de les retrouver.
Interface remote : on decrit dans cette interface les fonctions permettant de manipuler
les cartes de visite.package carte_cmp;
import j a v a x . e j b . E J B O b j e c t ;
4 import j a v a . r m i . R e m o t e E x c e p t i o n ;
p u b l ic i n t e r fa c e CarteDeVisiteCMP extends E J B O bj e ct {
public I n t e ge r g e t I d ( ) throws R e m o t e E x c e p t i o n ;
9 public S t r in g g e t N om ( ) throws R e m o t e E x c e p t i o n ;
public S t r in g g e t P r en o m ( ) throws R e m o t e E x c e p t i o n ;
public S t r in g g e t E m ai l ( ) throws R e m o t e E x c e p t i o n ;
public S t r in g g e t P h on e ( ) throws R e m o t e E x c e p t i o n ;
14 / / P as de se tI d , le SG BD la g en er e .
p u b l ic v o i d s e t P r en o m ( S t r i n g p r e n om ) throws R e m o t e E x c e p t i o n ;
p u b l ic v o i d s e t No m ( S t r i n g n o m ) throws R e m o t e E x c e p t i o n ;
p u b l ic v o i d s e t E ma i l ( S t r i n g e m a il ) throws R e m o t e E x c e p t i o n ;
p u b l ic v o i d s e t P ho n e ( S t r i n g p h o ne ) throws R e m o t e E x c e p t i o n ;
19 }
Listing 9.5 – Fichier CarteDeVisiteCMP.java
Interface Home : cette interface, accessible par les clients de l’application, permetde retouver les instances de cartes de visites, d’en creer, . . .. Les instances qu’elle renvoie
sont du type de l’interface precedente, ce qui masque aux clients l’implementation reelledu Bean .
1 package carte_cmp;
import j a v a . r m i . R e m o t e E x c e p t i o n ;
import javax.ejb.*;
import java.util.*;
6
p u b l ic i n t e r fa c e CarteDeVisiteCMPHome extends E J B Ho m e {
public CarteDeVisiteCMP
c r ea t e ( S tr i ng n om , S t ri n g p re no m , S t ri n g e ma il , S t ri n g p h on e )
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 6/32
6 CHAPITRE 9. PREMIERS PAS J2EE
11 throws R e m o te E x c e pt i o n , C r e a t e E xc e p t i o n ;
public C a r t e D eV i s i t e C M P f i n d B y P ri m a r y K e y ( I n t e g er i d )
throws R e m o te E x c e pt i o n , F i n d e r E xc e p t i o n ;
16 public C o l le c t i o n f i n d B yN o m ( S t r i n g n o m )
throws R e m o te E x c e pt i o n , F i n d e r E xc e p t i o n ;
public C o l le c t i o n f i n d B y P re f i x ( S t r i n g p r e f ix )
throws R e m o te E x c e pt i o n , F i n d e r E xc e p t i o n ;
21 }
Listing 9.6 – Fichier CarteDeVisiteCMPHome.java
Implementation du bean Le choix d’utiliser un CMP entity bean simplifie grandement
la mise en oeuvre puis que la plupart des methodes n’ont pas besoin d’etre implementeeset seront generees a partir des fichiers de deploiement.
package carte_cmp;
import java.sql.*;
4 import javax.sql.*;
import java.util.*;
import javax.ejb.*;
import j a v a x . n a m i n g . * ;
9 p u b l ic a b s t ra c t c l a s s CarteDeVisiteCMPBean implements E n t i t yB e an {
/ ** A bs tr ac t = => g er e p ar l e c on te n eu r * */p u b l ic a b s t ra c t I n t e g er g e t Id ( ) ;
p u bl i c a b st r ac t v oi d s e t Id ( I n t e g e r i d ) ;
14 p u b l ic a b s t ra c t S t r i ng g e t N om ( ) ;
p u bl i c a b st r ac t v oi d s e t N om ( S t r i n g n o m ) ;
p u b l ic a b s t ra c t S t r i ng g e t P r en o m ( ) ;
p u bl i c a b st r ac t v oi d s e t P r e no m ( S t r i n g p r e n om ) ;
p u b l ic a b s t ra c t S t r i ng g e t E m ai l ( ) ;
19 p u bl i c a b st r ac t v oi d s e t E m ai l ( S t r i n g e m a il ) ;
p u b l ic a b s t ra c t S t r i ng g e t P h on e ( ) ;
p u bl i c a b st r ac t v oi d s e t P h on e ( S t r i n g p h o ne ) ;
/ / C re e l e c om po sa nt e t l e s au ve ga r de d an s l a b as e d e d on ne es a ve c
24 / / u n i ns er t
public Integere j bC r ea t e ( S tr i ng n om , S t ri n g p re no m , S t ri n g e ma il , S t ri n g p h on e )
throws C r e a t e Ex c e p t i o n {
/ / r e ns e ig n e l es p r op r ie t e d u c o mp o sa n t
29 setNom(nom);
setPrenom(prenom);
s e t E m a i l ( e m a i l ) ;
s e t P h o n e ( p h o n e ) ;
r e t u rn n u l l;
34 }
p u b l ic v o i d
e j b P o s tC r e a t e ( S t r i n g n om , S t r i ng p r en o m ,
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 7/32
9.2. GESTION DE LA PERSISTANCE - ENTITY BEAN 7
S t ri n g e ma il , S t ri n g p h on e ) {
39 / / D an s l e c as d e t a bl e s d e l i ai s on s , o n i n s ta n c ie r a it l es
/ / l i ai s on s i ci .
}
/ / l es m e th o de s s u iv a nt e s n ’ o n t p as b e so i n d e tr e i m pl e m an t e d an s
44 // le c ad re d u n C MP e nt it y be an
p u b l ic v o i d ejbRemove() throws R e m o ve E x ce p t io n { }
p u b l ic v o i d s e t E n t it y C o n t e x t ( E n t i t y C on t e x t c o n t ex t ) {
this . c o n te x t = c o nt e xt ;
}
49 p u b l ic v o i d u n s e t E n t it y C o n t e x t ( ) { this . c o n te x t = null ; }
p u b l ic v o i d e j b Ac t i va t e ( ) { }
p u b l ic v o i d e j b P as s iv a t e () { }
p u b l ic v o i d e j bL oa d () { }
54 p u b l ic v o i d e j bS to re ( ) { }}
Listing 9.7 – Fichier CarteDeVisiteCMPBean.java
La cle primaire utilisee sera de type auto incrementee (par la base de donnee). Nousn’utiliserons donc jamais la methode setId().
TODO pourquoi alors mettre cette methode ? on a besoin de l’operation ’setId’ car le
generateur de code rale si elle n’y est pas. Reste a le reverifier.
Descripteur de deploiement J2EE : ce fichier est commun a tous les serveur d’ap-plications J2ee et permet de preciser le mapping a utiliser entre l’entity bean et la base
de donnee.< ? x ml v e r s io n = " 1 . 0 " e n c o d in g = " I S O - 8 8 5 9 - 1 "? >
< e j b - j a r >
< d e sc r ip t io n > D e s c ri p te u r d e d e p lo i em e n t p o ur l E JB c a rt e d e v i si t e
</description >
5 <display -name> EJB CarteDeVisite </display -name>
<enterprise -beans >
<entity >
< d e s c r ip t i o n > E J B C a r t e D e Vi s i t e ( C M P ) < / d e s c ri p t i on >
<ejb-name>CarteDeVisiteCMP </ejb-name>
10 <home> carte_cmp. CarteDeVisiteCMPHome </home>
<remote >carte_cmp .CarteDeVisiteCMP </ remote >
<ejb-class>carte_cmp.CarteDeVisiteCMPBean </ejb-class>
<persistence -type >Container </persistence -type><prim-key-class>java.lang.Integer </prim-key-class>
15 <reentr ant >False </re entrant >
<abstract -schema -name> cartedevisite </abstract -schema -name >
< c m p - f i e l d >
< f i e l d - n a m e > i d < / f i e l d - n a m e >
</cmp-field>
20 < c m p - f i e l d >
< f i e l d - n a m e > n o m < / f i e l d - n a m e >
</cmp-field>
< c m p - f i e l d >
<field-name>prenom </field-name>
25 </cmp-field>
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 8/32
8 CHAPITRE 9. PREMIERS PAS J2EE
< c m p - f i e l d >
<field-name>email </field-name>
</cmp-field>
< c m p - f i e l d >
30 <field-name >phone </field- name>
</cmp-field>
<primkey -field>id </primkey -field>
< q u e r y >
<query- method >
35 <method -name> findByNom </method -name>
<method -params >
<method -param> java.lang. String </method -param>
</method -params >
</query-method >
40 < ej b - ql > S E L EC T O B JE C T ( o) F RO M c a r te d e vi s i te o
W HE RE o . no m = ? 1
</ejb-ql></query>
< q u e r y >
45 <query- method >
<method -name> findByPrefix </method -name>
<method -params >
<method -param> java.lang. String </method -param>
</method -params >
50 </query-method >
< ej b - ql > S E L EC T O B JE C T ( o) F RO M c a r te d e vi s i te o
W H ER E L O CA T E (? 1 , o . no m ) > 0
</ejb-ql>
</query>
55 < q u e r y >
<query- method >
<method -name> findByPrimaryKey </method -name>
<method -params >
<method -param> java.lang. Integer </method -param >
60 </method -params >
</query-method >
< ej b - ql > S E L EC T O B JE C T ( o) F RO M c a r te d e vi s i te o
W HE RE o . id = ? 1
</ejb-ql>
65 </query>
</entity >
</enterprise -beans >
< assembly -descrip tor >70 <contain er -tr ansaction >
<method >
<ejb-name>CarteDeVisiteCMP </ejb-name>
<method -name >*</ method -name >
</method >
75 <trans- attribute >Required </trans- attribute >
</container -transaction >
</ assembly -descrip tor >
</ejb-jar>
Listing 9.8 – Fichier ejb-jar.xml
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 9/32
9.2. GESTION DE LA PERSISTANCE - ENTITY BEAN 9
Descripteur Jonas : En complement du descripteur J2EE, chaque fournisseur de plate-forme propose son propre descripteur afin de completer la description initiale. Dans Jonas,
il permet de decrire l’utilisation des ressources tierces comme par exemple l’acces a la basede donnees.
1 < ? x ml v e r s io n = " 1 . 0 " e n c o d i n g = " I S O - 8 8 59 - 1 " ? >
< j o n a s - e j b - j a r >
<jonas- entity >
<ejb-name>CarteDeVisiteCMP </ejb-name>
<jndi-name>MyCarteDeVisiteCMP </jndi-name>
6 <jdbc-mapping >
<jndi-name>jdbc_1 </jndi-name>
<jdbc-table-name>CARTEDEVISITE </jdbc-table-name>
<automatic -pk>true </automatic -pk>
<cmp-field -jdbc- mapping >
11 < f i e l d - n a m e > i d < / f i e l d - n a m e >
< j d b c - f i e l d - n a m e > I D < / j d b c - f i e l d - n a m e >
</cmp -field- jdbc-mapping >
<cmp-field -jdbc- mapping >
< f i e l d - n a m e > n o m < / f i e l d - n a m e >
16 < j d b c - f i e l d - n a m e > N O M < / j d b c - f i e l d - n a m e >
</cmp -field- jdbc-mapping >
<cmp-field -jdbc- mapping >
<field-name>prenom </field-name>
<jdbc-field-name>PRENOM </jdbc-field-name>
21 </cmp -field- jdbc-mapping >
<cmp-field -jdbc- mapping >
<field-name>phone </field-name>
<jdbc-field-name>PHONE </jdbc-field-name>
</cmp -field- jdbc-mapping >26 <cmp-field -jdbc- mapping >
<field-name>email </field-name>
<jdbc-field-name>EMAIL </jdbc-field-name>
</cmp -field- jdbc-mapping >
</jdbc -mapping >
31 </jonas -entity >
< / j o n a s - e j b - j a r >
Listing 9.9 – Fichier jonas-ejb-jar.xml
9.2.3 Compilation et deploiement de l’entity bean
Nous avons maintenant tout ce qui est necessaire pour compiler et deployer l’entity
bean . A l’issue de cette premiere etape nous avons l’arborescence suivante :
+ - - > b in /
+ - - > M ET A - I N F /
3 + - - > e jb - j a r . x m l
+ - - > j o na s - e j b - j a r . x m l
+ - - > s rc /
+ - - > C a r t e D e V i si t e C M P B e a n . j a v a
+ - - > C a r t e D e V i si t e C M P H o m e . j a v a
8 + - - > C a r t e D e Vi s i t e C M P . j a v a
+ - - > T e s t . j a va
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 10/32
10 CHAPITRE 9. PREMIERS PAS J2EE
Nous conserverons cette arborescence pour les autres beans que nous creerons. Lerepertoire bean contiendra toujours le code compile des fichiers java et le repertoire
bin/META-INF, les descripteurs.Pour pouvoir etre execute par Jonas, un bean a besoin d’etre contenu dans une archive
qui facilite son deploiement. Pour cela, on genere le bytecode, puis on ecrit les descripteurde deploiement et on archive l’ensemble dans un fichier jar.
1 L I N UX : ~ / C a r t e D e V is i t e > j a v a c - d b i n s r c / * . j a va
L I N UX : ~ / C a r t e D e V is i t e > c p * . x m l b i n / M ET A - I N F / .
LINUX:~/ CarteDeVisite > cd bi n
L I N UX : ~ / C a r t e D e V i s i t e / b in > j a r c f . . / C a r t e D e V i si t e C M P . j a r
L I N U X : ~ / C a r t e D e V i s i t e / b i n > cd ..
6 LINUX:~/ CarteDeVisite >
Deployer un bean dans Jonas se fait en deux etapes :
– deposer l’archive dans le repertoire ejbjars de Jonas,– utiliser l’outils d’administration de Jonas pour publier le bean.
L I N UX : ~ / C a r t e D e V is i t e > c p C a r t e D e V is i t e C M P . j a r $ J O N A S _R O O T / e j b j a r s / .
LINUX:~/ CarteDeVisite > cd $JONAS_ROOT/ejbjars
L I N UX : / o p t / J O N A S _ 4 _ 7 _ 7 / e j bj a r s > j o n as a d m in - a C a r t e D e V is i t e C M P . j a r
4 LINUX:/opt/ JONAS_4_7_7/ ejbjars > cd -
LINUX:~/ CarteDeVisite >
9.2.4 Client d’un entity bean
Ce client, non essentiel a l’application, permet de valider le code de l’entity bean. Nous
faisons le choix d’implementer un client simple qui cree deux entity bean, effectue unerecherche par nom et une recherche a l’aide d’un prefixe.
package carte_cmp;
import j a v a . u t i l . P r o p e r t i e s ;
import j a v a x . n a m i n g . I n i t i a l C o n t e x t ;
5 import j a v a x . n a m i n g . C o n t e x t ;
import j a v a x . r mi . P o r t a b l e R e m o t e O b j ec t ;
import j a v a . u t i l . C o l l e c t i o n ;
import j a v a . u t i l . I t e r a t o r ;
10
p u b l ic c l a s s T es t {
p u bl i c s t at i c v o id m a in ( S t r in g a r gs [ ] ) {
15 tr y {
/ / R e ch e rc h e d e l ’ i n t e r fa c e h o me d e l ’ E J B
C on te xt i n it ia l Co n te x t = ne w InitialContext();
O b je c t o b jr e f = i n i ti a l Co n t ex t . l o ok u p (" M y C a r t e D e V i s i t e C M P ");
20
/ / R e fe r en c e a l ’ i n t er f ac e l o ca l e d e l ’ E J B
C a r t e D e V i s it e C M P H o m e h o m e = ( C a r t e D e V i si t e C M P H o m e )
P o r t a b l e R e mo t e O b j e c t . n a r r ow ( o b j r ef , C a r t e D e V i s it e C M P H o m e . class );
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 11/32
9.2. GESTION DE LA PERSISTANCE - ENTITY BEAN 11
25 / / Cr ea ti on d eu x ca rt e de v is it e da ns la b as e de d on ne es
C a r te D e Vi s i te C M P t o to =
h o m e . c r e a t e (" T O T O " , " t o t o " , " t o t o @ t o t o " , " 0 1 2 3 4 5 6 " );
C a r te D e Vi s i te C M P t i ti =
h o m e . c r e a t e (" T I T I " , " t i t i " , " t i t i @ t i t i " , " 9 8 7 6 4 4 3 " );
30
/ / O bj et d e t yp e c li en t p ou r f ai re n os t es ts
C a r t e D e V is i t e C M P t e s t ;
35 / / R ec he rc h e p ar l e n om d e l a p er so nn e
S y s t e m . o u t . p r i n t l n (" L i s t e d e s c l i e n t s q u i o n t p o u r n o m T O T O : ");
C o ll e c ti o n c l = h o me . f i n d By N om ( " T O T O " );
I t er a to r i L as t = c l . i te r at o r ( );
while ( i L a st . h a s N e x t ( ) ) {
40 t es t = i L as t . n ex t ( );
S y s t e m . o u t . p r i n t l n (" - > " + t e s t . g e t P r en o m ( ) ) ;}
/ / R ec he rc h e p ar l e p re fi xe d e l a p er so nn e
45 S y s t e m . o u t . p r i n t l n (" L i s t e d e s c l i e n t s q u i o n t p o u r p r e f i x e T : ");
C o ll e c ti o n c 2 = h o me . f i n d By P r ef i x ("T " );
I t er a to r i L as t 1 = c 2 . i te r at o r ( );
while ( i L a s t1 . h a s N e x t ( ) ) {
t es t = i L as t 1 . n ex t ( );
50 S y s t e m . o u t . p r i n t l n ( " - > " + t e s t . g e t P r en o m ( ) ) ;
}
} catch ( E x ce pt i on e ) {
S y s t e m . e r r . p r i n t l n (" E r r e u r : " + e );
55 System.exit(2);
}
}
}
Listing 9.10 – Fichier Test.java
9.2.5 Compilation et execution du client
Apres avoir compile le client, nous utilisons pour l’executer, l’utilitaire jclient quipermet l’execution du client dans le contexte de Jonas et d’avoir ainsi l’acces a l’annuaire
de referencement JNDI dans lequel sont enregistres tous les beans publies dans Jonas.Nous considererons que le client possede dans son CLASSPATH le jar du bean, pour pou-
voir utiliser le proxy (classe RemoteStub generee par genic) permettant d’acceder au beancomme un objet distant. Pour ’travailler proprement’, il serait beaucoup plus judicieux dedonner au client soit le proxy, soit une maniere de le generer.
1 L I N UX : ~ / C a r t e D e V is i t e > j c l i en t \
- c p $ J O N A S _R O O T / e j b j a r s / C a r t e D e V i si t e C M P . j a r \
- a r g c l i e nt c a r t e _c m p . T e s t
C l i e n t C on t a i n e r . i n f o : S t a r ti n g c l i e nt . . .
L is te d es c li en ts q ui o nt p ou r n om T OT O :
6 - > t ot o
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 12/32
12 CHAPITRE 9. PREMIERS PAS J2EE
L is te d es c li en ts q ui o nt p ou r p re fi xe T :
- > t o to
- > t i ti
9.2.6 Automatisation des taches repetitives
La compilation, le deployement et la publication d’un bean, ainsi que la compilationet l’execution d’un client sont des taches repetitives qu’il est aise d’automatiser. Voici unexemple de script bash qui rendra la suite de l’ennonce beaucoup plus lisible. Ce scriptpermet :
– de compiler et generer l’archive d’un bean (compile),– de le deployer et de le publier (load),– de compiler et d’executer un client (run),
– de retirer le bean publie (unload),– et de nettoyer les sources (clean).
TODO valider le script pour tcsh
1 # ! / b i n / s h
# A TT EN TI ON : f on c ti on ne e n b as h , z sh
J A R _ N A M E = C a r t e D e V i s i t e C M P . j a r
6 C L I E N T _ C L A S S _ N A M E = c a r t e _ c m p . T e s t
# ## D O N O T E DI T A FT ER T HI S L IN E ! ! # # #
export J A R _ N A ME C L I E N T _ C LA S S _ N A M E
11 function c o m p il e ( ) {
echo " # # s r c - - > b i n c o m p i l a t i o n "
j a va c - d b in s rc / * . j av a
echo " # # D e p l o y i n g x m l d e s c r i p t o r s "
c p * . x ml b in / M ET A - I N F /.
16 echo " # # B u i l d i n g $ J A R _ N A M E "
cd bi n
j ar c f . ./ $ J A R _N A ME *
cd ..
}
21
function l o ad ( ) {
echo " # # M o v i n g $ J A R _ N AM E - - > $ J O N A S _ R O O T / e j b j ar s "
mv $JAR_NAME $ JONAS_ ROOT / ejbjar s /.echo " # # L o a d i n g $ J A R _ N A M E i n J O N A S "
26 cd $JONAS_ROOT/ejbjars
j o na s a d mi n - a $ J AR _ NA M E
cd -
}
31 function u n l oa d ( ) {
echo " # # U n l o a d i n g $ J A R _ N A M E f r o m J O N A S "
cd $JONAS_ROOT/ejbjars
j o na s a d mi n - r $ J AR _ NA M E
cd -
36 }
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 13/32
9.2. GESTION DE LA PERSISTANCE - ENTITY BEAN 13
function r un ( ) {
echo " # # R u n n n i n g c l i e n t . . . "
j c l i e nt - c p $ J O N A S _R O O T / e j b j a r s / $ J A R _ N AM E - a r g c l i e nt $ C L I E N T _ CL A S S _ N A M E
41 }
function c l e an ( ) {
echo " # # C l e a n i n g d i r e c t o r i e s "
r m - rf * ~ * . ja r s rc / *~ b in / *
46 m k d ir b i n / M ET A - I N F
}
function m a in ( ) {
case $ 1 i n
51 " c o m p i l e ") c o mp i le ; ;
" l o a d ") load ;;
" u n l o a d ") u nl oa d ;;" r u n ") run ;;
" c l e a n ") clean ;;
56 " a l l ") un lo an d; c le an ; co mp il e; loa d; run ;;
*) " U n k n o w n C o m m a n d : [ $ 1 ] " ;;
esac
}
61 m ai n $ @
Listing 9.11 – Fichier manage.sh
La gestion d’un bean devient simple. Il suffit de modifier l’entete pour indique le nom
du bean et le nom du client et d’executer ce script en precisant l’action a effectuer.
L I N UX : ~ / C a r t e D e V is i t e > . / m a n a g e . s h c l e a n
# # C l ea n in g d i re c t or i es
L I N UX : ~ / C a r t e D e V is i t e > . / m a n a g e . s h c o m p il e
4 # # s r c - - > b in c o mp i l at i on
# # D e pl o yi n g x ml d e s cr i pt o r s
# # B u i l di n g C a r t e D e Vi s i t e C M P . j a r
L I N UX : ~ / C a r t e D e V is i t e > . / m a n a g e . s h l o a d
# # M o v i ng C a r t e D e V is i t e C M P . j a r - - > / o p t / J O N A S _ 4 _ 7_ 7 / e j b j a r s
9 # # L o ad i ng C a r te D e Vi s i te C M P . ja r i n J O NA S
L I N UX : ~ / C a r t e D e V is i t e > . / m a n a g e . s h r u n
# # R un nn in g c l ie nt . ..
C l i e n t C on t a i n e r . i n f o : S t a r ti n g c l i e nt . . .
L is te d es c li en ts q ui o nt p ou r n om T OT O :
14 ->toto
->toto
L is te d es c li en ts q ui o nt p ou r p re fi xe T :
->toto
->titi
19 ->toto
->titi
L I N UX : ~ / C a r t e D e V is i t e > . / m a n a g e . s h u n l o ad
# # U n lo a di n g C a r te D e Vi s i te C M P . ja r f r om J O NA S
Note : Nous utiliserons systematiquement ce script par la suite.
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 14/32
14 CHAPITRE 9. PREMIERS PAS J2EE
9.3 La partie metier - statefull session bean
9.3.1 PrincipeDans le cadre d’une architecture n− tiers, il n’est pas pensable d’offrir aux differents
clients la possibilite de manipuler directement les entity beans qui representent des donneesdans la base de donnees. En effet, les acces sont trop elementaires. Nous allons encapsulerla logique metier de l’application dans un session bean.
Les operations d’ajout et de supression de cartes etant critiques nous allons unique-ment les autoriser pour l’administrateur de l’application a l’aide d’un bean de session aetat (statefull session bean ), dans lequel on positionnera un etat estIdentifie . L’executiondes actions d’ajout et de suppression sera autorisee uniquement pour les utilisateurs au-thentifies.
9.3.2 Ecriture du statefull session bean
Comme pour un entity bean, l’ecriture d’un session bean demande l’ecriture de deuxinterfaces (l’interface remote et l’interface home), d’une classe java implementant le beanet de deux descripteurs de configuration (celui present dans toutes plates-formes J2EE etcelui de Jonas).
Interface remote
package a d m i n i s t r a t o r s ;
3 import j a v a x . e j b . E J B O b j e c t ;
import j a v a . r m i . R e m o t e E x c e p t i o n ;
p u b l ic i n t e r fa c e Administrator extends E J B O bj e ct {
/ / L og i nt o t he b ea n
8 p u b l ic b o o l ea n l o g i n ( S t r i n g u s e rn a m e , S t r i ng p a s s w or d )
throws R e m o t e E x c e p t i o n ;
// L og o ut f r om t he b e an
p u b l ic b o o l ea n logout() throws R e m o t e E x c e p t i o n ;
13
/ / A dd a ’ C a r te D e Vi s i te ’ i n si d e t he s y st e m
p u b l ic b o o l ea n a dd ( S t ri n g n om , S t ri n g p re no m ,
S t ri n g m ai l , S t ri n g p h on e )throws R e m o t e E x c e p t i o n ;
18
/ / D el et e a c ar d , u si ng t he i d t o r et ri ev e t he g oo d o ne
p u b l ic b o o l ea n d e le t e ( in t id ) throws R e m o t e E x c e p t i o n ;
}
Listing 9.12 – Fichier Administrator.java
Interface home
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 15/32
9.3. LA PARTIE M ETIER - STATEFULL SESSION BEAN 15
package a d m i n i s t r a t o r s ;
3 import j a v a . r m i . R e m o t e E x c e p t i o n ;import j a v a x . e j b . C r e a t e E x c e p t i o n ;
import j a v a x . e j b . E J B H o m e ;
p u b l ic i n t e r fa c e AdministratorHome extends E J B Ho m e {
8 / / C o n st r uc t e ur p ar d e fa u t
A d m i n i st r a t o r c r e a te ( ) throws R e m o te E x c e pt i o n , C r e a t e E x ce p t i o n ;
/ / P er me t d e s e l og ge r d ir e ct em en t a l a c re at io n
A d m i n i st r a t o r c r e a te ( S t r i n g u s e rn a m e , S t r i ng p a s s w or d )
13 throws R e m o te E x c e pt i o n , C r e a t e E xc e p t i o n ;
}
Listing 9.13 – Fichier AdministratorHome.java
Implementation du session bean
package a d m i n i s t r a t o r s ;
import j a v a . r m i . R e m o t e E x c e p t i o n ;
import j a v a x . e j b . S e s s i o n B e a n ;
5 import j a v a x . e j b . S e s s i o n C o n t e x t ;
import javax.rmi.*;
import j a v a x . n a m i n g . I n i t i a l C o n t e x t ;import j a v a x . n a m i n g . C o n t e x t ;
10
import java.util.*;
import carte_cmp.*;
15 p u b l ic c l a s s AdministratorBean implements S e s s i on B e a n {
p r i v a te b o o l e an i s L o g g e d ;
p u b l ic b o o l ea n l o g i n ( S t r i ng u s e rn a m e , S t r i ng p a s s w or d ) {
20 if ( u s e r n a m e . e q u a l s ( " a d m i n ") & & p a ss w or d . e q ua l s (" a d m i n ")) {
this . i s L og g ed = true ;
r e t u rn t r u e;}
r e t u rn f a l s e;
25 }
p u b l ic b o o l ea n l o g ou t ( ) {
this . i s L og g ed = false ;
r e t u rn t r u e;
30 }
p u b l ic b o o l ea n a d d ( S t ri n g n om , S t ri n g p re no m , S t ri n g m ai l , S t ri n g p h on e ) {
tr y {
if ( i s L og g ed ) {
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 16/32
16 CHAPITRE 9. PREMIERS PAS J2EE
35 C o nt e xt i n i ti a l Co n t ex t = ne w InitialContext();
O b je c t o b jr e f = i n i ti a l Co n t ex t . l o o ku p (" M y C a r t e D e V i s i t e C M P ");
/ / R e fe r en c e a l ’ i n t er f ac e l o ca l e d e l ’ E J B
C a r t e D e V i s it e C M P H o m e h o m e = ( C a r t e D e V i si t e C M P H o m e )
40 PortableRemot eObject .narrow( objref ,
C a r t e D e V i s i t e C M P H o m e .class );
/ / O b je t d e t y pe C a rt e De V is i te , a v ec c r ea t io n d e c et o b je t
C a r te D e Vi s i te C M P t e st = h o me . c r e at e ( n om , p re no m , m ai l , p h on e ) ;
45
r e t u rn t r u e;
} e l se r e tu r n f a ls e ;
}
catch ( E x ce pt io n e ) {
50 S y s t e m . e r r . p r i n t l n ( " E r r e u r : " + e ) ;
}r e t u rn f a l s e;
}
55 p u b l ic b o o l ea n d e le t e ( in t i d) {
tr y {
if ( i s L og g ed ) {
C o nt e xt i n i ti a l Co n t ex t = ne w InitialContext();
O b je c t o b jr e f = i n i ti a l Co n t ex t . l o o ku p (" M y C a r t e D e V i s i t e C M P ");
60
/ / R e fe r en c e a l ’ i n t er f ac e l o ca l e d e l ’ E J B
C a r t e D e V i si t e C M P H o m e h o m e = ( C a r t e D e V i si t e C M P H o m e )
PortableRemoteObject.narrow(objref ,
C a r t e D e V i s i t e C M P H o m e .class );
65
/ / O b je t d e t y pe C a rt e De V is i te , e n v ue d e l e s u pp r im e r
/ / d e l a t a b le
C a r t e D e V is i t e C M P t e s t = h o m e . f i n d B y P r im a r y K e y ( i d ) ;
if ( t e st ! = null ){
70 test.remove();
r e t u rn t r u e;
} e l se r e tu r n f a ls e ;
} e l se r e tu r n f a ls e ;
}
75 catch ( E x ce pt io n e ) {
S y s t e m . e r r . p r i n t l n (" E r r e u r : " + e ) ;
}
r e t u rn f a l s e;}
80
p u b l ic v o i d e jb Cr ea te ( ) { this . i s L og i n = false ; }
p u b l ic v o i d e j b C r e a te ( S t r i n g u s e rn a m e , S t r in g p a s s w or d ) {
if ( u s e r n a m e . e q u a l s ( " a d m i n ") & & p a ss w or d . e q ua l s (" a d m i n "))
85 this . i s L og i n = true ;
else
this . i s L og i n = false ;
}
90 p u b l ic v o i d e j b R e mo v e ( ) { }
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 17/32
9.3. LA PARTIE M ETIER - STATEFULL SESSION BEAN 17
p u b l ic v o i d e j b A c t iv a t e ( ) { }
p u b l ic v o i d e j b P a s si v a t e ( ) { }
p u b l ic v o i d s e t S e s si o n C o n t e x t ( S e s s i o n C on t e x t s c ) { }
95 }
Listing 9.14 – Fichier AdministratorBean.java
Descripteur J2EE
< ? x ml v e r s io n = " 1 . 0 " e n c o d in g = " I S O - 8 8 5 9 - 1 "? >
< e j b - j a r >
< d e s c r ip t i o n > D e s c r i p t e ur d e d e p l o i em e n t d u b e a n a d m i n i s tr a t i f
</description >
5 <display -name> Administrator </display -name><enterprise -beans >
<session >
<description >Administrator </description >
<display -name> Administrator </display -name>
10 <ejb-name >Administrator </ejb-name >
<home>administrators.AdministratorHome </home>
<remote >admin istrators .Administrator </remote >
<ejb-class>administrators.AdministratorBean </ejb-class>
<session -type >Stateful </session -type>
15 <transaction -type> Container </transaction -type>
</session >
</enterprise -beans >
< assembly -descrip tor >
<container -transaction >
20 <method >
<ejb-name>Administrator </ejb-name>
<method -name >*</ method -name >
</method >
<trans- attribute >Required </trans- attribute >
25 </contain er -tra nsaction >
</ assembly - descriptor >
</ejb-jar>
Listing 9.15 – Fichier ejb-jar.xml
Descripteur Jonas
< ? x ml v e r s io n = " 1 . 0 " e n c o d in g = " I S O - 8 8 5 9 - 1 "? >
< j o n a s - e j b - j a r >
3 <jonas- session >
<ejb-name>Administrator </ejb-name>
<jndi-name>MyAdministrator </jndi-name>
</jonas -session >
< / j o n a s - e j b - j a r >
Listing 9.16 – Fichier jonas-ejb-jar.xml
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 18/32
18 CHAPITRE 9. PREMIERS PAS J2EE
9.3.3 Ecriture d’un client du session bean
Comme pour l’entity bean, nous allons construire un client capable de se connecter ausession bean precedemment decrit.
package a d m i n i s t r a t o r s ;
3 import j a v a . u t i l . P r o p e r t i e s ;
import j a v a x . n a m i n g . I n i t i a l C o n t e x t ;
import j a v a x . n a m i n g . C o n t e x t ;
import j a v a x . r mi . P o r t a b l e R e m o t e O b j ec t ;
8 import j a v a . i o . B u f f e r e d R e a d e r ;
import j a v a . i o . I n p u t S t r e a m R e a d e r ;
import j a v a . i o . I O E x c e p t i o n ;
import j a v a . i o . O u t p u t S t r e a m W r i t e r ;
import j a v a . i o . P r i n t W r i t e r ;13
import java.util.*;
import carte_cmp.*;
18 p u b l ic c l a s s A d m i n i s t ra t o r C l i e n t {
p u bl i c s t at i c v o id m a in ( S t r in g a r gs [ ] ) {
tr y {
23
I n p ut S t re a m Re a d er i sr = ne w I n p u t St r e am R e ad e r ( S y st e m . in ) ;
B u f fe r e dR e a de r b r = ne w B u f fe r e dR e a de r ( i sr ) ;boolean i s Qu i t = false ;
/ / R e ch e rc h e d e l ’ i n t e rf a ce h o me d e l ’ E J B
28 C on te xt i ni ti al Co nt ex t = ne w InitialContext();
O b je c t o b jr e f = i n it i a lC o n te x t . l o ok u p (" M y A d m i n i s t r a t o r ");
/ / R e fe r en c e a l ’ i n te r f ac e l o ca l e d e l ’ E JB
A d m in i s tr a t or H o me h o me = ( A d m i ni s t ra t o rH o m e )
33 PortableRemote Object .narrow( objref ,
A d m i n i s t r a t o r H o m e .class );
/ / C r ea t io n d ’ u n a d mi n i st r a te u r
A d m i n i st r a t o r m y A d m i n i st r a t o r = h o m e . c r e at e ( ) ;
38 while ( i s Q ui t = = false ) {
S y s t e m . o u t . p r i n t l n (" 1 . C o n n e x i o n ");S y s t e m . o u t . p r i n t l n (" 2 . Q u i t t e r ");
S y s t e m . o u t . p r i n t l n (" >" );
S t ri n g p r i nc i p al C ho i c e = null ;
43 p r i nc i p al C h oi c e = b r . r ea d Li n e ( );
if ( p r i n c i p a l C h o i c e . e q u a l s ( " 2" )) {
i s Qu i t = true ;
S y s t e m . o u t . p r i n t l n (" B y e b y e . . . ");
48 }
e ls e i f ( p r i n c i p a l C h o i c e . e q u a l s ( "1 " )) {
S y s t e m . o u t . p r i n t l n (" U s e r n a m e : ");
S t ri n g u s er n am e = null ;
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 19/32
9.3. LA PARTIE M ETIER - STATEFULL SESSION BEAN 19
u s er n am e = b r . r ea d Li n e ( );
53
S y s t e m . o u t . p r i n t l n (" P a s s w o r d : ");
S t ri n g p a ss w or d = null ;
p a ss w or d = b r . r ea d Li n e ( );
if ( m y A d m i n i s t r a to r . l o g i n ( u s e r na m e , p a s s w or d ) ) {
58 boolean i s Lo g = true ;
while ( i s Lo g = = true ){
S y s t e m . o u t . p r i n t l n (" 1 . A j o u t e r u n e c a r t e "
+ " d e v i s i t e ");
S y s t e m . o u t . p r i n t l n (" 2 . R e t i r e r u n e c a r t e "
63 + " d e v i s i t e ");
S y s t e m . o u t . p r i n t l n (" 3 . S e d e c o n n e c t e r ");
S y s t e m . o u t . p r i n t l n (" >" );
S t ri n g s e c on d a ry C ho i c e = null ;
s e c on d a ry C h oi c e = b r . r ea d Li n e ( );
68 if ( s e c o n d a r y C h o i c e . e q u a l s ( " 1" )) {S y s t e m . o u t . p r i n t l n (" A j o u t d ’ u n e "
+ " c a r t e d e v i s i t e . . . ");
S y s t e m . o u t . p r i n t l n (" E n t r e r l e n o m : ");
S tr in g n om = null ;
73 n om = b r. r ea dL in e ( );
S y s t e m . o u t . p r i n t l n (" E n t r e r l e p r e n o m : ");
S t ri n g p r en o m = null ;
p r en o m = b r . r ea d Li n e ( );
S y s t e m . o u t . p r i n t l n (" E n t r e r l e m a i l : ");
78 S tr in g m ai l = null ;
ma il = br . readLine () ;
S y s t e m . o u t . p r i n t l n (" E n t r e r l e n u m e r o d e "
+ " t e l : ");
S t ri n g p h on e = null ;
83 p h on e = b r . r ea d Li n e ( );
boolean i s Ad d = m y A d m i n is t r a t o r . a d d ( n om ,
prenom ,
ma il ,
phone);
88 if ( i s A d d ) {
S y s t e m . o u t . p r i n t l n (" A j o u t r e u s s i . . . ");
}
else {
S y s t e m . o u t . p r i n t l n (" A j o u t e c h o u e . . . ");
93 }
}
e ls e i f ( s e c o n d a r y C h o i c e . e q u a l s ( "2 " )) {S y s t e m . o u t . p r i n t l n (" S u p p r e s s i o n d ’ u n e "
+ " c a r t e d e v i s i t e . . . ");
98 S y s t e m . o u t . p r i n t l n ( " E n t r e r l ’ I D : ");
S tr in g i d = null ;
i d = b r. r ea dL in e ( );
in t i d T o De l e t e = I n t e ge r . p a r s e I n t ( i d ) ;
boolean i s D el e te =
103 m y A d m i n i s t r a t o r . d e l e t e ( i d T o D e l e t e ) ;
if ( i s D e l e t e ) {
S y s t e m . o u t . p r i n t l n (" S u p p r e s s i o n "
+ " r e u s s i e . . . ");
}
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 20/32
20 CHAPITRE 9. PREMIERS PAS J2EE
108 else {
S y s t e m . o u t . p r i n t l n (" S u p r e s s i o n "
+ " e c h o u e e . . . ");
}
}
113 e ls e i f ( s e c o n d a r y C h o i c e . e q u a l s ( "3 " )) {
myA dmi nis trat or . logo ut () ;
i s Lo g = false ;
S y s t e m . o u t . p r i n t l n (" L o g o u t . . . ");
}
118 else {
S y s t e m . o u t . p r i n t l n (" C h o i x i n c o r r e c t . . . ");
}
}
}
123 else {
S y s t e m . o u t . p r i n t l n (" U s e r n a m e o u P a s s w o r d "+ " i n c o r r e c t . . . ");
}
}
128 else {
S y s t e m . o u t . p r i n t l n (" C h o i x i n c o r r e c t . . . ");
}
}
} catch ( E x ce pt i on e ) {
133 S y s t e m . e r r . p r i n t l n ( " E r r e u r : " + e ) ;
System.exit(2);
}
}
}
Listing 9.17 – Fichier AdministratorClient.java
9.3.4 Compilation du bean et du client, deploiement du bean etexecution du client
Il est essentiel que le repertoire du session bean connaisse les interfaces de l’entitybean qu’il va acceder. Il est donc necessaire de copier les interfaces home et remote del’entity bean (CarteDeVisiteCMPHome.java et CarteDeVisiteCMP.java) dans le repertoiresrc du session bean avec l’ensemble du package2.
Il reste aussi a copier le script associe a l’entity bean ( manage.sh) dans le repertoire
du session bean et de le mettre a jour avec les modifications suivantes :
J A R _ N A M E = A d m i n i s t r a t o r . j a r
C L I E N T _ C LA S S _ N A M E = a d m i n i s t r at o r s . A d m i n i s t r a t o r C l ie n t
L’execution du client doit etre parametree de la maniere suivante :
j c l i e nt - c p / o p t / J O N A S _ 4 _ 7_ 7 / e j b j a r s / $ J A R _ N A M E : \
$ J O N A S _R O O T / e j b j a r s / C a r t e D e V i si t e C M P . j a r \
3 - a r g c l i e nt $ C L I E N T _ C LA S S _ N A M E
2Une solution plus eleguante, laisxssee au lecteur en exercice, serait d’implementer un systeme de char-
gement de classe dynamique qui telechargerait ces classes depuis le serveur Jonas a l’execution.
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 21/32
9.3. LA PARTIE M ETIER - STATEFULL SESSION BEAN 21
Il ne reste plus qu’a utiliser le script pour compiler le bean et le client, deployer le beanpuis execution le client.
L I N UX : ~ / A d m i n i s t ra t o r > . / m a n a g e . s h c o m p il e
2 # # s r c - - > b in c o mp i l at i on
# # D e pl o yi n g x ml d e s cr i pt o r s
# # B u i l di n g A d m i n i st r a t o r . j a r
LINUX:~/ Administrator >
L I N UX : ~ / A d m i n i s t ra t o r > . / m a n a g e . s h l o a d
7 # # M o v i n g A d m i n i s tr a t o r . j a r - - > / o p t / J O N A S _ 4 _ 7 _ 7 / e j b j a r s
# # L o ad i ng A d m in i s tr a t or . j a r i n J O NA S
L I N UX : ~ / A d m i n i s t ra t o r > . / m a n a g e . s h r u n
# # R un nn in g c l ie nt . ..
C l i e n t C on t a i n e r . i n f o : S t a r ti n g c l i e nt . . .
12 1 . C o n n e x io n
2 . Q u i t te r
>toto
C h o ix i n c o r re c t . . .
17 1 . C o n n e x io n
2 . Q u i t te r
>
1
U s er n am e :
22 toto
P a ss w or d :
titi
U s er n am e o u P a ss w or d i n co r r ec t . . .
1 . C o n n e xi o n
27
2 . Q u i t t er>
1
U s er n am e :
admin
32 P a ss w or d :
admin
1 . A jo ut er u ne c ar te d e v is it e
2 . R et ir er u ne c ar te d e v is it e
3 . S e d e c on n ec t e r
37 >
1
A j ou t d ’ u ne c a rt e d e v i si t e . ..
E nt re r l e n om :
MOSSER42 E nt re r l e p re no m :
sebastien
E nt re r l e m ai l :
mos ser @po lyte ch . uni ce . fr
E nt re r l e n um er o d e t e l :
47 01234566
A j o ut r e u s si . . .
1 . A jo ut er u ne c ar te d e v is it e
2 . R et ir er u ne c ar te d e v is it e
3 . S e d e c on n ec t e r
52 >
1
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 22/32
22 CHAPITRE 9. PREMIERS PAS J2EE
A j ou t d ’ u ne c a rt e d e v i si t e . ..
E nt re r l e n om :
JOFFROY
57 E nt re r l e p re no m :
Cedric
E nt re r l e m ai l :
j o f f r o y @ p o l y t e c h . u n i c e . f r
E nt re r l e n um er o d e t e l :
62 987654321
A j o ut r e u s si . . .
1 . A jo ut er u ne c ar te d e v is it e
2 . R et ir er u ne c ar te d e v is it e
3 . S e d e c on n ec t e r
67 >
3
Logout...
1 . C o n n e xi o n2 . Q u i t te r
72 >
2
B ye b ye . . .
9.4 La partie metier (suite) - stateless session bean
Apres avoir ecrit la partie metier qui sera utilise par l’administrateur de l’application,nous allons construire celle utilisee par tous les utilisateurs souhaitant consulter la base decartes de visite. La consultation d’une carte ne necessite pas de session, nous utiliserons
donc un stateless session bean.Comme tout bean, un stateless session bean a une remote interface, une home interface,
une implementation et deux descripteurs.
9.4.1 Ecriture du stateless session bean
Remote interface
1 package c o n s u l t a t o r s ;
import j a v a x . e j b . E J B O b j e c t ;
import j a v a . r m i . R e m o t e E x c e p t i o n ;
6 import java.util.*;
p u b l ic i n t e r fa c e Consultator extends E J B O bj e ct {
/ / F in d a c ar d l oo ki ng o n n am e
11 public C o l le c t i o n r e c h e r c he r N o m ( S t r i n g n o m )
throws R e m o t e E x c e p t i o n ;
/ / F i nd a c ar d w it h a p re fi x
public C o l l ec t i o n r e c h e r c h er P r e f i x ( S t r i n g p r e f ix )
16 throws R e m o t e E x c e p t i o n ;
}
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 23/32
9.4. LA PARTIE M ETIER (SUITE) - STATELESS SESSION BEAN 23
Listing 9.18 – Fichier Consultator.java
Home interface
package c o n s u l t a t o r s ;
3 import j a v a . r m i . R e m o t e E x c e p t i o n ;
import j a v a x . e j b . C r e a t e E x c e p t i o n ;
import j a v a x . e j b . E J B H o m e ;
p u b l ic i n t e r fa c e ConsultatorHome extends E J B Ho m e {
8
C o n s u l ta t o r c r e a te ( ) throws R e m o t eE x c e pt i o n , C r e a t e E xc e p t i o n ;
}
Listing 9.19 – Fichier ConsultatorHome.java
Implementation du bean
package c o n s u l t a t o r s ;
import j a v a . r m i . R e m o t e E x c e p t i o n ;
4 import j a v a x . e j b . S e s s i o n B e a n ;
import j a v a x . e j b . S e s s i o n C o n t e x t ;
import javax.rmi.*;
import j a v a x . n a m i n g . I n i t i a l C o n t e x t ;
9 import j a v a x . n a m i n g . C o n t e x t ;
import java.util.*;
import carte_cmp.*;
14
p u b l ic c l a s s ConsultatorBean implements S e s s io n B e a n {
public C o l l e ct i o n r e c he r c he r N om ( S t r in g n om ) {
tr y {
19 C on te xt i ni t ia lC o nt e xt = ne w InitialContext();O b je c t o b jr e f = i n i ti a l Co n t ex t . l o o ku p (" M y C a r t e D e V i s i t e C M P ");
/ / R e fe r en c e a l ’ i n te r f ac e l o ca l e d e l ’ E JB
C a r t e D e V i si t e C M P H o m e h o m e = ( C a r t e D e V i s i t e CM P H o m e )
24 PortableRemote Object .narrow( objref ,
C a r t e D e V i s i t e C M P H o m e .class );
/ / O bj et d e t yp e c li en t p ou r f ai re n os t es ts
C a r t e D e V is i t e C M P t e s t ;
29
/ / R ec he rc he p ar l e n om d e l a p er so nn e
C o ll e ct i o n c 1 = h om e . f i nd B y No m ( n om ) ;
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 24/32
24 CHAPITRE 9. PREMIERS PAS J2EE
return c1 ;
34 }
catch ( E x ce pt io n e ) {
S y s t e m . e r r . p r i n t l n (" E r r e u r : " + e ) ;
}
r e t u rn n u l l;
39 }
public C o ll e c t i o n r e c h e r c h er P r e f i x ( S t r i n g p r e f ix ) {
tr y {
C on te xt i ni t ia lC o nt e xt = ne w InitialContext();
44 O b je c t o b jr e f = i n i ti a l Co n t ex t . l o o ku p (" M y C a r t e D e V i s i t e C M P ");
/ / R e fe r en c e a l ’ i n te r f ac e l o ca l e d e l ’ E JB
C a r t e D e V i si t e C M P H o m e h o m e = ( C a r t e D e V i s i t e CM P H o m e )
PortableRemoteObject.narrow(objref ,49 C a r t e D e V i s i t e C M P H o m e . class );
/ / O bj et d e t yp e c li en t p ou r f ai re n os t es ts
C a r t e D e V is i t e C M P t e s t ;
54 / / R ec he rc he p ar l e p re fi x d e l a p er so nn e
C o l l e ct i o n c 1 = h o m e . f i n d B y Pr e f i x ( p r e f i x ) ;
return c1 ;
}
59 catch ( E x ce pt io n e ) {
S y s t e m . e r r . p r i n t l n (" E r r e u r : " + e ) ;
}
r e t u rn n u l l;
}
64
p u b l ic v o i d e j b C r ea t e ( ) { }
p u b l ic v o i d e j b R e mo v e ( ) { }
p u b l ic v o i d e j b A c t iv a t e ( ) { }
p u b l ic v o i d e j b P a s si v a t e ( ) { }
69 p u b l ic v o i d s e t S e s si o n C o n t e x t ( S e s s i o n C on t e x t s c ) { }
}
Listing 9.20 – Fichier ConsultatorBean.java
Descripteur J2EE
< ? x ml v e r s io n = " 1 . 0 " e n c o d in g = " I S O - 8 8 5 9 - 1 "? >
< e j b - j a r >
< d e s c r ip t i o n > D e s c r i p t e ur d e d e p l o i em e n t d u c o n s ul t a t or < / d e s c ri p t i on >
<display -name >Consultator </display -name>
5 <enterprise -beans>
<session >
<description >Consultator </description >
<display -name >Consultator </display -name>
<ejb-name>Consultator </ejb-name>
10 <home> consultators .ConsultatorHome </home>
<remote >consultators. Consultator </remote >
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 25/32
9.4. LA PARTIE M ETIER (SUITE) - STATELESS SESSION BEAN 25
<ejb-class>consultators.ConsultatorBean </ejb-class>
<session -type >Stateless </session -type >
<transaction -type >Container </transaction -type>
15 </session >
</enterprise -beans >
< assembly -descrip tor >
<container -transaction >
<method >
20 <ejb-name >Consultator </ejb-name >
<method -name >*</ method -name >
</method >
<trans- attribute >Required </trans- attribute >
</container -transaction >
25 </ assembly - descriptor >
</ejb-jar>
Listing 9.21 – Fichier ejb-jar.xml
Descripteur Jonas
< ? x ml v e r s io n = " 1 . 0 " e n c o d in g = " I S O - 8 8 5 9 - 1 "? >
< j o n a s - e j b - j a r >
<jonas- session >
4 <ejb-name> Consultator </ejb-name >
<jndi-name>MyConsultator </jndi-name>
</jonas -session >
< / j o n a s - e j b - j a r >
Listing 9.22 – Fichier jonas-ejb-jar.xml
9.4.2 Client d’un session bean
Nous commencons a etre rode... aucune difficulte particuliere.
package c o n s u l t a t o r s ;
3 import j a v a . u t i l . P r o p e r t i e s ;
import j a v a x . n a m i n g . I n i t i a l C o n t e x t ;
import j a v a x . n a m i n g . C o n t e x t ;
import j a v a x . r mi . P o r t a b l e R e m o t e O b j ec t ;
8 import j a v a . i o . B u f f e r e d R e a d e r ;import j a v a . i o . I n p u t S t r e a m R e a d e r ;
import j a v a . i o . I O E x c e p t i o n ;
import j a v a . i o . O u t p u t S t r e a m W r i t e r ;
import j a v a . i o . P r i n t W r i t e r ;
13
import java.util.*;
import carte_cmp.*;
p u b l ic c l a s s C o n s u l t at o r C l i e n t {
18
p u bl i c s t at i c v o id m a in ( S t r in g a r gs [ ] ) {
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 26/32
26 CHAPITRE 9. PREMIERS PAS J2EE
tr y {
23 / / R e ch e rc h e d e l ’ i n te r fa c e h o me d e l ’ E JB
C o nt e xt i n i ti a l Co n t ex t = ne w InitialContext();
O b je c t o b jr e f = i n it i a lC o n te x t . l o ok u p (" M y C o n s u l t a t o r ");
/ / R ef er en ce a l ’ EJ B
28 C o n su l t at o r Ho m e h om e = ( C o n s ul t a to r Ho m e )
P o r t a b l e R em o t e O b j e c t . n a r r ow ( o b j r ef , C o n s u l t at o r H o m e . class );
C o n s u l ta t o r m y C o n s ul t a t o r = h o m e . c r e at e ( ) ;
/ / O n v a c he rc he r d es g en s
33 I n p ut S t re a m Re a d er i sr = ne w I n p u t St r e am R e ad e r ( S y st e m . in ) ;
B u f fe r e dR e a de r b r = ne w B u f fe r e dR e a de r ( i sr ) ;
boolean i s Qu i t = false ;
38 while ( i s Q ui t = = false ){
S y s t e m . o u t . p r i n t l n (" 1 . R e c h e r c h e r p a r n o m ");
S y s t e m . o u t . p r i n t l n (" 2 . R e c h e r c h e r p a r p r e f i x e ( s u r l e n o m ) ");
S y s t e m . o u t . p r i n t l n (" 3 . Q u i t t e r ");
S y s t e m . o u t . p r i n t l n (" >" );
43
S t ri n g c h oi c e = null ;
c h oi c e = b r . r ea d Li n e ( );
if ( c h o i c e . e q u a l s ( " 1 " )) {
S y s t e m . o u t . p r i n t l n (" R e c h e r c h e p a r n o m . . . ");
48 S tr in g n om = null ;
S y s t e m . o u t . p r i n t l n (" E n t r e z l e n o m : ");
n om = b r . r ea d Li n e ( );
C a r te D e Vi s it e C MP t es t = null ;
C o l l e ct i o n c l = m y C o n s ul t a t o r . r e c h e r c h e r No m ( n o m ) ;
53 S y s t e m . o u t . p r i n t l n ( " r e c h e r c h e s u r l e n o m : "+nom);
if ( c l ! = null ){
I t er a to r i L as t = c l . i te r at o r ( );
while ( i L a st . h a s N e x t ( ) ) {
t es t = i L as t . n ex t ( );
58 S y s t e m . o u t . p r i n t l n ( " - > " + t e s t . g e t P r en o m ( ) ) ;
}
}
else {
S y s t e m . o u t . p r i n t l n (" E c h e c d e r e c h e r c h e . . . ");
63 }
}e ls e i f ( c h o i c e . e q u a l s ( " 2" )) {
S y s t e m . o u t . p r i n t l n (" R e c h e r c h e p a r p r e f i x e . . . ");
S t ri n g p r ef i x = null ;
68 S y s t e m . o u t . p r i n t l n ( " E n t r e z l e p r e f i x e : ");
p r ef i x = b r . r ea d Li n e ( );
C a r te D e Vi s it e C MP t es t = null ;
C o l l e ct i o n c 2 = m y C o n s ul t a t o r . r e c h e r c h e r P re f i x ( p r e f i x ) ;
S y s t e m . o u t . p r i n t l n (" r e c h e r c h e s u r l e p r e f i x : "+ p r e f i x ) ;
73 if ( c 2 ! = null ){
I t er a to r i L as t 2 = c 2 . i te r at o r ( );
while ( i L a st 2 . h a s N e x t ( ) ) {
t es t = i L as t 2 . n ex t ( );
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 27/32
9.4. LA PARTIE M ETIER (SUITE) - STATELESS SESSION BEAN 27
S y s t e m . o u t . p r i n t l n (" - > " + t e s t . g e t P r en o m ( ) ) ;
78 }
}
else {
S y s t e m . o u t . p r i n t l n (" E c h e c d e r e c h e r c h e . . . ");
}
83 }
e ls e i f ( c h o i c e . e q u a l s ( " 3" )) {
S y s t e m . o u t . p r i n t l n (" B y e b y e . . . ");
i s Qu i t = true ;
}
88 else {
S y s t e m . o u t . p r i n t l n (" C h o i x i n c o r r e c t . . . ");
}
}
} catch ( E x ce pt i on e ) {
93 S y s t e m . e r r . p r i n t l n ( " E r r e u r : " + e ) ;System.exit(2);
}
}
}
Listing 9.23 – Fichier ConsultatorClient.java
9.4.3 Compilation, deploiement et execution
A vous de jouer. Voici un exemple d’execution :
L I N UX : ~ / C o n s u l t at o r > . / m a n a ge . s h r u n# # R un nn in g c l ie nt . ..
3 C l i e n t C on t a i n e r . i n f o : S t a r t i n g c l i e n t . . .
1 . R e ch e rc h e r p ar n om
2 . R ec he rc h er p ar p re fi xe ( s ur l e n om )
3 . Q u i t te r
>
8 1
R e ch e rc h e p ar n om . . .
E nt re z l e n om :
moss er
r ec he rc he s ur l e n om : m os se r
13 ->sebastien
1 . R e ch e rc h e r p ar n om
2 . R ec he rc h er p ar p re fi xe ( s ur l e n om )3 . Q u i t te r
>
18 2
R e c h e rc h e p a r p r e f ix e . . .
E nt re z l e p re fi xe :
m
r ec he rc he s ur l e p re fi x : m
23 ->sebastien
1 . R e ch e rc h e r p ar n om
2 . R ec he rc h er p ar p re fi xe ( s ur l e n om )
3 . Q u i t te r
>
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 28/32
28 CHAPITRE 9. PREMIERS PAS J2EE
28 3
B ye b ye . . .
9.5 La partie presentation - servlet
9.5.1 Principe
Apres avoir construit plusieurs clients lourds, il nous semble essentiel de pouvoir offriraux utilisateurs eventuels la possibilite de consulter les cartes depuis un client leger.
Dans l’approche J2EE, les clients legers sont offerts par exemple par l’utilisation deservlet qui generent des pages html consultables depuis n’importe quel butineur web.
9.5.2 Ecriture de la servlet
Les differents repertoires utilises sont :– src : contient les sources de la servlet– bin/WEB-INF/ : contient les descripteurs de deploiements– bin/WEB-INF/classes : le code compile de la servlet.La servlet implementee reagira a une requete GET3 et son code est le suivant :
1 import j a v a . i o . P r i n t W r i t e r ;
import j a v a . i o . I O E x c e p t i o n ;
import j a v a . u t i l . C o l l e c t i o n ;
import j a v a . u t i l . I t e r a t o r ;
import j a v a x . s e r v le t . S e r v l e t E x c e p t i o n ;
6 import j a v a x . s e r v le t . h t t p . H t t p S e r v l e t ;
import j a v a x . s e r v le t . h t t p . H t t p S e r v l e t R e q ue s t ;
import j a v a x . s e r v le t . h t t p . H t t p S e r v l e t R e s po n s e ;
import j a v a x . s e r v le t . h t t p . H t t p S e s s i o n ;
import j a v a x . n a m i n g . C o n t e x t ;
11 import j a v a x . n a m i n g . I n i t i a l C o n t e x t ;
import j a v a x . r mi . P o r t a b l e R e m o t e O b j ec t ;
import consultators.*;
import carte_cmp.*;
16
p u b l ic c l a s s Consult extends H t t p S er v l e t {
p r o t e ct e d v o i d d oG e t ( H t t p S e r v l e t R e qu e s t r eq , H t t p S e r v le t R e s p o n s e r e s )
throws S e r v l et E x c e pt i o n , I O E x c e p ti o n {21 r e s . s e t C o n t e n t T y p e ( " t e x t / h t m l ");
P r in t W ri t er o ut = r es . g e t Wr i te r ( ) ;
head(out);
O b je c t p r ef i x = r eq . g e t P ar a me t e r (" p r e f i x " );
look(prefix , out);
26 foot(out);
}
p r i v a te v o i d l o ok ( O b j ec t p , P r in t W ri t er o ut ) {
3Il est possible de construire une servlet reagissant a une requete POST en implementant unemethode doPost.
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 29/32
9.5. LA PARTIE PR ESENTATION - SERVLET 29
if ( p = = null ) {
31 o u t . p r i n t l n ( " Y o u m u s t e n t e r a n a m e ! ");
return ;
}
S tr in g p re fi x = ( S tr in g ) p ;
if ( p r e f i x . e q u a l s ( " " )) {
36 o u t . p r i n t l n ( " Y o u m u s t e n t e r a n a m e ! ");
return ;
}
/ / I n i ti a li z i ng c o nt e xt
41 C o nt e xt i n i ti a l Co n t ex t = null ;
tr y {
i n i t i a l Co n t e x t = ne w InitialContext();
}
catch ( E x ce pt io n e ) {
46 o u t . p r i n t l n ( " < h 3 > U n a b l e t o f i n d I n i t i a l C o n t e x t ! < / h 3 > ");o ut . p r i nt l n ( e + " < b r / > " );
return ;
}
51 / / C on ne ct i ng t o t he b ea n
C o n su l t at o r Ho m e h o me = null ;
tr y {
h om e = ( C o n s ul t a to r H om e )
P o r t a b l e R e m o t e O b j e c t . n a r r o w (
56 i n i t i a l C o n t e x t . l o o k u p ( " M y C o n s u l t a t o r ") ,
C o n s u l t a t o r H o m e .class );
}
catch ( E x ce pt io n e ) {
o u t . p r i n t l n (" < h 3 > U n a b l e t o r e t r i e v e M y C o n s u l t a t o r ! < / h 3 > ");
61 o ut . p r i nt l n ( e + " < b r / > " );
return ;
}
/ / I n s t a n c ia t i o n
66 C o ns u l ta t or c o ns u lt = null ;
tr y {
c o ns u lt = h o me . c r e at e ( ) ;
}
catch ( E x ce pt io n e ) {
71 o u t . p r i n t l n ( " < h 3 > U n a b l e t o c r e a t e a C o n s u l t a t o r "
+ " i n s t a n c e ! < / h 3 > " );
o ut . p r i nt l n ( e + " < b r / > " );return ;
}
76
/ / D oi ng t he s ea rc h . ..
C o ll e ct i o n r e su l t = null ;;
tr y {
r e s u lt = c o n s ul t . r e c h e r c h e r P r e f i x ( p r e f ix ) ;
81 o u t . p r i n t l n ( " < h 2 > S e a r c h R e s u l t f o r " + p re fi x + " < / h 2 > " );
printResult( result , out);
}
catch ( E x ce pt io n e ) {
o u t . p r i n t l n (" < h 3 > U n a b l e t o i n v o k e t h e C o n s u l t a t o r ! < / h 3 > ");
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 30/32
30 CHAPITRE 9. PREMIERS PAS J2EE
86 o ut . p r i nt l n ( e + " < b r / > " );
return ;
}
91 }
p r i v a te v o i d p r in t R e s u lt ( C o l l e c t i o n c , P r i n t W ri t e r o u t )
throws E x c e pt i on {
if ( c . si ze ( ) = = 0 ) {
96 o u t . p r i n t l n ( "<strong >No matchfound ! </strong >");
}
I t er a to r i = c . i t e ra t or ( ) ;
while ( i . h a s Ne x t ( )) {
C a r te D e Vi s it e C MP c ar d = ( C a r t eD e V is i t eC M P ) i . n ex t ( );
101 o u t . p r i n t l n ( " < h 4 > " + c a rd . g e t No m ( ) + " " + c a r d . g e t P r en o m ( )
+ " < / h 4 > " );o u t . p r i n t l n (" < u l > ");
o u t . p r i n t l n (" < l i > T e l e p h o n e : "+ c ar d . g e tP h on e ( ) +" < / l i > " );
o u t . p r i n t l n (" < l i > E m a i l : "+ c ar d . g e tE m ai l ( ) +" < / l i > " );
106 o u t . p r i n t l n ( " < / u l > ");
}
}
111
p r i v a te v o i d h e a d ( P ri n t Wr i t er o ut ) {
o u t . p r i n t l n ("<html><head><title>CardConsultationForm </title>"
+" < / h e a d > ");
o u t . p r i n t l n (" < b o d y > ");
116 o u t . p r i n t l n ( " < h 1 > L o o k i n g f o r S o m e o n e ? < / h 1 > < b r > ");
o u t . p r i n t l n (" < f o r m m e t h o d = \ " g e t \ " > ");
o u t . p r i n t l n (" < p > " );
o u t . p r i n t l n (" N a m e : & n b s p ; "
+ " < i n p u t t y p e = \ " t e xt \ " n a m e = \ " p r e f i x \ " / > ");
121 o u t . p r i n t l n ( " < b r / > < i n p u t t y p e = \ " s u b m i t \ " v a l u e = \ " S e a r c h ! \ " / > ");
o u t . p r i n t l n (" < / p > ");
o u t . p r i n t l n (" < / f o r m > ");
}
126 p r i v a te v o i d f o o t ( P ri n t Wr i t er o ut ) {
o u t . p r i n t l n (" < / b o d y > ");
o u t . p r i n t l n (" < / h t m l > ");
out.close();}
131 }
Listing 9.24 – Fichier Consult.java
Descripteur J2EE
Le descripteur permet de decrire l’URL associee a la servlet ainsi que les liens avec lesbeans utilises.
< ? x ml v e r s io n = " 1 . 0 " e n c o d in g = " I S O - 8 8 5 9 - 1 "? >
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 31/32
9.5. LA PARTIE PR ESENTATION - SERVLET 31
< w eb - a p p x m l ns = " h t t p : / / j a v a . s u n . c o m / x ml / n s / j 2 e e "
xmlns:xsi="http://www.w3.org/2001/XMLSchema -instance"
4 x s i : s c h e m a L o c a t i o n = " h t t p : / / j a v a . s u n . c o m / x m l / n s / j 2 e e
h t t p : / / j a v a . s u n . c o m / x m l / n s / j 2 e e / w e b - a p p _ 2 _ 4 . x s d "
version="2.4">
<servlet >
<servlet -name> CardManager </servlet -name>
9 <servlet -class> Consult </servlet -class>
</servlet >
<servlet -mapping >
<servlet -name> CardManager </servlet -name>
<url-pattern >/card/ consult </url- pattern >
14 </servlet -mapping >
< e j b - r e f >
<ejb-ref-name>Consultator </ejb-ref-name>
<ejb-ref-type>Session </ejb-ref-type>
<home>consultators.ConsultatorHome </home>19 <remote >consultat ors. Consultator </remote >
< e j b - l i n k > C o n s u l t a t o r . j a r < / e j b - l i n k >
</ejb-ref>
</web-app>
Listing 9.25 – Fichier web.xml
Descripteur Jonas
Ce descripteur decrit les liens avec le session bean utilise.
< ? x ml v e r s io n = " 1 . 0 " e n c o d in g = " I S O - 8 8 5 9 - 1 "? >
< j o na s - w e b - a p p x m l n s = " h t tp : / / w w w . o b j e c t w e b . o r g / j o n as / n s "
3 xmlns:xsi ="http:// www.w3.org /2001/XMLSchema -instance "
x s i : s c h e m a L oc a t i o n = " h t t p : / / w w w . o b j e ct w e b . o r g / j o n a s / n s
h t t p : / / w ww . o b j e c t w e b . o r g / j o n as / n s / j o n as - w e b - a p p _ 4 _ 0 . x s d " >
< j o n a s - e j b - r e f >
<ejb-ref-name>Consultator </ejb-ref-name>
8 <jndi-name >MyConsultator </jndi-name >
</jonas-ejb-ref>
<host> localhost </host>
<context -root> web-application </context -root>
< / j o n a s - w e b - a p p >
Listing 9.26 – Fichier jonas-web.xml
9.5.3 Compilation et deploiement
Les servlets sont chargees comme des applications web, et doivent etre packagees commetelles. Ainsi, on archivera le contenu du repertoire bin dans un fichier war (pour Web
Archive ) et non dans un jar (Java Archive )4.
4Un fichier .war se genere comme un fichier .jar en utilisant la commande jar
7/17/2019 atelier-ejb-2.0
http://slidepdf.com/reader/full/atelier-ejb-20 32/32
32 CHAPITRE 9. PREMIERS PAS J2EE
Compilation
La servlet accede au session bean et entity bean via son interface remote qu’il fautdonc copier dans le repertoire src.
TODO pourquoi aussi l’entity bean ?
TODO pourquoi pas via un proxy ?
L I N UX : ~ > j a v ac - c p $ J O N A S \ _ R O O T / l i b / c o m m o ns / j 2 e e . s e r v l e t . j a r - d b i n / WE B - I N F / c l a s s e s s r c / * j
L I NU X : ~ > c p w eb . x m l j on as - w eb . x m l b in / W EB - I N F / .
3 LINUX:~> cd bi n
L I NU X : ~/ b in > j ar c f . ./ C o n s ul t . w ar *
L I N U X : ~ / b i n > cd ..
Chargement
L I N UX : ~ > m v C o n s u l t . w a r > J O N A S _ R OO T / w e b a p p s / .
LINUX:~> cd $JONAS_ROOT/webappsL I NU X : ~ > j o na s a d mi n - a C o ns u lt . w a r
Execution
Il suffit d’ouvrir son butineur prefere, et de se rendre a l’adresse decrite dans le fichierde mapping : http://localhost:9000/web-application/card/consult.
9.6 Pour aller plus loin
– Ecrire une servlet pour l’administration de l’annuaire.– Utiliser la securite J2EE– Utiliser les transactions