HomeDocumentation > Developing > Tutorials > Developing Web applications > Accessing EJB in Web applications

To run this tutorial, as a minimum you will be required to have installed the following prerequisite software:

  1. Sun JDK 6.0+ (J2SE 1.6)
  2. Eclipse IDE for Java EE Developers, which is platform specific
  3. Apache Geronimo Eclipse Plugin 2.1.x
  4. Apache Geronimo Server 2.1.x

    Geronimo version 2.1.x, Java 1.5 runtime, and Eclipse Ganymede are used is used in this tutorial but other versions can be used instead (e.g., Geronimo version 2.2, Java 1.6, Eclipse Europa)

Details on installing eclipse are provided in the Development environment section. This tutorial is organized in the following sections:

Define a new Geronimo Server Runtime

  1. Launch Eclipse and Select New -> Other -> Server -> Server and select Next.




  2. Select Apache -> Apache Geronimo v2.1 Server and select Next.




  3. Specify the installation directory of an existing Geronimo server installation and select Finish.




  4. Start the server in Eclipse from the J2EE perspective.



Creating a EJB Project

  1. Launch Eclipse and right click under the Project Explorer. Select New -> EJB Project.




  2. Name the project as CurrencyConvertEJB and select Next.

    !


  3. Use the default values and select Finish.





  4. Right click on ejbModule and select New -> Interface.




  5. Name the package as ejb and the Interface as Converter. Select Finish.




  6. Right click on ejb package and select New -> Class.




  7. Name the class as ConverterBean and select Finish.

Creating a Dynamic Web Project

  1. Launch Eclipse. Select File -> New -> Project.




  2. Select Web -> Dynamic Web Project. Select Next.




  3. On the next screen, name the project as WebEJB.




  4. Select default values for all other fields. Finally select Finish.




  5. Right click on WebContent and select New -> JSP.




  6. Name the JSP index.jsp. Select Next --> Finish.







  7. Right click on Java Resources and select New -> Other.



  8. Select Web -> Servlet. Select Next.




  9. Name the package as webejb and Servlet as ConverterHandler. Select Next -> Finish.





Adding code to EJB Project

  1. Add the following code to the interface Converter.java.
    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);
    }
    
    Here the @Remote annotation is used to declare it as a Remote Interface.
  2. Add the following code to the class ConverterBean.java
    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);
    	}
    }
    
    Here the @Stateless annotation declares the POJO(Plain Old Java Object) as a Stateless session bean.
    This complete the EJB code.

Adding code to Web Project

  1. 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>
    
  1. 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

  1. Modify the deployment plan that is geronimo-web.xml as follows:
    geronimo-web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1" 
    xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.2" 
    xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0" 
    xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">
    
        <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>
    
    We have added a dependencies element (i.e., <dependencies>) and an ejb reference element (i.e., <ejb-ref>) to geronimo-web.xml. This step is required to make our CurrencyConvertEJB visible to our web application.
  2. 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.

















Deploy and Run

  1. Right click on the Geronimo server in the J2EE perspective and select Add and Remove Projects...

  2. There should be 2 projects in your workspace. Select Add All >> and then select Finish.




  1. Launch the browser in Eclipse (use the Globe icon at the top of the Eclipse main window) and run the application using http://localhost:8080/WebEJB/. This will give the following page.




  2. Give the amount as 15 and select Submit.




  3. This will convert the Dollar amount to Rupees and Euro.