Saturday, September 8, 2012

Create Enterprise Application with JBOSS and Netbeans

We have discussed about how install jboss and how to start jboss with netbeans in previous topics. We are going to discuss about how create an enterprise application, how to create an EJB , how to call that EJB from a servlet and how to deploy that enterprise application into jboss server.
As the first step, lets see how to create an enterprise application in netbeans.
Open netbeans add go to file and click on New Project. Then click on JAVA EE and select Enterprise Application as show in figure below.
Then click next. After that add a name for your enterprise application and click next. Then select jboss-as as the server and select create EJB module and create web module tick buttons. Now click Finish. Now you will see three new nodes appear project tab.
Then we need to create an EJB. We are going to create stateless session bean in this example. My enterprise application name is "test". Then you can see a node called "test-ejb" in the project tab. Right click on that tab and click session bean.
Now you will see a window like below. Give a suitable name to your session bean. Then specify a package name. Then select stateless and local as shown below.
 Now you will see two classes created under source package-> ejb directory. click on testSessionBean.java  and right click on work area and select add business method as shown in the following figure.
Then you will see a windows like below figure. We are going to create small session bean that returns addition of two numbers. So we need to get two integer values as parameters and we need to return the addition of those two numbers as a integer.
Now you will see a method call businessMethod in the testSessionBean.java and testSessionBeanLocal.java classes. Change the return statement as follows. Then that method will return the addition of given two numbers.

 public int businessMethod(int num1, int num2) {
        return num1+num2;
    }

Then save the java file. We created a session bean. Now we need to create a web application. This web app will use above session bean.
Right click on testApp-war node in project tab and go to New and select Servlet. Then you will get a window. Give a suitable name for your servlet such as testServlet and give a package name. Then click next and click finish on next window. Now we can see a new servlet java class created under the source package and your package name directory.
Now we need to call above sessionBean. So right click on work area and select insert code as mentioned above. Then select call Enterprise Bean. Then you will see a window and you have to select our ejb that we created as shown in the figure.
Now you will see following code added to the servlet class.


@EJB
    private testSessionBeanLocal testSessionBean;

Since we need to call session bean, we need to add following code in side the try catch block that created in the servlet.


            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet testServlet</title>");
            out.println("</head>");
            out.println("<body>");
             out.println("<h1>Addition of 10 and 15 is "+testSessionBean.businessMethod(10, 15)+"</h1>");
            out.println("</body>");
            out.println("</html>");

We are almost finished. Now we need to deploy our enterprise application to the server. Right click on testApp node in the project tab and click deploy. Then deploy our web application and start the jboss server. After deploying we can access our web application through a web browser. Open a web browser and enter following URL.
http://localhost:8080/testApp-war/testServlet

Now you will see the addition of 10 and 15 display in web browser.

Cheers..!!


No comments:

Post a Comment