Thursday, January 6, 2011

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



We will use in this tutorial:
we will create an application in the server side based on EJB 3.0, and an author web client application 
  1. Server Application Using EJB:
  • Configuration JBoss Server:
The second step is to configuarate the Jboss server in Eclipse Galieo IDE: 
in Eclipse Menu choose Window--> Preferences, 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 clik and choose new->Server then next 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
then if jdk is not checked check it ,or add it if it doesn't exist:
 
we finished configuration step let's move now to programming an
EJB Application.
 
  • 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 exampleSimpleCalculator , 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 first :
 
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 forgit 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 , it's name: SimpleCalculatorWeb 
add a jsp page called index to the WebContent directory : 
 
add this form to index.jsp:













now create a calServlet with web package in theJavaResources :src directory : 
servlet is created like this: 
 
add those import 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 the web client application to know the calc interface we must configure the build path of the project in adding the first project SimpleCalculator :







    in the project section click add  and select SimpleCalculator: 

    now add this import to your servlet class :
    import ejb.calc;


    let's deploy the two project ,firstly play the JBoss Server in Server section:

    deploying the EJB Project :


    with the same way deploy SimpleCalculatorWeb:

    if you have any remarks or questions dont hesitate to post it.

    No comments:

    Post a Comment