Upload
arvindkonline6095
View
218
Download
0
Embed Size (px)
Citation preview
7/22/2019 EJB Component Lab Practice
1/16
EJB Component Lab Practice
This section is designed to enhance the understanding of the EJB concepts by providing some concrete
examples and step-by-step guidelines demonstrating how to build EJB components, how to assemble and
deploy EJB components, and how to build different clients for these components and run the clients. Here
are two examples for lab practice. ab ! describes the steps to build a session bean called temperature converter component and to deployit by the utility tool deploytoolin J"EE !.#.x. The client of this temperature converterEJB application is
either a J$% web component which is accessed by a browser or a Java application client. ab ! also
demonstrates the running result of a web client and an application client of this EJB component.
#.&.! ab!' Temperature (onverter $ession Bean (omponent )evelopment
$tep!. *nstallation and configuration for lab! and lab"
!. )ownload J"EE !.# +J"EE!.# application server, J"$E!.#.x, J"EE!.# samples and install J"EE$)
!.# with J) !.#." on your machine.
". dd /J2EE_HOME01bin and /JAVA_HOME01bin to the front of the path environment variable so
that J"EE !.# pplication $erver scripts +asadmin, asant, deploytool,and wscompile
override other installations2. *n order to run the asantscripts create directory structure, properties files and 34 files as follows'
(reate wor5ing directory c:\cop\ejb.
6nder c:\cop\ejbdirectory create two sub-directories: common, tempconv
6nder c:\cop\ejb\tempconvcreate two sub-directories' srcand web. ll the Java
source code files will be stored in the srcsub-directory, and web related files li5e .jspfiles
will be in websub-directory.
6nder c:\cop\ejb\common create two files' build.propertiesand
targets.xml.6nder c:\cop\ejb\tempconvcreate build.xml file. ssume that
the J"EE !.# pplication $erver installation is ('7$un7pp$erver.
8r 9ust simply copy the entire (8% directory in ab# of the companion () to c:\. *t will
ta5e care of all above configurations.
build.propertyis as the follows'
j2ee.home=C:/un/!pperversunone.home="#j2ee.home$admin.password=admin.host=localhostadmin.user=adminadmin.port=%&%&https.port='(%)domain.resources=*domain.resources*domain.resources.port=&(&(db.root="#j2ee.home$/pointbase
db.driver=com.pointbase.jdbc.jdbc+niversalriverdb.host=localhostdb.port=-(-2db.sid=sunappservsamplesdb.url=jdbc:pointbase:server://"#db.host$:"#db.port$/"#db.sid$url.prop=atabaseameds.class=com.pointbase.jdbc.jdbcataourcebuild=buildconpool.name=0oint1ase0ooljdbc.resource.name=jdbc/0oint1ase
7/22/2019 EJB Component Lab Practice
2/16
targets.xml is shown here:
path id=*classpath*3 4ileset dir=*"#j2ee.home$/lib*3 include name=*j2ee.jar*/3 /4ileset3 /path3
target name=*clean* 3 delete dir=*"#build$* /3 delete dir=*"#dist$* /3/target3
path id=*db.classpath*3 4ileset dir=*"#db.root$/lib*3 include name=*5.jar*/3 /4ileset3 /path3
target name=*init*3 tstamp /3 /target3
target name=*prepare* depends=*init* description=*Create build directories.*3
m6dir dir=*"#build$* /3/target3
target name=*build* depends=*prepare*description=*Compile source code* 3
javac srcdir=*src* destdir=*"#build$*3include name=*55/5.java* /3
classpath re4id=*classpath*/3/javac3/target3
build.xmlis shown here'
78C90; project < 7;99 targets 9;> *../common/targets.xml*3?3project name=*j2eecopejb* de4ault=*build* basedir=*.*3 property 4ile=*../common/build.properties*/3 @targetsA/project3
$tep ". (oding the Enterprise Bean
The enterprise bean in this example includes the following code'
: ;emote interface
: Home interface
: Enterprise bean class
//Bemote inter4ace: 9empConv.java
7/22/2019 EJB Component Lab Practice
3/16
pac6age tempConvA
import javax.ejb.;18bjectAimport java.rmi.Bemote;xceptionA
public inter4ace 9empConv extends ;18bject # public double 49oC Ddouble 4 E throws Bemote;xceptionA public double c9oF Ddouble cE throws Bemote;xceptionA$
//Gome inter4ace: 9empConvGome.java
pac6age tempConvA
import java.io.erialiHableAimport java.rmi.Bemote;xceptionAimport javax.ejb.Create;xceptionAimport javax.ejb.;1GomeA
public inter4ace 9empConvGome extends ;1Gome #
9empConv createDE throws Bemote;xception, Create;xceptionA$
//;nterprise bean class:9empConv;1.javapac6age tempConvA
import java.rmi.Bemote;xceptionAimport javax.ejb.ession1eanAimport javax.ejb.essionContextAimport java.math.5A
public class 9empConv;1 implements ession1ean #
public double c9oFDdouble cE # return DDc5-.(/I.( J )2E5'((E/'((.(A $
public double 49oCDdouble 4E # return DD4)2E5I.(/-.(5'((E/'((.(A $
public 9empConv;1DE #$ public void ejbCreateDE #$ public void ejbBemoveDE #$ public void ejb!ctivateDE #$ public void ejb0assivateDE #$
public void setessionContextDessionContext scE #$
$
//!pplication Client 4or ;1
import tempConv.9empConvGomeAimport tempConv.9empConvAimport javax.naming.ContextAimport javax.naming.nitialContextA
7/22/2019 EJB Component Lab Practice
4/16
import javax.rmi.0ortableBemote8bjectAimport java.text.ecimalFormatAimport javax.swing.8ption0aneA
public class 9empConvClient # public static void mainDtring
7/22/2019 EJB Component Lab Practice
5/16
8bject objBe4 = ic.loo6upD*java:comp/env/ejb/MebConv*EA 9empConvGome home =D9empConvGomeE0ortableBemote8bject.narrowDobjBe4, 9empConvGome.classEA conv = home.createDEA $ catch DBemote;xception exE # ystem.out.printlnD*CouldnPt create 9empConv bean.*Jex.get>essageDEEA $ catch DCreate;xception exE # ystem.out.printlnD*CouldnPt create 9empConv bean.*Jex.get>essageDEEA $ catch Daming;xception exE # ystem.out.printlnD*+nable to loo6up home: *J *MebConv *Jex.get>essageDEEA $
$
public void jspestroyDE #conv = nullA
$N3
html3head3 title39emperature Converter/title3/head3
body bgcolor=*white*3h'3b3center3 9emperature Converter /center3/b3/h'3hr3p3;nter a degree to convert:/p34orm method=*get*3input type=*text* name=*degree* siHe=*2I*3br3p3input type=*submit* name=*49oC* value=*Fahrenheit to Centigrade*3input type=*submit* name=*c9oF* value=*Centigrade to Fahrenheit*3/4orm3
N ecimalFormat twoigits = new ecimalFormat D*(.((*EA tring degree = reQuest.get0arameterD*degree*EA i4 D degree 7= null @@ degree.lengthDE 3 ( E # double d = ouble.parseoubleDdegreeEAN3N i4 DreQuest.get0arameterD*49oC*E 7= null E #N3 p3
N= degree N3 4ahrenheit degrees are N= twoigits.4ormat Dconv.49oCDdEE N3 centigrade degrees.N $N3N i4 DreQuest.get0arameterD*c9oF*E 7= null E #N3 p3 N= degree N3 centigrade degrees are N= twoigits.4ormat
7/22/2019 EJB Component Lab Practice
6/16
Dconv.c9oFDdEE N3 4ahrenheit degrees .N $N3N $N3/body3/html3
$tep #. (ompiling all source code
!. 8n windows choose0rogram R un >icrosystem R 2;; '.% S R tart e4ault omain
to start J"EE pplication $erver
8r start up J"EE pplication $erver by command line in a terminal window.
>asadmin start-domain --verbose domain1
". (ompile the source files at command prompt in C:\cop\ejb\tempconvdirectory. Type
>asant build
new directory buildis created with class files.
$tep &. )eployment
!. $tart deploytool
*n icrosystem R 2;; '.% S R deploytool
to startdeploytoolutility.
8r on command line type>deploytool
". (reating the J"EE pplication *n deploytool, select File R ew R !pplication.
(lic5 1rowse.
*n the file chooser, navigate to c:\cop\ejb\tempconv.
*n the =ile >ame field, enter 9empConv!pp.ear.
(lic5 >ew pplication and 8
7/22/2019 EJB Component Lab Practice
7/16
2. %ac5aging the EJB
To pac5age an enterprise bean, you run the >ew Enterprise Bean wi?ard of the deploytoolutility
which will create the bean@s deployment descriptor, pac5age the deployment descriptor and the bean@sclasses in an EJB J; file, and *nserts the EJB J; file into the application@s 9empConv!pp.earfile.
To start the ew ;nterprise 1eanwi?ard, select File R ew R ;nterprise 1ean.
*n the EJB J; dialog box select the Create ew !B >odule in !pplication
radio button.
*n the combo box, select 9empConv!pp.
*n the J; )isplay >ame field, enter 9empConv!B.
(lic5 Edit.
*n the tree under vailable =iles, go to C:\cop\ejb\tempconv\builddirectory.
$elect 9empConv 4older from the vailable =iles tree and clic5 dd
(lic5 8. nd >ext
*n the Aeneral dialog box select temp(ovn.9empConv;1in the Enterprise Bean (lass combo box.erify 9empConv;1 in the Enterprise Bean >ame field. 6nder Bean Type, select the $ession and
$tateless radio buttons.
$elect tempConv.9empConvGome in the ;emote Home *nterface combo box, select
temp(onv.9empConvin the ;emote *nterface combo box, (lic5 >ext.
7/22/2019 EJB Component Lab Practice
8/16
C (lic5 =inish in Transaction 4anagement dialog box
fter the pac5aging process, you can view the deployment descriptor by selecting
9ools R escriptor Tiewer.
Uxml version=P'.(P encoding=P+9F&PU3ejbjar version=*2.'* xmlns=*http://java.sun.com/xml/ns/j2ee*
xmlns:xsi=*http://www.w).org/2(('/V>Wchemainstance*xsi:schemaWocation=*http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/ejbjarK2K'.xsd* 3displayname39empConv!B/displayname3enterprisebeans3
session3ejbname39empConv;1/ejbname3home3tempConv.9empConvGome/home3remote3tempConv.9empConv/remote3ejbclass3tempConv.9empConv;1/ejbclass3sessiontype3tateless/sessiontype3transactiontype31ean/transactiontype3securityidentity3 usecalleridentity3
/usecalleridentity3/securityidentity3/session3
/enterprisebeans3/ejbjar3
#. %ac5aging the pplication (lient
To start the >ew pplication (lient wi?ard, select =ile D >ew D pplication (lient
*n ar File Contentsdialog box select Create ew !ppClient >odule in
!pplication radio button.
$elect 9empConv!pp in the combo box
Enter 9empConvClientin the !ppClient amefield.
7/22/2019 EJB Component Lab Practice
9/16
*n the tree under vailable =iles, go to c:\cop\ejb\tempconv\build directory.
$elect the 9empConvClient.classfile and clic5 dd.
(lic5 8 and >ext.
$elect 9empConvClientin the 4ain (lass combo box in the Aeneral dialog box,
$elect container-managed authentication in the (allbac5 Handler (lass combo box,.
>ow clic5 >ext and =inish.
$pecifying the pplication (lients Enterprise Bean ;eference'
*n the tree, select 9empConvClient.
$elect the EJB ;efs tab, (lic5 dd.
Type ejb/ClientConvin the (oded >ame column, select $ession in the Type field, select ;emote
in the *nterfaces field
Type temp(onv.9empConvGomein the Home *nterface field, type tempConv.9empConv*n the
ocal1;emote *nterface field
7/22/2019 EJB Component Lab Practice
10/16
fter the pac5aging process, you can view the deployment descriptor by selecting
9ools R escriptor Tiewer.
Uxml version=P'.(P encoding=P+9F&PU3applicationclient version=*'.%*
xmlns=*http://java.sun.com/xml/ns/j2ee*xmlns:xsi=*http://www.w).org/2(('/V>Wchemainstance*xsi:schemaWocation=*http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/applicationclientK'K%.xsd* 3
displayname39empConvClient/displayname3ejbre43 ejbre4name3ejb/ClientConv/ejbre4name3
ejbre4type3ession/ejbre4type3
home3tempConv.9empConvGome/home3remote3tempConv.9empConv/remote3/ejbre43
/applicationclient3
#. %ac5aging the
7/22/2019 EJB Component Lab Practice
11/16
dds the M!Bfile to the applications 9empConv!pp.earfile
Build the web component by the >ew ext.
*n the (hoose Component9ypedialog box select J$% radio button. (lic5 >ext
*n the Component Leneral 0ropertiesdialog box select index.jspin the 0
Fileame combo box
(lic5 =inish.
7/22/2019 EJB Component Lab Practice
12/16
$pecifying the
7/22/2019 EJB Component Lab Practice
13/16
$pecifying the J>)* >ames
=ollow the following steps to map the enterprise bean references used by the clients to the J>)* name of
the bean, *n the tree, select 9empConv!pp.
$elect the J>)* >ames tab.
To specify a J>)* name for the bean, find the 9empConv;1component in the pplication table andenter >y9empConvin the J>)* >ame column.
To map the references, in the ;eferences table enter >y9empConvin the J>)* >ame for each row.
(lic5
7/22/2019 EJB Component Lab Practice
14/16
erify the full path name for the file 9empConv!ppClient.jarso that it will reside in the
c'7cop7e9b7tempconv subdirectory. The 9empConv!ppClient.jarfile has the EJB stub that
enable remote access to 9empConv;1.
(lic5 8.
". ;unning the J"EE pplication (lient
*n a terminal window, go to the c:\cop\ejb\tempconvdirectory, chec5 9empConv!pp.earand
9empConv!ppClient.jarfiles.
> appclient -client TempConvAppClient.jar
Fou will see the screen shown in the figure after entering !GG in the input field and clic5ing 8.
7/22/2019 EJB Component Lab Practice
15/16
7/22/2019 EJB Component Lab Practice
16/16