EJB's stateless session bean simple application - Learning and practice (Inber original)

xiaoxiao2021-03-06  61

Customers communicate through remote and local interfaces provided by EJB containers

Access control tables are used to ensure security their authorization can access specific features

User group or personal table.

Persistence means that the object state is permanently stored in a data store,

Such as databases.

Corporate Beans Type:

-

Entity (Entity) Beans

Entity Beans support multiple sessions and multiple customers, can be divided into:

BEAN - Management Bean Persistence

Container - Persistence of Manage Containers

-

Session (session) Beans

There is no storage mechanism when session beans perform business tasks, which can be divided into:

Stateful session (Stateful Session) bean

Stateless Session) Bean

This example takes the content I have learned as an example: Earnest Bank requires the development of a calculator component for a US dollar and rupee conversion. Description Use EJB to create this component is a reasonable and explained the code that must be written.

I. Identification mechanism. Determine the type of enterprise bean. Write remote interface code. Write local interface code. Write enterprise bean class code six. Compilation source file

I. Identification mechanism EJB is suitable technology because: EJB component automatic processing system-level service enterprise bean only implements business logic

II. Determining the calculator component of the type of business bean does not require a persistent storage mechanism currency, this value is not used in other places, therefore, is best suited to create a stateful session bean

3. Write remote interface code remote interface Define all business methods of enterprise beans, not including system-level operation methods. These business methods are implemented in the EJB class.

To write a remote interface: need javax.ejb.ejbobject and java.rmi.RemoteException interface to establish a remote interface of the extended EJBOBJECT interface to implement all the business methods defined in the remote interface in the EJB class. The statements in this problem, the remote interface program can be written as follows: import javax.ejb.EJBObject; import java.rmi.RemoteException; public interface Calculator extends EJBObject {public double dollarToRs (double dollars) throws RemoteException;} written in four local interface. The code local interface code defines how to allow EJB customers to establish and find EJB components. The steps to write the local interface are as follows: Java.io.serializable, java.rmi.remoteException, javax.eb.createException, javax.ejb.ejbhome interface to establish a specific EJB to establish a specific EJB An example of a class, the return type of the method must be the remote interface object of the EJB. import java.io.Serializable; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; public interface CalculatorHome extends EJBHome {Calculator create () throws RemoteException, CreateException;}

5. Write the business method to implement the business method described in the remote interface. To prepare the EJB class: Java.rmi.RemoteException, javax.ejb.sessionBean, Javax.ejb.SessionContext interface creates EJB classes to implement the sessionBean interface to implement the business method defined in the remote interface. Write ejbcreate (), EJBREMOVE (), EJBActivate (), EjbPassivate (), setSessionContext (), and implementation of the default constructor method. Note: The method name implemented in the EJB class and its characteristics must be consistent with the declarations declared in the remote method. According to the statement of the problem and the previous description, the following EJB code can be created. EJB code import java.rmi.RemoteException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class CalculatorEJB implements SessionBean {public double dollarToRs (double dollars) {return dollars * 47.20;} public CalculatorEJB () {} public Void ejbcreate () {} public void ejbremove () {} public void ejbactivate () {} public void ejbpassivate () {} public void setsessionContext (sessioncontext sc) {}} 6. Compilation source file

The following is the original code remote interface program: / * @ inbergondate 2004/9 / 8file name: Calculator.java * /

Import javax.ejb.ejbobject; import java.rmi.RemoteException;

public interface Calculator extends EJBObject {public double dollarToRs (double dollars) throws RemoteException; public double rsToDollar (double rs) throws RemoteException; public double rmbToDollar (double rmb) throws RemoteException; public double dollarToRMB (double dollars) throws RemoteException;} local interface program As follows: / * @ inbergondate 2004/9 / 8file name: CalculatorHome.java * /

Import java.io. *; import java.rmi. *; import javax.ejb. *;

