Creating the Enterprise Bean - The Java EE 5 Tutorial (original) (raw)
2. Using the Tutorial Examples
3. Getting Started with Web Applications
5. JavaServer Pages Technology
7. JavaServer Pages Standard Tag Library
10. JavaServer Faces Technology
11. Using JavaServer Faces Technology in JSP Pages
12. Developing with JavaServer Faces Technology
13. Creating Custom UI Components
14. Configuring JavaServer Faces Applications
15. Internationalizing and Localizing Web Applications
16. Building Web Services with JAX-WS
17. Binding between XML Schema and Java Classes
19. SOAP with Attachments API for Java
21. Getting Started with Enterprise Beans
Creating the converter Application Client
Coding the converter Application Client
Creating a Reference to an Enterprise Bean Instance
Compiling the converter Application Client
Creating the converter Web Client
Coding the converter Web Client
Compiling the converter Web Client
Deploying the converter Java EE Application
Deploying the converter Example Using NetBeans IDE
Deploying the converter Example Using Ant
Running the converter Application Client
Running the converter Application Client Using NetBeans IDE
Running the converter Application Client Using Ant
Running the converter Web Client
Modifying the Java EE Application
23. A Message-Driven Bean Example
24. Introduction to the Java Persistence API
25. Persistence in the Web Tier
26. Persistence in the EJB Tier
27. The Java Persistence Query Language
28. Introduction to Security in the Java EE Platform
29. Securing Java EE Applications
31. The Java Message Service API
32. Java EE Examples Using the JMS API
36. The Coffee Break Application
37. The Duke's Bank Application
Creating the Enterprise Bean
The enterprise bean in our example is a stateless session bean calledConverterBean. The source code for ConverterBean is in the tut-install/javaeetutorial5/examples/ejb/converter/converter-ejb/src/java/ directory.
Creating ConverterBean requires these steps:
- Coding the bean’s business interface and class (the source code is provided)
- Compiling the source code with the Ant tool
Coding the Enterprise Bean
The enterprise bean in this example needs the following code:
- Remote business interface
- Enterprise bean class
Coding the Business Interface
The business interface defines the business methods that a client can call. The business methods are implemented in the enterprise bean class. The source code for theConverter remote business interface follows.
package com.sun.tutorial.javaee.ejb;
import java.math.BigDecimal; import javax.ejb.Remote;
@Remote public interface Converter { public BigDecimal dollarToYen(BigDecimal dollars); public BigDecimal yenToEuro(BigDecimal yen); }
Note the @Remote annotation decorating the interface definition. This lets the container know that ConverterBean will be accessed by remote clients.
Coding the Enterprise Bean Class
The enterprise bean class for this example is called ConverterBean. This class implements the two business methods (dollarToYen and yenToEuro) that the Converter remote business interface defines. The source code for the ConverterBean class follows.
package com.sun.tutorial.javaee.ejb;
import java.math.BigDecimal; import javax.ejb.*;
@Stateless public class ConverterBean implements Converter { private BigDecimal yenRate = new BigDecimal("115.3100"); private BigDecimal euroRate = new BigDecimal("0.0071");
public BigDecimal dollarToYen(BigDecimal dollars) {
BigDecimal result = dollars.multiply(yenRate);
return result.setScale(2, BigDecimal.ROUND_UP);
}
public BigDecimal yenToEuro(BigDecimal yen) {
BigDecimal result = yen.multiply(euroRate);
return result.setScale(2, BigDecimal.ROUND_UP);
}
}
Note the @Stateless annotation decorating the enterprise bean class. This lets the container know that ConverterBean is a stateless session bean.
Compiling and Packaging the converter Example
Now you are ready to compile the remote business interface (Converter.java) and the enterprise bean class (ConverterBean.java), and package the compiled classes into an enterprise bean JAR.
Compiling and Packaging the converter Example in NetBeans IDE
Follow these instructions to build and package the converter example in NetBeans IDE.
- In NetBeans IDE, select File→Open Project.
- In the Open Project dialog, navigate to tut-install/javaeetutorial5/examples/ejb/.
- Select the converter folder.
- Select the Open as Main Project and Open Required Projects check boxes.
- Click Open Project.
- In the Projects tab, right-click the converter project and select Build. You will see the output in the Output tab.
Compiling and Packaging the converter Example Using Ant
To compile and package converter using Ant, do the following:
- In a terminal window, go to this directory:
tut-install/javaeetutorial5/examples/ejb/converter/ - Type the following command:
ant
This command calls the default task, which compiles the source files for the enterprise bean and the application client, placing the class files in the buildsubdirectories (not the src directory) of each submodule. Then the default task packages each submodule into the appropriate package file: converter-app-client.jar for the application client,converter-ejb.jar for the enterprise bean JAR, and converter-war.war for the web client. The web client in this example requires no compilation. For more information about the Ant tool, see Building the Examples.
Note - When compiling the code, the preceding ant task includes the javaee.jar file in the classpath. This file resides in the lib directory of your Application Server installation. If you plan to use other tools to compile the source code for Java EE components, make sure that the classpath includes the javaee.jar file.
Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Legal Notices