Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How J2EE creates Enterprise Bean

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how J2EE creates Enterprise Bean". In daily operation, I believe many people have doubts about how to create Enterprise Bean in J2EE. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how J2EE creates Enterprise Bean". Next, please follow the editor to study!

Enterprise bean is a server-side component that contains application business logic. At run time, the application client calls the method of enterprise bean to execute the business logic. In our example, enterprise bean is a stateless session bean called ConverterEJB. The source code of ConverterEJB bean is in the examples/src/ejb/converter directory.

Write Enterprise Bean

The enterprise bean in this example requires the following code:

Remote interface

Home interface

Enterprise bean class

Write Remote Interface

Remote interface defines the business methods that the client can call. Business methods are implemented in enterprise bean. The following is the source code of Converterremote interface.

Import Javax.ejb.EJBobject; import java.Rmi.RemoteException; public interface Converter extends EJBObject {public double dollarToYen (double dollars) throws RemoteException; public double yenToEuro (double yen) throws RemoteException;}

Write Home Interface

Home interface defines methods that allow clients to create, find, or remove enterprise bean. ConverterHome interface contains a single create method that returns an object of type remote interface. This is the source code of the ConverterHome API:

Import java.io.Serializable; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; public interface ConverterHome extends EJBHome {Converter create () throws RemoteException, CreateException;}

Write Enterprise Bean Class

The enterprise bean class in the example is called ConverterBean. This class implements two business methods, dollarToYen and yenToEuro, which are defined by Converter remote interface. The following is the source code of the ConverterBean class.

Import java.rmi.RemoteException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class ConverterBean implements SessionBean {public double dollarToYen (double dollars) {return dollars * 121.6000;} public double yenToEuro (double yen) {return yen * 0.0077;} public ConverterBean () {} public void ejbCreate () {} public void ejbRemove () {} public void ejbActivate () {} public void ejbPassivate () {} public void setSessionContext (SessionContext sc) {}}

Compile source file

You are now ready to compile remote interface (Converter.java), home interface (ConverterHome.java), and enterprise bean classes (ConverterBean.java):

To the examples/src directory.

Type the following command in the terminal window:

Ant converter

This command compiles the source files for enterprise bean and J2EE application clients. It puts the generated class files in the examples/build/ejb/converter directory. For more information about ant, see how to build and run examples.

Note: when compiling code, ant needs the j2ee.jar file included in classpath. This file is placed in the lib directory of the J2EE SDK installation. If you plan to use other tools to compile the source code of the J2EE component, be sure to include the j2ee.jar file in classpath.

Package Enterprise Bean

In this chapter you will run deploytool's New Enterprise Bean Wizard to perform these tasks:

Create the bean's deployment descriptor.

Package the deployment descriptor and bean classes in an EJB JAR file.

Embed the EJB JAR file into the application's ConverterApp.ear file.

To start New Enterprise Bean Wizard, select File- > New Enterprise Bean. The wizard displays the following dialog box.

Introduction dialog box

Read the explanatory text of the wizard's feature overview.

Click Next.

EJB JAR dialog box

Select Create new EJB File. Net in the application button.

In the combo box, select ConverterApp.

Enter ConverterJAR. EJB Display Name in the column.

Click Edit.

Under the Available Files directory tree, find the examples/build/ejb/converter directory. If the converter directory is under multiple layers of the tree, you can simplify the view of the tree by entering the directory pathname of all or part of the converter in the Starting Directory column.

Select the following class from the Available Files directory tree and click Add: Converter.class, ConverterBean.class, ConverterHome.class. You can also drag these class files to the Contents text area.

Click OK.

Click Next.

General dialog box

Under the Bean type, select the Session radio button.

Select the Stateless radio button.

In the Enterprise Bean Class combination box, select ConverterBean.

In the Enterprise Bean Name column, enter ConverterEJB.

In the Remote Home Interface combo box, select ConverterHome.

In the Remote Interface combo box, select Converter.

Click Next.

Transaction management dialog box

Because you can ignore the rest of the dialog box and click Finish.

At this point, the study on "how J2EE creates Enterprise Bean" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report