Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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.

...

You should now have the following project structure.

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.

...

Note

Remove META-INF/openejb-jar.xml file as it leads to 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.

...

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:

...

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 with the following values:

...

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.

...

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 introduction to 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.

...

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.

...

Code Block
xml
xml
borderStylesolid
titleapplication.xml
<?xml version="1.0" encoding="ASCII"?>
<application xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:application="http://java.sun.com/xml/ns/javaee/application_5.xsd"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd"
  version="5">
  <display-name>SampleEAR</display-name>
  <module>
    <ejb>SampleEJB.jar</ejb>
  </module>
  <module>
    <web>
      <web-uri>SampleWAR.war</web-uri>
      <context-root>/SampleWAR</context-root>
    </web>
  </module>
</application>

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!

...