This is a specific post to describe the steps to configure an environment in your IDE to create EJB applications, and deploy them successfully in a server. In the example are used:
IDE - Eclipse Luna
Server - glassfish 3.1
JDK - Java 1.7
Create EJB project
EJB project structure - example
*Note: Take into account that the EJB 3.1 API needs to be added to the build path
*Note: Take into account that the servlet3.0 API needs to be added to the build path
Make the EJB project visible to the web project Include the EJB project in the build path of the web project: -Select web project, right button. -Build path/Projects/ Add the EJB project
Important: to make the EJBs visible to the web project, the EJB project should be included in the deployment assembly: - Select web project, right button. - Deployment assembly/ Add: add EJB jar
OCEJBCD (SCBCD) - 1Z0-895 - Enterprise JavaBeans Developer Certification 3.1. Understand the purpose and role of JNDI in relation to EJB components JNDI : Java naming directory Binding- assigning a name to an EJB object which can be used later. Lookup- looking up and getting an object of EJB.
3.3. Use JNDI to look up a resource Accessing an enterprise bean can be done by dependency injection or JNDI lookup. Example using JNDI lookup: ExampleRemote example = (ExmapleRemote) InitialContext.lookup("java:global/myApp/ExampleRemote");
3.4. Write code that receives a resource reference through injection
The annotations for inject a resource are: @EJB- used to inject other EJB reference (on fields or on methods). @Resource- used to inject datasource or singleton services like sessionContext, timerService etc. We will see a complete code example in the followings sections (3.5- 3.7) and step by step we will create a javaEE application. This simple application is compound by:
Web client project : with a servlet and JSP
EJB server project :with sessionEJB and its interface.
The purpose will be create an application that allows a user selecting one color from a dropdown list. When submitting the selection a servlet will be executed and will call an EJB bean method. The bean method will return the color that will be displayed in the screen.
3.5. Create a session bean client We are going to define an stateless session bean with a method that will evaluate a number of options and it will return the hex code corresponding to one color: If input is 1 -- > color blue If input is 2 -- > color red If input is 3 -- > color yellow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3.6. Create a session facade http://www.oracle.com/technetwork/java/sessionfacade-141285.html Session facade provides a level of abstraction between the client and the business object. It manages the interactions between client and business services. For the session bean defined in the previous section we will define an interface, that will be used by the client to access to our bean.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3.7. Use dependency injection to locate an EJB Now, we will have to define the client that will call the bean (Servlet and JSP). JSP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Servlet class The servlet class will have a method doGet, that will be called everytime the button submit is clicked. The dependency injection to locate the bean is applied by annotation: @EJB TestBeanLocal colorBean;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
After deploying our application (web and EJB) in the server. When entering this url in the browser: http://localhost:8080/ColorWeb/myColor, the page is open:
When a color is selected the corresponding hex code and color are shown in the screen