Creating a J2EE app You cannot deploy enterprise components (EJB) to the J2EE server directly, you must add components to a J2EE application and then deploy. In this section, you will create a new J2EE app called Convertrapp and saved as convertERAPP.EAR. 1. Launch the J2EE server at the command line: J2EE? Verbose (stop server command j2ee -stop.) 2. Run the deployment tool in another terminal window: Deploytool (press F1 to get deployment tool help) 3. Create a new J2EE Application a. Select "File" menu B in the deployment tool. Select "New Application" from "File" menu. Click to browse D. Locate the directory of the .ser file in the file selection box. The file is named Convertrapp. Ear. f. Click "New Application" G. Click "OK" Enterprise Components (EJB) Packing this section You will run the EJB wizard of the deployment tool to complete the following tasks: • Creating a component deployment descriptor • Will describe the descriptor and components The class package is. JAR file • Pack the EJB.jar file to the J2EE Application CONVERTERAPP.EAR file Select Create EJB from the File menu to start creating new EJBs, this wizard will display the following dialog: introduction dialog: a Read Wizard Character Description Document B. Click "Next" EJB JAR dialog: a. Select Convertrapp. B in the combo box labeled "Enterprise Bean Will Go In," Enter Converterjar in Jar Display Name, Declare EJB The .jar file contains the component, which is rendered in the tree application structure. c. Click to add the content text domain d. In the top of the content editing dialog, enter the directory E.CLASS file e. Select this directory. Class Add: converter.class, convertERejb.class, and converterHome.class. f. click Determine G. Click Next Integrated dialog: a. Select "Session" in the component type. Select "Status" b. Select ConvertRejb. C. Select ConvertRejb. C. Select ConverterHome in the local interface combo box. In the remote interface combo box, select Converter. E. Enter ConverterBean. F in the enterprise component named domain. F. Click Next Environment Entry dialog: Because you can skip the back dialog, you can click Finish to create an EJB application task . Deploying J2EE App Now the J2EE application already contains enterprise components (EJB), you can deploy it. 1. Specify the JNDI name of the corporate component a. In the application deployment wave farmer tool, select Convertrapp B in the application tree structure. Select the JNDI name tag c. In the JNDI name domain, enter MyConverter and confirm. The client will use this name to locate the local interface.
2. Deploy J2EE Application a. Select "Deploy" from the Tools menu. Select Convertrapp to deploy objects in the first dialog box, localhost is the target server c. Select the Select Box D marked with "Return Client Jar.". Type CONVERTERAPPCLIENT.JAR in the text domain, such as J2EE's example directory Doc / Guides / EJB / Examples / Converter E. Click Next F. In the second dialog box, verify that ConverterBean's JNDI is named myconverter. g. Click Next h. Click End i in the third dialog. In the Deployment Process dialog box, click OK to complete the development client client clientClient is an independent Java application, creating ConverterClient mainly includes the following steps. : 1. Client Code 2. Client Compile Client Coding ConverterClient.java Source Code Shows the most basic task implementation of the EJB client, namely: • Positioning local interface · Create Enterprise Components (EJB) · Call business method Positioning local interface Converterhome The interface defines the EJB declaration cycle method such as CREATE. The ConverterHome object must be instantiated before the CREATE method can call the CREATE method: 1. Create JNDI Name Context (); 2. Find a named JNDI name MyConverter bound object java.lang.Object objref = initial.lookup ( "MyConverter"); 3. ConverterHome object reference points to define ConverterHome home = (ConverterHome) PortableRemoteObject.narrow (objref, ConverterHome.class); create enterprise components ( EJB) An instance client creates a CONVERTEREJB class object by calling the CREATE method of the local interface ConvertHome object, and the CREATE method returns an object of the component Converter type. Then, the remote method defines the business approach implemented in ConvertRejb to be called by the client. When the client calls the CREATE method, the EJB container will instantiate ConvertRejb and then call the ConvertRejb.ebcreate method. CONVERTER CURRENCYCONVERTER = home.create (); After calling a business method to complete the above tasks, calling a business method is relatively simple. You call the method of the Converter object, the EJB container runs the corresponding call to the ConvertRejb instance of the J2EE server. The code of the client calls the business method Dollartoyen is as follows: double amount = currencyconverter.dollartoyen (100.00); ConverterClient Source Code below is the full source code of ConverterClient.java: import javax.naming.context;
Import javax.naming.initialcontext;
Import javax.rmi.portableremoteObject;
Import converter;
Import converterhome;
Public class convertern {
Public static void main (String [] args) {
Try {
Context initial = new initialContext (); object objref = initial.lookup ("myconverter");
Converterhome Home =
(Converterhome) PortableRemoteObject.narrow (Objref,
Converterhome.class;
Converter currencyconverter = home.create ();
Double amount = currencyconverter.dollartoyen (100.00);
System.out.println (String.Valueof (Amount));
Amount = CurrencyConverter.yentoeuro (100.00);
System.out.println (String.Valueof (Amount));
Currencyconverter.remove ();
} catch (exception ex) {
System.err.Println ("Caught An Unexpected Exception!");
EX.PrintStackTrace ();
}
}
}