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

Compare with Current View Page History

« Previous Version 3 Next »

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 Eclipse and Geronimo Eclipse plugin as described at Geronimo Eclipse Plugin Installation Instructions. It makes a huge difference. 5 minutes seem 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.PNG

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.

geronimo-sample-ear-details.PNG

Press Next twice.

Fill in the Geronimo Deployment Plan's 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-ear

geronimo-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.PNG

You should now have the following project structure.

geronimo-sample-ear-created.PNG

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

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

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

Press Next.

Unselect the Create an EJB Client JAR module to hold the client interfaces and classes checkbox.

geronimo-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

geronimo-sample-ejb-plan.PNG

Press Finish.

You should now have the following project structure.

geronimo-sample-ejb-created.PNG

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.

geronimo-sample-war.PNG

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

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

Press Next twice.

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

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

geronimo-sample-war-plan.PNG

Press Finish.

You should now have the following project structure.

geronimo-sample-war-created.PNG

Create Stateless Session EJB

Every stateless session ejb has its own business interface. There're three types of business interface - @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 say that every EJB development starts from defining one 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:

geronimo-sample-ejb-RemoteBusinessInterface.PNG

Press Finish.

Add a business method and mark the interface as a remote one.

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 as follows. Don't forget to add the interface RemoteBusinessInterface.

geronimo-sample-ejb-MyStatelessSessionBean.PNG

Press Finish.

Implement the business method sayHello and mark the class as a stateless session bean.

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

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

geronimo-sample-war-dependency.PNG

Press OK.

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

geronimo-sample-war-MyServlet.PNG

Press Next.

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

geronimo-sample-war-url-mapping.PNG

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));
    }
}

Create application.xml

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.

application.xml
<?xml version="1.0" encoding="ASCII"?>
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 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 time to give it a shot and see if it works.

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.

geronimo-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.PNG

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. Stop the timer and let us know how much it actually took.

  • No labels