Child pages
  • 5-minute Tutorial on Enterprise Application Development with Eclipse and Geronimo
Skip to end of metadata
Go to start of metadata

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.

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

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

    Geronimo version 3.0.x, Java 1.6 runtime, and Eclipse Indigo are used in this tutorial but other versions can be used instead (e.g., Geronimo version 2.2, Java 1.6, Eclipse Galileo)

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

Create the 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 Enterprise Application Project.
  2. In the EAR Application Project wizard type in SampleEAR as the Project name and select Apache Geronimo v3.0 in Target Runtime. Leave the rest as it is.



  3. Click Next.
  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 Creating 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

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

Create the EJB project

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

  1. Select File -> New, select EJB Project.

  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. Unselect the Create an EJB Client JAR module to hold the client interfaces and classes checkbox. We're not interested in it. Click Next.



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


  5. 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 the dynamic Web project

Now that you have EAR and EJB projects created the next step is to create a Dynamic Web project to hold your Web application.

  1. Select File -> New, select Dynamic Web Project.
  2. 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 and click Next twice.



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


  4. Click Finish.

You should now have the following project structure.


Create Stateless Session EJB

Every stateless session EJB has its own business interface. There are three types of business interfaces - @Remote, @Local and @WebService - and combinations of these three. EJB development starts from defining a business interface and implementing it in a bean implementation class. We do this in the next steps.

Create remote business interface

  1. Right click on the SampleEJB project and select New -> Interface and fill it in with the following values:
    • Package: sampleear
    • Name: RemoteBusinessInterface


  2. Click Finish.

Now we need to 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

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


  2. Click 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. In this section we create a jsp index.jsp page that executes a servlet MyServlet that in turn executes the ejb MyStatelessSessionBean.

Create welcome page index.jsp

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

  2. Change it so it executes the servlet upon form submission.
    index.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
    <!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=UTF-8">
    <title>5-minute Tutorial on Enterprise Application Development
    	with Eclipse and Geronimo</title>
    </head>
    <body>
    	<form action="MyServlet">
    		<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.

  1. Right click on the SampleWAR project and select Properties. Go to Java Build Path and click "Project"item, then click "Add" button to select the "SampleEJB" project and click OK.
    • Or select Properties, choose Deployment Assembly. Choose the Manifest Entries tab, then Add..., then Finish, Apply, OK.





  2. Right click on the SampleWAR project and select New -> Servlet and fill it in with the following values:
    • Java Package: sampleear
    • Class name: MyServlet


  3. Click Next.
  4. Change the URL Mapping section so the servlet serves at /sayHello url mapping and click Finish.



    MyServlet.java opens up automatically for editing after creation, update the servlet as shown below 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.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import sampleear.RemoteBusinessInterface;
    
    @WebServlet("/MyServlet")
    public class MyServlet extends HttpServlet {
    	private 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));
    	}
    
    }
    

  5. Update web.xml under WEB-INF directory of SampleWAR
    web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    	id="WebApp_ID" version="3.0">
    	<display-name>SampleWAR</display-name>
    	<welcome-file-list>
    		<welcome-file>index.jsp</welcome-file>
    	</welcome-file-list>
    	<servlet>
    		<display-name>MyServlet</display-name>
    		<servlet-name>MyServlet</servlet-name>
    		<servlet-class>sampleear.MyServlet</servlet-class>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>MyServlet</servlet-name>
    		<url-pattern>/MyServlet</url-pattern>
    	</servlet-mapping>
    </web-app>
    

  6. Check geronimo-web.xml under WEB-INF directory of SampleWAR
    geronimo-web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web:web-app xmlns:app="http://geronimo.apache.org/xml/ns/j2ee/application-2.0"
    	xmlns:bp="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    	xmlns:client="http://geronimo.apache.org/xml/ns/j2ee/application-client-2.0"
    	xmlns:conn="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2"
    	xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2" xmlns:ejb="http://openejb.apache.org/xml/ns/openejb-jar-2.2"
    	xmlns:jaspi="http://geronimo.apache.org/xml/ns/geronimo-jaspi"
    	xmlns:log="http://geronimo.apache.org/xml/ns/loginconfig-2.0"
    	xmlns:name="http://geronimo.apache.org/xml/ns/naming-1.2" xmlns:pers="http://java.sun.com/xml/ns/persistence"
    	xmlns:pkgen="http://openejb.apache.org/xml/ns/pkgen-2.1" xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0"
    	xmlns:web="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1">
    	<dep:environment>
    		<dep:moduleId>
    			<dep:groupId>sampleear</dep:groupId>
    			<dep:artifactId>sample-war</dep:artifactId>
    			<dep:version>1.0</dep:version>
    			<dep:type>war</dep:type>
    		</dep:moduleId>
    	</dep:environment>
    	<web:context-root>/sayHello</web:context-root>
    </web:web-app>
    

Deploy and run

All that's left before we test this sample is to deploy it. This task is done automatically for you when you choose to run the application directly from the eclipse workspace.

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



  2. Click Finish.
  3. 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 v3.0 Server at localhost and select Start.

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



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


  • No labels