EJB 3 Tutorial Using RAD7.5 and WebSphereWeb Age Solutions Inc. IntroductionThis tutorial will show you how to use RAD7.5 beta to develop EJB3 applications. Download and Install RAD 7.5 BetaDownload the code from the RAD7.5 Open Beta home page. For more information and help, use the open beta forum. Launch RAD7.5From the Start menu, choose Programs->IBM Software Delivery Platform->IBM Rational Application Developer 7.5->IBM Rational Application Developer.
Enter a workspace folder name. Note: It is a good idea to use a short path name for the workspace to avoid Windows 255 character file name restriction.
Close the Welcome view. Create an EJB ProjectFrom the menubar, select File->New->Other.
Choose EJB->EJB Project. Click Next.
Enter these values: Project name: TutorialEJB Click Next.
Leave the Java Persistence facet unchecked for now. In this tutorial, we will develop session EJBs only. Click Next.
By default, system will create a separate Java project to store remote interface and generated classes like stubs. These artifacts are required to compile and execute a client application. RAD refers to it as the client JAR project. In our case, system will create the TutorialEJBClient project. Click Finish to create the EJB project and the enterprise application (EAR) project. Create a EJB3 Session EJBRAD7.5 lets you develop EJB 2.1 as well as EJB 3 compliant EJBs. The steps are quite different. Now, we will develop a EJB 3 compliant session EJB. In EJB 3, a session bean does not need home interface. A remote interface is also optional but highly recommended. First, we will create the remote interface. The remote interface needs to be create in the client JAR project. Right click the TutorialEJBClient project and select New->Interface.
Enter com.webage.ejbs as the package name. Enter HelloRemote as the interface name. Click Finish. In the interface file, enter the code shown in bold face. @Remote The @Remote annotation makes this otherwise basic interface a remote interface. Note, how you don't have to add special feature to the interface (for example, the method does not have to throw RemoteException). Press Control+Shift+O to organize package names. This will import the javax.ejb.Remote interface name for the @Remote annotation. Save and close the file. Now, we will create the bean class in the EJB project. Right click the TutorialEJB project and select New->Class.
Enter com.webage.ejbs as the package name and HelloBean as the class name. Click Finish. System will open the class file. First of all, we must designate the class as a stateless session EJB class using the @Stateless annotation.
RAD 7.5 supports code completion for annotations. Feel free to use it to add the @Stateless annotation at the class level. @Stateless } Now, we will make the class implement the remote interface. Add the code shown in bold face. public class HelloBean implements HelloRemote {
Note: In EJB3, you can explicitly specify the name of the remote interface for an EJB using the @Remote annotation. This is necessary, only when an EJB supports both remote and local interface. Otherwise, as in our case here, system intelligently deduces that the EJB supports a remote interface and that it's name is HelloRemote. Now, we will add the implementation of the sayHello() method. Right click on the editor and select Source->Override/Implement Method.
System will automatically select the unimplemented methods (sayHello() in this case). Click OK. Enter the code as shown in boldface below. public String sayHello(String name) {
Now, press Control+Shift+O to organize imports. Save the file. That's all there is to it. Develop a ClientThe Universal Test Client (UTC) of RAD can not be used to test a EJB3 EJB. We must code the client. Now, we will develop a Servlet that will act as the client of the EJB. First, we will create a web project. From the menu bar, select File->New->Other.
Select Web->Dynamic Web Project. Click Next.
Enter TutorialWeb as the project name. Make sure TutorialApp is selected as the application project. Click Finish.
Click No. Before we can create the client, we must add a dependency on the EJB client JAR project. Right click the TutorialWeb project and select Properties.
Select the J2EE Module Dependencies property. Then, check TutorialEJBClient.jar. Click OK. What does that do? When the web project is exported as a WAR file, system automatically includes the TutorialEJBClient.jar file in the WEB-INF/lib folder of the WAR file. This allows the classes in the web module find the remote interfaces of the EJBs. Now, we can create the Servlet. Right click the TutorialWeb project and select New->Servlet.
Enter com.webage.servlet as the package name. Enter TestServlet as the class name. Click Finish. In the Servlet class, add a EJB reference using the @EJB annotation as shown in boldface below. public class TestServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
Note a few items:
Press Control+Shift+O to organize imports. Now, implement the doGet() method as shown below. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Save changes. Now, we are ready to test the servlet. TestStart WebSphere Application Server v6.1.
After the server starts, deploy the TutorialApp application to the server. To do that, right click the server in the Servers view and select Add Remove Projects. Wait for the publishing process to end. Right click TestServlet.java in the Project Explorer view. Select Run As->Run on Server.
ReviewIn this tutorial, we learned how to create a simple EJB 3 stateless session EJB using RAD 7.5. The annotations make it easy to develop the EJB and the client. Feedback |