Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

So you've got 5 minutes and want to see how much you can do with Eclipse and Geronimo v2, huh? If so, you won't find much here, then unless you have already installed 2, don't you? Install Eclipse and Geronimo Eclipse plugin as described at Geronimo Eclipse Plugin Installation Instructions. It makes a huge difference. and 5 minutes seem quite enough. Let's check it out!

Read some introductory material on how to define Geronimo in Eclipse at Geronimo Eclipse Plugin Usage Instructions

Create Enterprise Application Project

Start from creating an enterprise application project. Select File > New, select Project... (or alternatively press Ctrl-N) and in the popup window select select Enterprise Application Project in J2EE category.

geronimo-sample-ear.PNGImage Added

Press Next.

In the EAR Application Project wizard type in SampleEAR as the project name and select Apache Geronimo v2.0 in Target Runtime. Leave the rest as is.

Image Added

Press Next twice.

In the New EAR Application Project window select the Generate Deployment Descriptor checkbox.

Image Addedgeronimo-sample-ear-details.PNG

Press Next twice.

Fill in the Geronimo Deployment Plan's fields with the following values (you want to know more why they're important? Read it on in the Geronimo documentation - http://cwiki.apache.org/GMOxDOC12/deployment-plans.html1.2 Documentation - Deployment plans):

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

Image Addedgeronimo-sample-ear-plan.PNG

Press Finish.

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

geronimo-sample-ear-javaee-perspective.PNGImage Added

You should now have the following project structure.geronimo-sample-ear-created.PNG

Image Added

Note

Don't worry about the error cvc-complex-type.2.4.b: The content of element 'application' is not complete... for now. You'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. Press Ctrl-N and select EJB Project in EJB category.

geronimo-sample-ejb.PNGImage Added

Press Next.

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.

geronimo-sample-ejb-details.PNGImage Added

Press Next.

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

geronimo-sample-ejb-details-project-facets.PNGImage Added

Press Next.

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

Image Addedgeronimo-sample-ejb-details-clientintfs.PNG

Press Next.

Fill in the Geronimo Deployment Plan fields with the following values (want more why they're important? Read it on in the Geronimo documentation - http://cwiki.apache.org/GMOxDOC12/deployment-plans.html):

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

Image Addedgeronimo-sample-ejb-plan.PNG

Press Finish.

You should now have the following project structure.

geronimo-sample-ejb-created.PNGImage Added

Note

Remove ejbModule/META-INF/openejb-jar.xml file in the SampleEJB project as it leads to causes deployment issues.

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.

geronimo-sample-war.PNGImage Added

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.

geronimo-sample-war-details.PNGImage Added

Press Next.

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

geronimo-sample-war-details-project-facets.PNGImage Added

Press Next twice.

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

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

Image Addedgeronimo-sample-war-plan.PNG

Press Finish.

You should now have the following project structure.

geronimo-sample-war-created.PNGImage Added

Create Stateless Session EJB

Every stateless session ejb has its own business interface. There're three types of business interface interfaces - @Remote, @Local and @WebService - and their combinations. As you're in hurry (it's a 5-minute tutorial and that's 5 minutes seemed to be fine for a few lovely moments with Geronimo), suffice to Suffice to say that every EJB development starts from defining one 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 as follows:with the following values:

  • Package: sampleear
  • Name: RemoteBusinessInterface

Image Addedgeronimo-sample-ejb-RemoteBusinessInterface.PNG

Press Finish.

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

Code Block
java
java
borderStylesolid
titleRemoteBusinessInterface.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 as follows. Don't forget to add the interface RemoteBusinessInterface.with the following values:

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

Image Addedgeronimo-sample-ejb-MyStatelessSessionBean.PNG

Press Finish.

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

Code Block
java
java
borderStylesolid
titleMyStatelessSessionBean.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.

Code Block
html
html
borderStylesolid
titleindex.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 introductionTutorial toon enterpriseEnterprise applicationApplication developmentDevelopment 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).

geronimo-sample-war-dependency.PNGImage Added

Press OK.

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

  • Java Package: sampleear
  • Class name: MyServlet

Image Addedgeronimo-sample-war-MyServlet.PNG

Press Next.

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

geronimo-sample-war-url-mapping.PNGImage Added

Press Finish.

Change it to call off the ejb when executed.

Code Block
java
java
borderStylesolid
titleMyServlet.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));
    }
}

Create application.xml

Note

It appears that the file is not created by default and the application could not be deployed to any Java EE 5-compliant application server, Geronimo including. We need to define it manually. Don't know whether it's a bug in Eclipse or Geronimo Eclipse Plugin.

Create application.xml deployment descriptor in EarContent/META-INF directory of the SampleEAR project as follows.

...

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.

Image Addedgeronimo-sample-ear-definenewserver.PNG

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.

geronimo-sample-ear-geronimostart.PNGImage Added

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/SampleWARImage Removed.

geronimo-sample-running-welcomepage.PNGImage Added

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

geronimo-sample-running-buttonpressed.PNGImage Added

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