Public Interface CalculatorHome Extends Ejbhome {Calculator Create () THROWS CREATEXCEPTION, RemoteException;}

/ * @ Inbergondate 2004/9 / 8file name: Calculatorejb.java * / import java.rmi. *; Import javax.ejb. *;

Public class CalculatoreJB IMPLEments SessionBean {

public double dollarToRs (double dollars) throws RemoteException {return dollars * 47.20;} public double rsToDollar (double rs) throws RemoteException {return rs / 47.20;} public double rmbToDollar (double rmb) throws RemoteException {return rmb / 9;} public double DollartORMB (Double Dollars) throws RemoteException {Return Dollars * 9;

public void ejbCreate () throws CreateException, RemoteException {System.out.println ( "ejbCreate ()");} public CalculatorEJB () {System.out.println ( "CalculatorEJB ()");} public void setSessionContext (SessionContext sc) {System.Out.println ("setsessionContext ()");} public void ejbremove () {system.out.println ("ejbremove ()");} public void ejbactivate () {} public void ejbpassivate ()}

}

Finally, client program / * @ inbergondate 2004/9 / 8file name: CalculatorClient.java * /

Import java.awt. *; import java.awt.event. *; import javax.swing. *; import javax.naming. *; import javax.rmi. *;

public class CalculatorClient extends JFrame {public static int w = 500; public static int h = 95; public static String str = "Earnest Bank Welcomes You"; Container c; JLabel l, result; JTextField t; JButton b; public static String value Public statune DBL; Public Static Double Amt; Public CalculatorClient () {Super (STR); C = getContentPane (); C.SetLayout (New GridLayout (2, 2, 2, 2)); l = new jlabel (" ENTER THE AMOUNT IN DOLLARS ($) "); C.Add (L); T = New JtextField (10); C.Add (T); B = New JButton (" Calculate "); C.ADD (B); Result = new jlabel (); c.add (result); B.AddActionListener (new addevent ()); setsize (w, h); show ();} class addevent imports actionListener {public void actionPerformed (ActionEvent E) {Value = t.getText (); dbl = Double.parseDouble (value); try {Context ic = new InitialContext (); Object obj = ic.lookup ( "CalculatorJNDI"); // JNDI name CalculatorHome home = (CalculatorHome) PortableRemot EOBJECT.NARROW (Obj, CalculatorHome.Class); Calculator Calc = Home.create (); AMT = Calc.dollartors (DBL); Calc.remove (); Calculator Calc2 = Home.create (); if (Calc2.isident " Calc)) {system.out.println ("Two pile facts points to the same remote home instance! ");} Else {system.out.println (" Did not point to the same remote home instance! ");} Result.setText (" Result (RS.) String.Valueof (AMT));} catch (Exception T ) {System.out.println (t);}}} public static void main (String args []) {new calculatorclient ();}}

Start configuring this J2EE1.3

Run J2EESTART J2EE -VERBOSEDEPLOYTOOL

new-> applicationapplicaton file name: E: /public_html/public_java/ap2/ap2.earapplicaton disply name: ap2.earnew-> Eterprise BeanContents which joined Calculator.class CalculatorHome.class CalculatorEJB.class three files -> NEXT next dialog box appears : New Enterprise Bean Wizard -General Selects Session in Bean Type -> Stateless Select CalculatorHome in Enterprise Bean Calss Select CalculatorHome in the local interface Select Calculator-> Next-> Next-> Next-> Next-> Next -> Next-> Next-> FINISH Point Application AP2 in the left window -> Tools-> Deploy ..-> Select Reploys Return Client Jar-> Next Ap2 -jndi Names dialog in Application's JNDI Name Enter the JNDI name to find in the client program: Calculatorjndi -> Next-> Finished ........-> OK

Enter the root of the program in the DOS window to join the JAR package set classpath =% classpath%; ap2client.jarjava CalculatorClient

转载请注明原文地址:https://www.9cbs.com/read-118948.html

New Post(0)