|
| Home > Apache Geronimo v2.1 > Documentation > Developer's guide > Tutorials > EJB applications > 5-minute Tutorial on Enterprise Application Development with Eclipse and Geronimo |
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.
Details on installing eclipse are provided in the Development environment section. This tutorial is organized in the following sections:
Start from creating an enterprise application project from the eclipse workspace.
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). |
The next step is to create an EJB project to hold your EJBs.
You should now have the following project structure.
| Important When workng on versions pervious to Geronimo 2.1.7, 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. However, when using Geronimo 2.1.7, you must include this ejbModule/META-INF/openejb-jar.xml file or else you will encounter NullPointerException error due to a bug issue. |
Now that you have EAR and EJB projects created the next step is to create a Dynamic Web project to hold your web application.
You should now have the following project structure.
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. No need to say that every EJB development starts from defining a business interface and implementing it by a bean implementation class so, that is exactly what we will be doing in the next steps.
Now we need to add a business method and mark the interface as a remote one with @Remote annotation.
package sampleear; import javax.ejb.Remote; @Remote public interface RemoteBusinessInterface { public String sayHello(String name); }
Implement the business method sayHello and mark the class as a stateless session bean with the @Stateless annotation.
package sampleear; import javax.ejb.Stateless; @Stateless public class MyStatelessSessionBean implements RemoteBusinessInterface { public String sayHello(String name) { return getClass().getName() + " says hello to " + name + "."; } }
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.
<%@ 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>
Since the servlet calls the EJB, the web project the servlet is in depends on the EJB project. Let's define the dependency.
MyServlet.java opens up automatically for editing after creation, update the servlet as shown below to call off the ejb when executed.
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)); } }
All it's left before we test this sample it to deploy it. This task is done automatically for you when you choose to run the application directly from the eclipse workspace.
|
|
Privacy Policy - Copyright © 2003-2011, The Apache Software Foundation, Licensed under ASL 2.0. |