Creating the Enterprise Bean - The Java EE 6 Tutorial (original) (raw)
2. Using the Tutorial Examples
3. Getting Started with Web Applications
4. JavaServer Faces Technology
7. Using JavaServer Faces Technology in Web Pages
8. Using Converters, Listeners, and Validators
9. Developing with JavaServer Faces Technology
10. JavaServer Faces Technology: Advanced Concepts
11. Using Ajax with JavaServer Faces Technology
12. Composite Components: Advanced Topics and Example
13. Creating Custom UI Components and Other Custom Objects
14. Configuring JavaServer Faces Applications
16. Uploading Files with Java Servlet Technology
17. Internationalizing and Localizing Web Applications
18. Introduction to Web Services
19. Building Web Services with JAX-WS
20. Building RESTful Web Services with JAX-RS
21. JAX-RS: Advanced Topics and Example
23. Getting Started with Enterprise Beans
Modifying the Java EE Application
24. Running the Enterprise Bean Examples
25. A Message-Driven Bean Example
26. Using the Embedded Enterprise Bean Container
27. Using Asynchronous Method Invocation in Session Beans
Part V Contexts and Dependency Injection for the Java EE Platform
28. Introduction to Contexts and Dependency Injection for the Java EE Platform
29. Running the Basic Contexts and Dependency Injection Examples
30. Contexts and Dependency Injection for the Java EE Platform: Advanced Topics
31. Running the Advanced Contexts and Dependency Injection Examples
32. Introduction to the Java Persistence API
33. Running the Persistence Examples
34. The Java Persistence Query Language
35. Using the Criteria API to Create Queries
36. Creating and Using String-Based Criteria Queries
37. Controlling Concurrent Access to Entity Data with Locking
38. Using a Second-Level Cache with Java Persistence API Applications
39. Introduction to Security in the Java EE Platform
40. Getting Started Securing Web Applications
41. Getting Started Securing Enterprise Applications
42. Java EE Security: Advanced Topics
Part VIII Java EE Supporting Technologies
43. Introduction to Java EE Supporting Technologies
45. Resources and Resource Adapters
46. The Resource Adapter Example
47. Java Message Service Concepts
48. Java Message Service Examples
49. Bean Validation: Advanced Topics
50. Using Java EE Interceptors
51. Duke's Bookstore Case Study Example
52. Duke's Tutoring Case Study Example
53. Duke's Forest Case Study Example
The enterprise bean in our example is a stateless session bean calledConverterBean. The source code for ConverterBean is in the tut-install/examples/ejb/converter/src/java/ directory.
Creating ConverterBean requires these steps:
- Coding the bean’s implementation class (the source code is provided)
- Compiling the source code
Coding the Enterprise Bean Class
The enterprise bean class for this example is called ConverterBean. This class implements two business methods: dollarToYen and yenToEuro. Because the enterprise bean class doesn’t implement a business interface, the enterprise bean exposes a local, no-interface view. The public methods in the enterprise bean class are available to clients that obtain a reference to ConverterBean. The source code for the ConverterBean class is as follows:
package converter.ejb;
import java.math.BigDecimal; import javax.ejb.*;
@Stateless public class ConverterBean { private BigDecimal yenRate = new BigDecimal("83.0602"); private BigDecimal euroRate = new BigDecimal("0.0093016");
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 annotation lets the container know that ConverterBean is a stateless session bean.
Creating the converter Web Client
The web client is contained in the following servlet class:
tut-install/examples/ejb/converter/src/java/converter/web/ConverterServlet.java
A Java servlet is a web component that responds to HTTP requests.
The ConverterServlet class uses dependency injection to obtain a reference to ConverterBean. The javax.ejb.EJBannotation is added to the declaration of the private member variable converterBean, which is of type ConverterBean. ConverterBean exposes a local, no-interface view, so the enterprise bean implementation class is the variable type:
@WebServlet public class ConverterServlet extends HttpServlet { @EJB ConverterBean converterBean; ... }
When the user enters an amount to be converted to yen and euro, the amount is retrieved from the request parameters; then the ConverterBean.dollarToYen and the ConverterBean.yenToEuro methods are called:
... try { String amount = request.getParameter("amount"); if (amount != null && amount.length() > 0) { // convert the amount to a BigDecimal from the request parameter BigDecimal d = new BigDecimal(amount); // call the ConverterBean.dollarToYen() method to get the amount // in Yen BigDecimal yenAmount = converter.dollarToYen(d);
// call the ConverterBean.yenToEuro() method to get the amount
// in Euros
BigDecimal euroAmount = converter.yenToEuro(yenAmount);
...
} ... }
The results are displayed to the user.
Running the converter Example
Now you are ready to compile the enterprise bean class (ConverterBean.java) and the servlet class (ConverterServlet.java) and to package the compiled classes into a WAR file. You can use either NetBeans IDE or Ant to build, package, deploy, and run the converter example.
To Build, Package, and Deploy the converter Example in NetBeans IDE
- From the File menu, choose Open Project.
- In the Open Project dialog, navigate to:
tut-install/examples/ejb/ - Select the converter folder.
- Select the Open as Main Project check box.
- Click Open Project.
- In the Projects tab, right-click the converter project and select Deploy.
To Build, Package, and Deploy the converter Example Using Ant
- In a terminal window, go to:
tut-install/examples/ejb/converter/ - Type the following command:
ant all
This command calls the default task, which compiles the source files for the enterprise bean and the servlet, placing the class files in the build subdirectory (not the src directory) of the project. The default task packages the project into a WAR module: converter.war. For more information about the Ant tool, see Building the Examples.
Note - When compiling the code, the ant task includes the Java EE API JAR files in the classpath. These JARs reside in the modules directory of your GlassFish 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 Java EE API JAR files.
To Run the converter Example
- Open a web browser to the following URL:
http://localhost:8080/converter
The Servlet ConverterServlet page opens. - Type 100 in the input field and click Submit.
A second page opens, showing the converted values.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Legal Notices