Stateless -Session Bean Prepared by: A.Saleem Raja MCA.,M.Phil.,(M.Tech) Lecturer/MCA Chettinad College of Engineering and Technology-Karur E-Mail: asaleemrajasec@gmail.com Creating an Enterprise Application Project using netbean Let's get started the code itself. The first thing to do is to create the Enterprise Application Project, or simply, EAR. To do that, back to the tab Projects and right mouse click and select option New Project. On the new screen, select the Category Enterprise and then the project Enterprise Application, click next to go to the next screen. On the next screen select the Project name and the Project Location. (In my example, I am going to use the name Test), click next again. On the third screen, select the server (by default Netbeans brings Glasshfish), choose the Java Version (Java EE 5 by default), and select the EJB and Web module. Chettinad College of Engineering and Technology-Karur Page 1
Note: By default, the EJB module's name is "EAR name" plus "-ejb". The Web module's name is "EAR name" plus "-war". However you are free to change these names. When the projects are created, they will be shown on the Projects tab. Creating our first EJB component (Stateless Session Beans) On the Projects tab, expand the EJB module (in my case called "Test-ejb"). You will see some options, such as: Enterprise Beans, Configuration Files, Server Resources and so on. We will work around the Enterprise Beans, therefore right mouse click on Enterprise Beans -> New -> Session Bean. Chettinad College of Engineering and Technology-Karur Page 2
On the screen that will come up, choose the EJB Name, Package, Session Type and Interface. For my example, I am using "TestEJB","stateless","Stateless","Remote". Click finish when your fields are filled. After the Session Bean is created, you will see the class (and the interface) in the Source Packages section, as well as the Bean in the Enterprise Beans section. In the main tab, the TestEJBBean.java will be opened automatically. Chettinad College of Engineering and Technology-Karur Page 3
Our Stateless Session Bean was created into the stateless package, but now we have to implement it. Open it (if it is closed) with double click on the TestEJBBean from Enterprise Beans section (you can open the file directly from the Source Package as well). In the body of the class there is a comment " // Add business logic below. (Right-click in editor and choose // "EJB Methods > Add Business Method" or "Web Service > Add Operation")" So right mouse click in the editor and choose EJB Methods > Add Business Method. A new screen will come up. In your example, we will have only one method (called getmessage()) with String return. Hence, put in the field name getmessage and return type: String. Chettinad College of Engineering and Technology-Karur Page 4
Done, our first EJB3 Session Bean object has been created. To verify the code it has created, you can open the class TestEJBBean (implementation) and the interface TestEJBRemote (remote interface). Different than Eclipse, where we put ourself the interface and the class name, Netbeans uses the pattern Object Name plus Remote or Local. Of course you can change it yourself The last thing to do is to implement the business method we just added. It is simple, only return a string message: Hello EJB World. The code looks like below: Chettinad College of Engineering and Technology-Karur Page 5
package stateless; import javax.ejb.stateless; @Stateless public class TestEJBBean implements TestEJBRemote { public String getmessage() { return "Hello EJB World"; } } Using the Web Module as EJB Client We have already created the Web Module previously when the EAR has been created, so let s use it as EJB Client. When the Web module is created, by default Netbeans already creates a file called index.jsp in the Web Pages section. Open this file to add a call to the servlet. The code looks like below: <%@page contenttype="text/html" pageencoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>jsp Page</title> </head> <body> <h2>hello World!</h2> <a href="testservlet">click here to call the EJB component</a> </body> </html> As you can see, we must create a servlet called TestServlet. it will call the EJB component when the link from User Interface is clicked. Chettinad College of Engineering and Technology-Karur Page 6
To create the servlet itself, right click on Web project (in my case Test-web), New -> Servlet. On the new screen, select the Class Name and the Package. On next screen, you can keep all options and click on Finish button. The Servlet will be created and its code will appear on the Main tab. Much easier than JBoss, in Glassfish there is the annotation @EJB. When the container sees this annotation, is inject the component directly in the Servlet, Java Class, Managed Bean, whatever. (In JBoss this annotation has not been implemented yet, so you must use JNDI to call a remote component). The @EJB annotation gets inside the attribute and can be used directly in the code. The code from Servlet looks like below: package servlets; import java.io.*; import javax.ejb.ejb; import javax.servlet.*; import javax.servlet.http.*; import stateless.testejbremote; public class TestServlet extends HttpServlet { //This annotation INJECTS the TestEJBRemove object from EJB //into this attribute @EJB private TestEJBRemote testejb; @Override protected void doget(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException { response.setcontenttype("text/html;charset=utf-8"); PrintWriter out = response.getwriter(); out.println("<html>"); Chettinad College of Engineering and Technology-Karur Page 7
out.println("<head>"); out.println("<title>servlet TestServlet</title>"); out.println("</head>"); out.println("<body>"); out.println(testejb.getmessage()); out.println("</body>"); out.println("</html>"); } } Look at the line #13. It has the @EJB annotation. Also, look at the body of doget method. No JNDI method was used, no code to call the EJB object was used, thus the code gets much clear than JNDI. Chettinad College of Engineering and Technology-Karur Page 8