You are on page 1of 8

Step By Step

Create Simple Calculator

Using Eclipse Galileo & EJB 3.0

Created by :
SAIDY Fouad http://fouad-saidy.blogspot.com fouadsaidy@gmail.com

Step By Step Create Simple Calculator using Eclipse Galileo & EJB 3.0
This is a simple tutorial to show you by examples how to create your first EJB Application in Eclipse Galileo using EJB 3.0. We will use in this tutorial:

Eclipse Galileo IDE (download http://www.eclipse.org/downloads/download.php? file=/technology/epp/downloads/release/galileo/SR1/eclipse-jee-galileo-SR1-win32.zip )

Step By Step Create Simple Calculator using Eclipse Galileo & EJB 3.0

Page |2

JBoss-5.1.0.GA ( download http://sourceforge.net/projects/jboss/files/JBoss/JBoss-5.1.0.GA/ )

We will create an application in the server side based on EJB 3.0, and an author web client application

1. Configuration JBoss Server:


Configuration JBoss Server: The second step is to configuarate the Jboss server in Eclipse Galieo IDE: In Eclipse Menu choose Window--> Preferences, and then choose Server --> Runtime Environments. Click add Button then choose JBoss--> JBoss v5.0 and check" Create a new local server"

In Application Server Directory select the JBoss Server directory, not the Content of the directory! :

You added the Jboss Server to Eclipse; now let's add the Jboss Server to this section: Select the Server section click and choose new->Server then next until finish

The server appears like this in Server section:

Now we will verify that Jdk is the default installed JRE's : Select Window-->Preferences in this windows select Java--> Installed JREs Check JDK if is not checked, or add it if it doesn't exist:

Step By Step Create Simple Calculator using Eclipse Galileo & EJB 3.0

Page |3

We just finished configuration step let's move now to programming an EJB Application.

1.

EJB Application:

In this application we will create: Remote Interface calc,

Class calcImpl which implements calc.

Select File-->New--> Other.. Then create EJB Project:

Specify the name of the project for example SimpleCalculator , and choose the JBoss v5 in target field:

Now we created a simple EJB project, we will create source code in ejbModule directory:

We finished creating EJB project; create the interface calc with package ejb:

Step By Step Create Simple Calculator using Eclipse Galileo & EJB 3.0

Page |4

Add this code to your calc interface:

package ejb; import javax.ejb.Local; @Local public interface calc { public float sum(float a,float b); public float mult(float a,float b); public float minus(float a,float b); public float div(float a,float b); } Next step is to create calcImpl with the same way, dont forget to specify the same package (ejb), and add this code to class: package ejb; import javax.ejb.Stateless; @Stateless(mappedName="Firstcalc") public class calcImpl implements calc { public float sum(float a,float b) { return a+b; } public float mult(float a,float b) { return a*b; } public float minus(float a,float b) { return a-b; } public float div(float a,float b) { try { return a/b; }catch(Exception e){ System.out.println("Error:devision by 0!!"); return 0; } } } Now we finished EJB Application let's move to The Web Client Application.

1.

Web Client Application:

Select File-->New-->Dynamic Web Project, its name: SimpleCalculatorWeb

Add a JSP page called index to the WebContent directory:

Step By Step Create Simple Calculator using Eclipse Galileo & EJB 3.0

Page |5

Add this form to index.jsp: <h2> <b> Hello World To The Simple Calculator </b> </h2> <% float a=2,b=1; if (request.getAttribute("a")!=null) a=Float.parseFloat(request.getAttribute("a").toString()); if( request.getAttribute("b")!=null) b=Float.parseFloat(request.getAttribute("b").toString()); %> <form method="post" action="calcServlet"> <b>Number 1:</b><input type='text' name='n1' value="<%=a%>" /> <br/> <b>Number 2:</b><input type='text' name='n2' value="<%=b%>" /> <br/> <u><b>Options:</b></u> <br/> <ul> <li><b>+</b><input type='radio' name="oper" value='+' checked /></li> <li><b>&nbsp;-</b><input type='radio' name="oper" value='-' /></li> <li><b>*</b><input type='radio' name="oper" value='*' /></li> <li>&nbsp; <b>/</b><input type='radio' name="oper" value='/' /></li> </ul> <b>-------------------------------------------</b> <br/> <input type="submit" value="Executer" /> </form>

<font color='blue'><b>Result is: </b> <%=session.getAttribute("result")%> </font> <br/> <font color='red' >Error: <%=session.getAttribute("error")%></font> Now create a calServlet with web package in the JavaResources :src directory :

Servlet is created like this:

Step By Step Create Simple Calculator using Eclipse Galileo & EJB 3.0

Page |6

Add those imports to your servlet class: import java.io.IOException; import javax.naming.*; import javax.servlet.*; import javax.servlet.*; import javax.servlet.http.*;

Copy this code to the doGet function: HttpSession session=request.getSession(true); RequestDispatcher rd=this.getServletContext().getRequestDispatcher("/index.jsp"); float a=Float.parseFloat(request.getParameter("n1")); float b=Float.parseFloat(request.getParameter("n2")); char oper=request.getParameter("oper").charAt(0); float result=0; try { Context ctx=new InitialContext(); // call the calcImpl class of the SimpleCalculator EJB with the mappedName calc cl=(calc) ctx.lookup("Firstcalc"); switch(oper) { case '+': result=cl.sum(a, b); break; case '-': result =cl.minus(a, b); break; case '*': result =cl.mult(a, b); break; case '/': result =cl.div(a, b); break; } session.setAttribute("result",result); request.setAttribute("a", a); request.setAttribute("b", b); }catch(NamingException e) { session.setAttribute("erreur: ",e.getMessage()); } rd.forward(request,response); To let the web client application to reference the calc interface we must configure the build path of the project in adding the first project SimpleCalculator :

Step By Step Create Simple Calculator using Eclipse Galileo & EJB 3.0

Page |7

In the project section click add and select SimpleCalculator:

Now add this import to your servlet class:


import ejb.calc; Lets deploy the two projects, firstly play the JBoss Server in Server section:

Deploying the EJB Project:

Step By Step Create Simple Calculator using Eclipse Galileo & EJB 3.0

Page |8

With the same way deploy SimpleCalculatorWeb:

Congratulation this is your first simple Calculator using EJB 3.0 ^^. If you have any remarks or questions dont hesitate to alert me by email: fouadsaidy@gmail.com.

You might also like