To run this tutorial, as a minimum you will be required to have installed the following prerequisite software.
- Sun JDK 5.0+ (J2SE 1.5)
- Eclipse 3.3.1.1 (Eclipse Classic package of Europa distribution), which is platform specific
- Web Tools Platform (WTP) 2.0.1
- Data Tools Platform (DTP) 1.5.1
- Eclipse Modeling Framework (EMF) 2.3.1
- Graphical Editing Framework (GEF) 3.3.1
Details on installing eclipse are provided in the Development environment section. This tutorial is organized in the following sections:
The application development will take you through the following
Creating a EJB Project
- Launch Eclipse and Right Click Under the Project Explorer. Select New->EJB Project.
- Name the project as CurrencyConvertEJB and Select Next.
- Use the default values and Select Finish.
- Right Click on ejbModule and Select New->Interface.
- Name the package as ejb and Interface as Converter. Select Finish.
- Right Click on ejb package and Select New->Class.
- Name the class as ConverterBean and select Finish.
Creating a Dynamic Web Project
- Launch Eclipse. Select File->New->Project.
- Select Web->Dynamic Web Project. Select Next.
- On the next screen give the name of the project as WebEJB.
- Select default values for all other fields. Finally select Finish.
- Right click on WebContent and Select New->JSP.
- Name the jsp as index.jsp. Select Next->Finish.
- Right Click on JavaResources and Select New->Other.
- Select Web->Servlet. Select Next.
- Name the package as webejb and Servlet as ConverterHandler. Select Next->Finish.
Adding code to EJB Project
- Add the following code to the interface Converter.java.
Here the @Remote annotation is used to declare it as a Remote Interface.Converter.java
package ejb; import java.math.BigDecimal; import javax.ejb.Remote; @Remote public interface Converter { public BigDecimal dollarToRupees(BigDecimal dollars); public BigDecimal rupeesToEuro(BigDecimal rupees); }
- Add the following code to the class ConverterBean.java
Here the @Stateless annotation declares the POJO(Plain Old Java Object) as a Stateless session bean.ConverterBean.java
package ejb; import java.math.BigDecimal; import javax.ejb.*; @Stateless public class ConverterBean implements Converter { private BigDecimal rupeeRate = new BigDecimal("40.58"); private BigDecimal euroRate = new BigDecimal("0.018368"); public BigDecimal dollarToRupees(BigDecimal dollars) { BigDecimal result = dollars.multiply(rupeeRate); return result.setScale(2, BigDecimal.ROUND_UP); } public BigDecimal rupeesToEuro(BigDecimal rupees) { BigDecimal result = rupees.multiply(euroRate); return result.setScale(2, BigDecimal.ROUND_UP); } }
This complete the EJB code.
Adding code to Web Project
- Add the following code to index.jsp.
index.jsp
<html> <head> <title>Converter</title> </head> <body bgcolor="white"> <h1>Converter</h1> <hr> <p>Enter an amount to convert:</p> <form method="get" action="index.jsp"> <input type="text" name="amount" size="25"><br> <p> <input type="submit" value="Submit"> <input type="reset" value="Reset"> </form> <jsp:include page="/ConverterHandler" /> </body>
- Add the following code to ConverterHandler servlet.
ConverterHandler.java
package webejb; import ejb.Converter; import java.io.IOException; import java.io.PrintWriter; import java.math.BigDecimal; import javax.ejb.EJB; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ConverterHandler extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { @EJB(name = "ejb/Converter") private Converter converter; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); String amount = request.getParameter("amount"); if (amount != null && amount.length() > 0) { BigDecimal d = new BigDecimal(amount); BigDecimal rupeeAmount = converter.dollarToRupees(d); out.println("<p>" + amount + " Dollars are " + rupeeAmount + " Rupees.<p>"); BigDecimal euroAmount = converter.rupeesToEuro(rupeeAmount); out.println(amount + " Dollars are " + euroAmount + " Euro."); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
The @EJB(name = "ejb/Converter") injects the EJB into the servlet.
This completes the code for Web project.
Some more configurations
- Modify the deployment plan that is geronimo-web.xml as follows
we have added a dependencies element and a ejb reference element(ejb-ref) to geronimo-web.xml. This step is required to make our CurrencyConvertEJB visible to our web application.geronimo-web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.1" xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.1" xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.1" xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.1"> <sys:environment> <sys:moduleId> <sys:groupId>default</sys:groupId> <sys:artifactId>WebEJB</sys:artifactId> <sys:version>1.0</sys:version> <sys:type>car</sys:type> </sys:moduleId> <sys:dependencies> <sys:dependency> <sys:groupId>default</sys:groupId> <sys:artifactId>CurrencyConvertEJB</sys:artifactId> <sys:version>1.0</sys:version> <sys:type>car</sys:type> </sys:dependency> </sys:dependencies> </sys:environment> <context-root>/WebEJB</context-root> <nam:ejb-ref xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.2"> <nam:ref-name>ejb/Converter</nam:ref-name> <nam:pattern> <nam:groupId>default</nam:groupId> <nam:artifactId>CurrencyConvertEJB</nam:artifactId> <nam:version>1.0</nam:version> <nam:name>ConverterBean</nam:name> </nam:pattern> </nam:ejb-ref> </web-app>
- Right Click on WebEJB project and select properties. Select Java Build Path->Projects. Click add and add CurrencyConvertEJB. This is required for the compilation of Client code. This is explained with the following screenshots.
- You need to correct the namespace for openejb-jar.xml.
Warning
Due to some limitations in Geronimo Eclipse Plugin the generated deployment plan(openejb-jar.xml) does not have the correct namespace. Replace the existing namespace as shown in the figure with the following
<openejb-jar xmlns="http://www.openejb.org/xml/ns/openejb-jar-2.2" xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.2" xmlns:pkgen="http://www.openejb.org/xml/ns/pkgen-2.0" xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.2" xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">
Deploy and Run
Warning
Due to limitation with Geronimo Eclipse Plugin, you will have to export the CurrencyConvertEJB EJB project as a jar.
- Export the CurrencyConvertEJB project as a jar.
- Start the server in Eclipse and Launch the Administrative console using http://localhost:8080/console.
- Enter default username and password.
- In the welcome page, under Applications. Select Deploy New.
- Browse and Select CurrencyConvertEJB.jar. Once done Select Install. This will deploy the EJB application on to server.
- In Eclipse right click on WebEJB project and Select Run As-> Run on Server. This will deploy the web application on to the server.
- Launch a browser and run the application using http://localhost:8080/WebEJB/. This will give the following page
- Give the amount as 15 and Select Submit.
- This will convert the Dollar amount to Rupees and Euro.