EJB Hello World Raiders By Apple@java.10.13.21.88
System Configuration Windows 2000 Pro JDK (JRE 1.4.1) UE J2EE1.3 Deploy Tool (J2EESDK comes with)
1. Reference of Mastering EJB2.0)
/ ** For each class / interface, please refer to EJB Textbook:) * /
////HellolocalHome.javaImport javax.ejb. *;
Public interface hellolocalhome extends ejblocalhome {public hellocal create () throws createException;}
/// ///hellolocal.javaimport javax.ejb. *;
Public Interface Hellolocal Extends EjblocalObject {public string Sayhello ();
//////HELLOHOME.JAVAIMPORT JAVAX.EJB. *; Import Java.rmi. *;
Public interface hellohome extends ejbhome {public hello create () throws remoteException, CreateException;
///Hello.javaimport java.rmi. *; Import javax.ejb. *;
Public interface hello extends ejbobject {public string Sayhello () THROWS RemoteException;
//Hellobean.javaImport javax.ejb. *;
Public Class Hellobean Implements SessionBean {Private SessionContext CTX;
Public void ejbcreate () {system.out.println ("ejbcreate ()");}
Public void ejbremove () {system.out.println ("ejbremove ()");}
Public void ejbactivate () {system.out.println ("ejbactivate ()");}
Public void ejbpassivate () {system.out.println ("ejbpassivate ()");}
Public void setsessionContext (sessioncontext ctx) {this.ctX = ctx;}
Public String Sayhello () {system.out.println ("Hello ()"); Return "Hello, World ~";}}
////Helloclient.java
Import javax.rmi. *; import javax.naming. *; import javax.naming.directory. *; import java.util. *; import java.rmi. *; import javax.ejb. *;
Public class helloclient {public static void main (string args []) {Try {INIALCONText Con = new initialcontext (); object obj = con.lookup ("helloWorld");
HelloHome home = (HelloHome) PortableRemoteObject.narrow (obj, HelloHome.class); Hello hello = home.create (); System.out.println (hello.sayHello ()); hello.remove ();} catch (RemoteException rme ) {System.out.println (RME.TOString ());} catch (namingexception ne) {system.out.println (ne.tostring ()); ne.printStackTrace ();} catch (exception re) {system. Out.println (Re.toString ());}}}
After writing, you can compile it, just compile into .class files :)
2. Publish EJB Start J2EE1.3 Start Deploy Tool (the machine is not good, it may be slower, wait a moment)
Under the Deploy Tool interface
1) Add ApplicationFile-> New-> Application ...
Select the path you want to store your EAR, then type your name of your EAR file and enter your name to display your Application. For example, my EAR file is Hello, the name of the display is also called Hello if there is no this EAR file. File, if any, you have an original file. This is created so that your application is created.
2) Add EJBFILE-> New-> Enterprise Bean ...
Next-> Enter the name of the created JAR file, such as my name HelloWorld-> Click the EIDT button in the right -> Select the catalog of compiled Class files, add hellolocal.classhellolocalhome.class hello.class hellohome.class Hellobean. class-> OK-> next-> select the Bean's Type Stateless Session bean-> EnterpriseBean HelloBean to choose, Local Home interface selected HelloLocalHome, LocalInterface select HelloLocal, Remote Home interface selected HelloHome, Remote interface selection Hello-> next-> Finish .
I remember that it is also important to set up HelloWorld JNDI Name, I set it is HelloWorld (with the JAR name, you can also set another)
3) Add Application Client
File-> new-> Application Client ...
Next-> edit-> Add HelloClient.class method to -> Next-> Next-> Next-> Now see if you are an EJB REFERENCED IN CODE -> Add-> Code Name Fill in Hello, Type Session, Interface This chooses REMOTE or LOCAL affects the following fill in, I chose Remote-> Home Interface to fill in HelloHome-> local / remoteInterface Fill in Hello-> Select JNDI Name, fill in the HelloWorld, pay attention to the same as the JNDI Name set above -> Finish.4) Check the menu with a green pair of buttons, click, point OK on the new pop-up dialog. Note that there is no error message, if not, the lower Details should show hello: there at no no Failed Tests. You can release it. Click Close to close the dialog.
5) Publish deploy on the menu Click on, put the return client JAR -> NEXT-> Whether the two JNDI Name is the same, it should be helloworld-> next-> finish. If there is nothing, it should be correctly released. If your package is occupied, if you use Winzip or WinRAR to open the check, you may report it wrong.
3. Operating procedures After you have finished posting, you should have two files hello.ear helloclient.jar, and there are other temporary files tagged as Temp.
Before running, you can add a PATH% J2EE_HOME% / bin :) Command line How to store the path to the EAR file to type the following command set appcpath = helloclient.jarrunclient -client hello.iear -name HelloClient -TextAuth Enter the user guest password guest123 (This is set by J2EE yourself, you can modify the configuration file) You can see your helloworld.
E: / temp / javatest / hello / hello> set appcpath = helloclient.jar
E: / Temp / javatest / hello / hello> runclient -client Hello.ear -nameHelloClient -textauthInitiating login ... Username = nullEnter Username: guestEnter Password: guest123Binding name: `java: comp / env / HelloWorld`starting lookuplookup overHello, world ~ Unbinding name: `java: comp / env / helloworld`e: / temp / javatest / hello / hello>
Hellobean created and deleted records in J2EE's window