25396096 Step by Step Create Simple Calculator Using Eclipse Galileo EJB 3 0



Comments



Description

Step By StepCreate Simple Calculator Using Eclipse Galileo & EJB 3.0 Created by : SAIDY Fouad http://fouad-saidy.blogspot.com [email protected] 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 ) 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.GA ( download http://sourceforge. Click add Button then choose JBoss--> JBoss v5.0 Page |2 ➢ JBoss-5.1.1.Step By Step Create Simple Calculator using Eclipse Galileo & EJB 3.0. and then choose Server --> Runtime Environments.net/projects/jboss/files/JBoss/JBoss-5.0.0. not the Content of the directory! : You added the Jboss Server to Eclipse. and an author web client application 1. or add it if it doesn't exist: .GA/ ) We will create an application in the server side based on EJB 3. 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.0 and check" Create a new local server" In Application Server Directory select the JBoss Server directory. . 1.Step By Step Create Simple Calculator using Eclipse Galileo & EJB 3. we will create source code in ejbModule directory: We finished creating EJB project. Select File-->New--> Other. EJB Application: In this application we will create: ➢ Remote Interface calc. create the interface calc with package ejb: . and choose the JBoss v5 in target field: Now we created a simple EJB project. Then create EJB Project: Specify the name of the project for example SimpleCalculator . ➢ Class calcImpl which implements calc.0 Page |3 We just finished configuration step let's move now to programming an EJB Application. float b) { return a-b.float b). its name: SimpleCalculatorWeb Add a JSP page called index to the WebContent directory: . } } } Now we finished EJB Application let's move to The Web Client Application. don’t forget to specify the same package (ejb).float b). }catch(Exception e){ System.ejb. public float mult(float a.float b). and add this code to class: package ejb.println("Error:devision by 0!!"). } public float mult(float a. public float div(float a. } public float minus(float a. 1. Web Client Application: Select File-->New-->Dynamic Web Project.0 Page |4 Add this code to your calc interface: package ejb.float b) { return a+b. import javax.out.Step By Step Create Simple Calculator using Eclipse Galileo & EJB 3. } Next step is to create calcImpl with the same way. } public float div(float a.float b).float b) { return a*b. public float minus(float a.Stateless. @Local public interface calc { public float sum(float a.ejb. import javax.Local.float b) { try { return a/b. @Stateless(mappedName="Firstcalc") public class calcImpl implements calc { public float sum(float a. return 0. Step By Step Create Simple Calculator using Eclipse Galileo & EJB 3. if( request.getAttribute("a").parseFloat(request.jsp: <h2> <b> Hello World To The Simple Calculator </b> </h2> <% float a=2. %> <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.getAttribute("error")%></font> Now create a calServlet with web package in the JavaResources :src directory : Servlet is created like this: .getAttribute("result")%> </font> <br/> <font color='red' >Error: <%=session.toString()).b=1.getAttribute("b").-</b><input type='radio' name="oper" value='-' /></li> <li><b>*</b><input type='radio' name="oper" value='*' /></li> <li>&nbsp.parseFloat(request.toString()).getAttribute("a")!=null) a=Float. <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.0 Page |5 Add this form to index. if (request.getAttribute("b")!=null) b=Float. break.IOException.forward(request.lookup("Firstcalc").minus(a.naming.setAttribute("erreur: ".*.getParameter("n1")).getServletContext().div(a. import javax. case '*': result =cl.parseFloat(request.charAt(0).sum(a.servlet.*.getRequestDispatcher("/index. case '-': result =cl.*.0 Page |6 Add those imports to your servlet class: import java.e.setAttribute("result". 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 : .servlet. b). b).jsp").*. a). break. char oper=request.result). Copy this code to the doGet function: HttpSession session=request.mult(a.io. RequestDispatcher rd=this.parseFloat(request.http. break.getParameter("oper"). request. // call the calcImpl class of the SimpleCalculator EJB with the mappedName calc cl=(calc) ctx. case '/': result =cl.getMessage()). float a=Float.getSession(true). float b=Float. float result=0. import javax.servlet. request. } session.response). import javax.Step By Step Create Simple Calculator using Eclipse Galileo & EJB 3. import javax.setAttribute("b". break. try { Context ctx=new InitialContext().getParameter("n2")). } rd. b). b).setAttribute("a". }catch(NamingException e) { session. b). switch(oper) { case '+': result=cl. calc. firstly play the JBoss Server in Server section: Deploying the EJB Project: . Let’s deploy the two projects.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. 0 Page |8 With the same way deploy SimpleCalculatorWeb: Congratulation this is your first simple Calculator using EJB 3.com.Step By Step Create Simple Calculator using Eclipse Galileo & EJB 3. If you have any remarks or questions don’t hesitate to alert me by email: fouadsaidy@gmail. .0 ^^.
Copyright © 2024 DOKUMEN.SITE Inc.