You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 10 Next »

This tutorial walks you through configuring, developing and deploying an enterprise application with Eclipse and Geronimo. 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:

Create Enterprise Application Project

Start from creating an enterprise application project from the eclipse workspace.

  1. Select File -> New, select Project... and in the popup window select select Enterprise Application Project in J2EE category and click Next.



  2. In the EAR Application Project wizard type in SampleEAR as the Project name: and select Apache Geronimo v2.1 in Target Runtime. Leave the rest as it is.



  3. Click Next twice.
  4. In the J2EE Modules to Add to the EAR window select the Generate Deployment Descriptor checkbox and click Next.



  5. In the Geronimo Deployment Plan window enter the values as specified below. To find out more about what these values mean check the Deployment plans section.
    • Group Id: sampleear
    • Artifact Id: sample-ear
    • Version: 1.0
    • Artifact Type: ear




  6. Click Finish.

If asked about changing to the Java EE perspective, click Yes. You may want to select the Remember my decision checkbox to avoid dealing with it in the future.

You should now have the following project structure.


Error message

Don't worry about the error cvc-complex-type.2.4.b: The content of element 'application' is not complete... for now. We'll fix it in the next step when you define an ejb module (and webapp module afterwards).

Create EJB project

The next step is to create an EJB project to hold your EJBs.

  1. Press Ctrl-N, select EJB Project in EJB category and click Next.
  2. In the EJB Project wizard type in SampleEJB as the project name and select Add project to an EAR checkbox. Leave the rest as is and click Next.



  3. Make sure that 5.0 for the Java facet in the Project Facets popup window's selected and click Next.



  4. Unselect the Create an EJB Client JAR module to hold the client interfaces and classes checkbox. We're not interested in it. Click Next.



  5. Fill in the Geronimo Deployment Plan fields with the following values:
    • Group Id: sampleear
    • Artifact Id: sample-ejb
    • Artifact Type: ejb




  6. Click Finish.

You should now have the following project structure.


Important

Remove ejbModule/META-INF/openejb-jar.xml file in the SampleEJB project as it causes deployment issues. See the file highlighted in the image above.

Create Dynamic Web Project

The next step is to create a Dynamic Web project to hold your web application. Press Ctrl-N and select Dynamic Web Project in Web category.

Press Next.

In the Dynamic Web Project wizard type in SampleWAR as the project name and select Add project to an EAR checkbox. Leave the rest as is.

Press Next.

Make sure that 5.0 for the Java facet in the Project Facets popup window's selected.

Press Next twice.

Fill in the Geronimo Deployment Plan fields with the following values:

  • Group Id: sampleear
  • Artifact Id: sample-war
  • Artifact Type: war

Press Finish.

You should now have the following project structure.

Create Stateless Session EJB

Every stateless session ejb has its own business interface. There're three types of business interfaces - @Remote, @Local and @WebService - and their combinations. Suffice to say that every EJB development starts from defining a business interface and implementing it by a bean implementation class.

Create remote business interface

Right-click on the SampleEJB project and select New > Interface and fill it in with the following values:

  • Package: sampleear
  • Name: RemoteBusinessInterface

Press Finish.

Add a business method and mark the interface as a remote one with @Remote annotation.

RemoteBusinessInterface.java
package sampleear;

import javax.ejb.Remote;

@Remote
public interface RemoteBusinessInterface {
    public String sayHello(String name);
}

Create bean class

Right-click on the SampleEJB project and select New > Class and fill it in with the following values:

  • Package: sampleear
  • Name: MyStatelessSessionBean
  • Interfaces: sampleear.RemoteBusinessInterface

Press Finish.

Implement the business method sayHello and mark the class as a stateless session bean with the @Stateless annotation.

MyStatelessSessionBean.java
package sampleear;

import javax.ejb.Stateless;

@Stateless
public class MyStatelessSessionBean implements RemoteBusinessInterface {

    public String sayHello(String name) {
        return getClass().getName() + " says hello to " + name + ".";
    }
}

Web application development

The time has come to use the ejb in the web application. We create a jsp page index.jsp that executes a servlet MyServlet that in turn executes the ejb MyStatelessSessionBean.

Create welcome page - index.jsp

Right-click on the SampleWAR project and select New > JSP. Name it index.jsp. Press Finish.

Change it so it executes the servlet upon form submission.

index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>5-minute Tutorial on Enterprise Application Development with Eclipse and Geronimo</title>
  </head>
  <body>
    <form action="${pageContext.request.contextPath}/sayHello">
      <input type="text" name="name" /><input type="submit" value="Press me!" />
    </form>
  </body>
</html>

Create servlet - MyServlet

Since the servlet calls the EJB, the web project the servlet is in depends on the EJB project. Let's define the dependency.

Right-click on the SampleWAR project and select Properties. Go to J2EE Module Dependencies and select the checkbox next to SampleEJB.jar (it's in the J2EE Modules tab).

Press OK.

Right-click on the SampleWAR project and select New > Servlet and fill it in with the following values:

  • Java Package: sampleear
  • Class name: MyServlet

Press Next.

Change the URL Mapping section so the servlet serves at /sayHello url mapping.

Press Finish.

Change it to call off the ejb when executed.

MyServlet.java
package sampleear;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    static final long serialVersionUID = 1L;

    @EJB
    RemoteBusinessInterface remoteBusinessIntf;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        if (name == null || name.length() == 0) {
            name = "anonymous";
        }
        response.getWriter().write(remoteBusinessIntf.sayHello(name));
    }
}

Run it!

After a very hard 4-minute development it's time to give it a shot and see if it works. Not much time left so hurry up!

Right-click on the SampleEAR project and select Run As > Run on Server. When Run On Server popup window comes up, select the Always use this server when running this project checkbox. Leave the rest as is.

Press Finish.

The server's stopped so nothing happens (from a user's perspective at least). Open up the Servers tab and right-click on Apache Geronimo v2.0 Server at localhost and select Start.

After a few seconds, Geronimo should be up and running with the enterprise application published. Open up the browser of your choice and go to http://localhost:8080/SampleWAR.

Type in any name you want, e.g. Jacek and press Press me! button.

The tutorial's finished. Let us know how much it actually took and if it worked at all (wink)

  • No labels