geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Geronimo v2.2 > 5-minute Tutorial on Enterprise Application Development with Eclipse and Geronimo
Date Thu, 31 Dec 2009 07:21:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=GMOxDOC22&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/GMOxDOC22/5-minute+Tutorial+on+Enterprise+Application+Development+with+Eclipse+and+Geronimo">5-minute
Tutorial on Enterprise Application Development with Eclipse and Geronimo</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~daition@gmail.com">delos</a>
    </h4>
     
          <br/>
     <div class="notificationGreySide">
         <p>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.</p>

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

<ol>
	<li>Sun JDK 5.0&#43;</li>
	<li>Eclipse IDE for Java EE Developers, which is platform specific</li>
	<li>Apache Geronimo Eclipse Plugin 2.1.x</li>
	<li>Apache Geronimo Server 2.1.x
<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><p>Geronimo version 2.1.x,
Java 1.5 runtime, and Eclipse Ganymede are used in this tutorial but other versions can be
used instead (e.g., Geronimo version 2.2, Java 1.6, Eclipse Galileo)</p></td></tr></table></div></li>
</ol>


<p>Details on installing eclipse are provided in the <a href="/confluence/display/GMOxDOC22/Development+environment"
title="Development environment">Development environment</a> section. This tutorial
is organized in the following sections:</p>

<div>
<ul>
    <li><a href='#5-minuteTutorialonEnterpriseApplicationDevelopmentwithEclipseandGeronimo-Createtheenterpriseapplicationproject'>Create
the enterprise application project</a></li>
    <li><a href='#5-minuteTutorialonEnterpriseApplicationDevelopmentwithEclipseandGeronimo-CreatetheEJBproject'>Create
the EJB project</a></li>
    <li><a href='#5-minuteTutorialonEnterpriseApplicationDevelopmentwithEclipseandGeronimo-CreatethedynamicWebproject'>Create
the dynamic Web project</a></li>
    <li><a href='#5-minuteTutorialonEnterpriseApplicationDevelopmentwithEclipseandGeronimo-CreateStatelessSessionEJB'>Create
Stateless Session EJB</a></li>
<ul>
    <li><a href='#5-minuteTutorialonEnterpriseApplicationDevelopmentwithEclipseandGeronimo-Createremotebusinessinterface'>Create
remote business interface</a></li>
    <li><a href='#5-minuteTutorialonEnterpriseApplicationDevelopmentwithEclipseandGeronimo-Createbeanclass'>Create
bean class</a></li>
</ul>
    <li><a href='#5-minuteTutorialonEnterpriseApplicationDevelopmentwithEclipseandGeronimo-Webapplicationdevelopment'>Web
application development</a></li>
<ul>
    <li><a href='#5-minuteTutorialonEnterpriseApplicationDevelopmentwithEclipseandGeronimo-Createwelcomepage%7B%7Bindex.jsp%7D%7D'>Create
welcome page <tt>index.jsp</tt></a></li>
    <li><a href='#5-minuteTutorialonEnterpriseApplicationDevelopmentwithEclipseandGeronimo-CreateservletMyServlet'>Create
servlet - MyServlet</a></li>
</ul>
    <li><a href='#5-minuteTutorialonEnterpriseApplicationDevelopmentwithEclipseandGeronimo-Deployandrun'>Deploy
and run</a></li>
</ul></div>

<h1><a name="5-minuteTutorialonEnterpriseApplicationDevelopmentwithEclipseandGeronimo-Createtheenterpriseapplicationproject"></a>Create
the enterprise application project</h1>

<p>Start from creating an enterprise application project from the eclipse workspace.</p>

<ol>
	<li>Select <b>File &#45;&gt; New</b>, select <b>Project...</b>
and in the popup window select <b>Enterprise Application Project</b>.</li>
	<li>In the EAR Application Project wizard type in <em>SampleEAR</em> as
the Project name and select <b>Apache Geronimo v2.1</b> in <em>Target Runtime</em>.
Leave the rest as it is.
<br clear="all" />
<br clear="all" />  <img src="/confluence/download/attachments/93322/ganymede_ear_prj.jpg"
align="absmiddle" border="0" />
<br clear="all" />
<br clear="all" /></li>
	<li>Click <b>Next</b>.</li>
	<li>In the <em>J2EE Modules to Add to the EAR</em> window select the <b>Generate
Deployment Descriptor</b> checkbox and click <b>Next</b>.
<br clear="all" />
<br clear="all" />  <img src="/confluence/download/attachments/93322/ear_prj_2.jpg"
align="absmiddle" border="0" />
<br clear="all" />
<br clear="all" /></li>
	<li>In the <em>Geronimo Deployment Plan</em> window enter the values as
specified below. To find out more about what these values mean check the <a href="/confluence/display/GMOxDOC22/Creating+deployment+plans"
title="Creating deployment plans">Creating deployment plans</a> section.
<table class="sectionMacro" border="0" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><ul>
	<li>Group Id: <b>sampleear</b></li>
	<li>Artifact Id: <b>sample-ear</b></li>
	<li>Version: <b>1.0</b></li>
	<li>Artifact Type: <b>ear</b></li>
</ul>
</tr></tbody></table>
<p><img src="/confluence/download/attachments/93322/ear_prj_3.jpg" align="absmiddle"
border="0" />
<br clear="all" />
<br clear="all" /></p></li>
	<li>Click <b>Finish</b>.</li>
</ol>


<p>If asked about changing to the Java EE perspective, click <b>Yes</b>.
You may want to select the <b>Remember my decision</b> checkbox to avoid dealing
with it in the future.</p>

<p>You should now have the following project structure.
<br clear="all" />
<br clear="all" />  <img src="/confluence/download/attachments/93322/ear_struct.jpg"
align="absmiddle" border="0" />
<br clear="all" /></p>

<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Error message</b><br
/><p>Don't worry about the error <b>cvc-complex-type.2.4.b: The content of
element 'application' is not complete...</b> for now. We'll fix it in the next step
when you define an ejb module (and webapp module afterwards).</p></td></tr></table></div>

<h1><a name="5-minuteTutorialonEnterpriseApplicationDevelopmentwithEclipseandGeronimo-CreatetheEJBproject"></a>Create
the EJB project</h1>

<p>The next step is to create an EJB project to hold your EJBs.</p>

<ol>
	<li>Select <b>File &#45;&gt; New</b>, select <b>EJB Project</b>.
<br clear="all" />
<br clear="all" /></li>
	<li>In the EJB Project wizard type in <em>SampleEJB</em> as the project
name and select <b>Add project to an EAR</b> checkbox. Leave the rest as is and
click <b>Next</b>.
<br clear="all" />
<br clear="all" />  <img src="/confluence/download/attachments/93322/ganydede_ejb_prj_1.1.jpg"
align="absmiddle" border="0" />
<br clear="all" />
<br clear="all" /></li>
	<li>Unselect the <b>Create an EJB Client JAR module to hold the client interfaces
and classes</b> checkbox. We're not interested in it. Click <b>Next</b>.
<br clear="all" />
<br clear="all" />  <img src="/confluence/download/attachments/93322/ejb_prj_3.jpg"
align="absmiddle" border="0" />
<br clear="all" />
<br clear="all" /></li>
	<li>Fill in the Geronimo Deployment Plan fields with the following values:
<table class="sectionMacro" border="0" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><ul>
	<li>Group Id: <b>sampleear</b></li>
	<li>Artifact Id: <b>sample-ejb</b></li>
	<li>Artifact Type: <b>ejb</b></li>
</ul>
</tr></tbody></table>
<p><img src="/confluence/download/attachments/93322/ejb_prj_4.jpg" align="absmiddle"
border="0" />
<br clear="all" />
<br clear="all" /></p></li>
	<li>Click <b>Finish</b>.</li>
</ol>


<p>You should now have the following project structure.
<br clear="all" />
<br clear="all" />  <img src="/confluence/download/attachments/93322/ejb_struct.jpg"
align="absmiddle" border="0" />
<br clear="all" /></p>

<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Important</b><br
/><p>Remove <tt>ejbModule/META-INF/openejb-jar.xml</tt> file in the SampleEJB
project as it causes deployment issues. See the file highlighted in the image above.</p></td></tr></table></div>

<h1><a name="5-minuteTutorialonEnterpriseApplicationDevelopmentwithEclipseandGeronimo-CreatethedynamicWebproject"></a>Create
the dynamic Web project</h1>

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

<ol>
	<li>Select <b>File &#45;&gt; New</b>, select <b>Dynamic Web
Project</b>.</li>
	<li>In the Dynamic Web Project wizard type in <em>SampleWAR</em> as the
project name and select <b>Add project to an EAR</b> checkbox. Leave the rest
as is and click <b>Next</b> twice.
<br clear="all" />
<br clear="all" />  <img src="/confluence/download/attachments/93322/ganymede_war_prj_1.1.jpg"
align="absmiddle" border="0" />
<br clear="all" />
<br clear="all" /></li>
	<li>Fill in the Geronimo Deployment Plan fields with the following values:
<table class="sectionMacro" border="0" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><ul>
	<li>Group Id: <b>sampleear</b></li>
	<li>Artifact Id: <b>sample-war</b></li>
	<li>Artifact Type: <b>war</b></li>
</ul>
</tr></tbody></table>
<p><img src="/confluence/download/attachments/93322/war_prj_3.jpg" align="absmiddle"
border="0" />
<br clear="all" />
<br clear="all" /></p></li>
	<li>Click <b>Finish</b>.</li>
</ol>


<p>You should now have the following project structure.
<br clear="all" />
<br clear="all" />  <img src="/confluence/download/attachments/93322/war_struct.jpg"
align="absmiddle" border="0" />
<br clear="all" /></p>

<h1><a name="5-minuteTutorialonEnterpriseApplicationDevelopmentwithEclipseandGeronimo-CreateStatelessSessionEJB"></a>Create
Stateless Session EJB</h1>

<p>Every stateless session EJB has its own business interface. There are three types
of business interfaces - <tt><b>@Remote</b></tt>, <tt><b>@Local</b></tt>
and <tt><b>@WebService</b></tt> &#45; 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.</p>

<h2><a name="5-minuteTutorialonEnterpriseApplicationDevelopmentwithEclipseandGeronimo-Createremotebusinessinterface"></a>Create
remote business interface</h2>

<ol>
	<li>Right click on the <b>SampleEJB</b> project and select <b>New
&#45;&gt; Interface</b> and fill it in with the following values:
<table class="sectionMacro" border="0" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><ul>
	<li>Package: <b>sampleear</b></li>
	<li>Name: <b>RemoteBusinessInterface</b></li>
</ul>
</tr></tbody></table>
<p><img src="/confluence/download/attachments/93322/biz_int.jpg" align="absmiddle"
border="0" />
<br clear="all" />
<br clear="all" /></p></li>
	<li>Click <b>Finish</b>.</li>
</ol>


<p>Now we need to add a business method and mark the interface as a remote one with
<tt><b>@Remote</b></tt> annotation.</p>

<div class="code panel" style="border-style: solid;border-width: 1px;"><div class="codeHeader
panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>RemoteBusinessInterface.java</b></div><div
class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">package</span> sampleear;

<span class="code-keyword">import</span> javax.ejb.Remote;

@Remote
<span class="code-keyword">public</span> <span class="code-keyword">interface</span>
RemoteBusinessInterface {
    <span class="code-keyword">public</span> <span class="code-object">String</span>
sayHello(<span class="code-object">String</span> name);
}
</pre>
</div></div>

<h2><a name="5-minuteTutorialonEnterpriseApplicationDevelopmentwithEclipseandGeronimo-Createbeanclass"></a>Create
bean class</h2>

<ol>
	<li>Right click on the <b>SampleEJB</b> project and select <b>New
&#45;&gt; Class</b> and fill it in with the following values:
<table class="sectionMacro" border="0" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><ul>
	<li>Package: <b>sampleear</b></li>
	<li>Name: <b>MyStatelessSessionBean</b></li>
	<li>Interfaces: <b>sampleear.RemoteBusinessInterface</b></li>
</ul>
</tr></tbody></table>
<p><img src="/confluence/download/attachments/93322/bean_class.jpg" align="absmiddle"
border="0" />
<br clear="all" />
<br clear="all" /></p></li>
	<li>Click <b>Finish</b>.</li>
</ol>


<p>Implement the business method <b>sayHello</b> and mark the class as a
stateless session bean with the @Stateless annotation.</p>

<div class="code panel" style="border-style: solid;border-width: 1px;"><div class="codeHeader
panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>MyStatelessSessionBean.java</b></div><div
class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">package</span> sampleear;

<span class="code-keyword">import</span> javax.ejb.Stateless;

@Stateless
<span class="code-keyword">public</span> class MyStatelessSessionBean <span
class="code-keyword">implements</span> RemoteBusinessInterface {

    <span class="code-keyword">public</span> <span class="code-object">String</span>
sayHello(<span class="code-object">String</span> name) {
        <span class="code-keyword">return</span> getClass().getName() + <span
class="code-quote">" says hello to "</span> + name + <span class="code-quote">"."</span>;
    }
}
</pre>
</div></div>

<h1><a name="5-minuteTutorialonEnterpriseApplicationDevelopmentwithEclipseandGeronimo-Webapplicationdevelopment"></a>Web
application development</h1>

<p>The time has come to use the EJB in the Web application. In this section we create
a jsp <tt>index.jsp</tt> page that executes a servlet <b>MyServlet</b>
that in turn executes the ejb <b>MyStatelessSessionBean</b>.</p>

<h2><a name="5-minuteTutorialonEnterpriseApplicationDevelopmentwithEclipseandGeronimo-Createwelcomepage%7B%7Bindex.jsp%7D%7D"></a>Create
welcome page <tt>index.jsp</tt></h2>

<ol>
	<li>Right click on the <b>SampleWAR</b> project and select <b>New
&#45;&gt; JSP</b>. Name it <tt>index.jsp</tt> and click <b>Finish</b>.
<br clear="all" />
<br clear="all" /></li>
	<li>Change it so it executes the servlet upon form submission.
<table class="sectionMacro" border="0" cellpadding="5" cellspacing="0" width="100%"><tbody><tr>
<div class="code panel" style="border-style: solid;border-width: 1px;"><div class="codeHeader
panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>index.jsp</b></div><div
class="codeContent panelContent">
<pre class="code-html"><span class="code-tag">&lt;%@ page language=<span
class="code-quote">"java"</span> contentType=<span class="code-quote">"text/html;
charset=ISO-8859-1"</span> pageEncoding=<span class="code-quote">"ISO-8859-1"</span>%&gt;</span>
<span class="code-tag">&lt;!DOCTYPE html PUBLIC <span class="code-quote">"-//W3C//DTD
HTML 4.01 Transitional//EN"</span> <span class="code-quote">"http://www.w3.org/TR/html4/loose.dtd"</span>&gt;</span>
<span class="code-tag">&lt;html&gt;</span>
  <span class="code-tag">&lt;head&gt;</span>
    <span class="code-tag">&lt;meta http-equiv=<span class="code-quote">"Content-Type"</span>
content=<span class="code-quote">"text/html; charset=ISO-8859-1"</span>&gt;</span>
    <span class="code-tag">&lt;title&gt;</span>5-minute Tutorial on Enterprise
Application Development with Eclipse and Geronimo<span class="code-tag">&lt;/title&gt;</span>
  <span class="code-tag">&lt;/head&gt;</span>
  <span class="code-tag">&lt;body&gt;</span>
    <span class="code-tag">&lt;form action=<span class="code-quote">"${pageContext.request.contextPath}/sayHello"</span>&gt;</span>
      <span class="code-tag">&lt;input type=<span class="code-quote">"text"</span>
name=<span class="code-quote">"name"</span> /&gt;</span><span class="code-tag">&lt;input
type=<span class="code-quote">"submit"</span> value=<span class="code-quote">"Press
me!"</span> /&gt;</span>
    <span class="code-tag">&lt;/form&gt;</span>
  <span class="code-tag">&lt;/body&gt;</span>
<span class="code-tag">&lt;/html&gt;</span>
</pre>
</div></div></tr></tbody></table></li>
</ol>


<h2><a name="5-minuteTutorialonEnterpriseApplicationDevelopmentwithEclipseandGeronimo-CreateservletMyServlet"></a>Create
servlet - MyServlet</h2>

<p>Since the servlet calls the EJB, the Web project the servlet is in depends on the
EJB project. Let's define the dependency.</p>

<ol>
	<li>Right click on the <b>SampleWAR</b> project and select <b>Properties</b>.
Go to <b>J2EE Module Dependencies</b> and select the checkbox next to <b>SampleEJB.jar</b>
(it's in the <b>J2EE Modules</b> tab) and click <b>OK</b>.
<br clear="all" />
<br clear="all" />  <img src="/confluence/download/attachments/93322/war_dep.1.jpg"
align="absmiddle" border="0" />
<br clear="all" />
<br clear="all" /></li>
	<li>Right click on the <b>SampleWAR</b> project and select <b>New
&#45;&gt; Servlet</b> and fill it in with the following values:
<table class="sectionMacro" border="0" cellpadding="5" cellspacing="0" width="100%"><tbody><tr><ul>
	<li>Java Package: <b>sampleear</b></li>
	<li>Class name: <b>MyServlet</b></li>
</ul>
</tr></tbody></table>
<p><img src="/confluence/download/attachments/93322/myServlet.jpg" align="absmiddle"
border="0" />
<br clear="all" />
<br clear="all" /></p></li>
	<li>Click <b>Next</b>.</li>
	<li>Change the URL Mapping section so the servlet serves at <b>/sayHello</b>
url mapping and click <b>Finish</b>.
<br clear="all" />
<br clear="all" />  <img src="/confluence/download/attachments/93322/url_mapping.jpg"
align="absmiddle" border="0" />
<br clear="all" /></li>
</ol>


<p><tt>MyServlet.java</tt> opens up automatically for editing after creation,
update the servlet as shown below to call off the ejb when executed.</p>

<div class="code panel" style="border-style: solid;border-width: 1px;"><div class="codeHeader
panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>MyServlet.java</b></div><div
class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">package</span> sampleear;

<span class="code-keyword">import</span> java.io.IOException;

<span class="code-keyword">import</span> javax.ejb.EJB;
<span class="code-keyword">import</span> javax.servlet.ServletException;
<span class="code-keyword">import</span> javax.servlet.http.HttpServletRequest;
<span class="code-keyword">import</span> javax.servlet.http.HttpServletResponse;

<span class="code-keyword">public</span> class MyServlet <span class="code-keyword">extends</span>
javax.servlet.http.HttpServlet <span class="code-keyword">implements</span> javax.servlet.Servlet
{
    <span class="code-keyword">static</span> <span class="code-keyword">final</span>
<span class="code-object">long</span> serialVersionUID = 1L;

    @EJB
    RemoteBusinessInterface remoteBusinessIntf;

    <span class="code-keyword">protected</span> void doGet(HttpServletRequest
request, HttpServletResponse response) <span class="code-keyword">throws</span>
ServletException, IOException {
        <span class="code-object">String</span> name = request.getParameter(<span
class="code-quote">"name"</span>);
        <span class="code-keyword">if</span> (name == <span class="code-keyword">null</span>
|| name.length() == 0) {
            name = <span class="code-quote">"anonymous"</span>;
        }
        response.getWriter().write(remoteBusinessIntf.sayHello(name));
    }
}
</pre>
</div></div>

<h1><a name="5-minuteTutorialonEnterpriseApplicationDevelopmentwithEclipseandGeronimo-Deployandrun"></a>Deploy
and run</h1>

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

<ol>
	<li>Right click on the <b>SampleEAR</b> project and select <b>Run
As &#45;&gt; Run on Server</b>. When <em>Run On &#95;Server</em>
popup window comes up, select the <b>Always use this server when running this project</b>
checkbox. Leave the rest as is.
<br clear="all" />
<br clear="all" />  <img src="/confluence/download/attachments/93322/runOnServer.jpg"
align="absmiddle" border="0" />
<br clear="all" />
<br clear="all" /></li>
	<li>Click <b>Finish</b>.</li>
	<li>The server's stopped so nothing happens (from a user's perspective at least). Open
up the <em>Servers</em> tab and right click on <b>Apache Geronimo v2.1 Server
at localhost</b> and select <b>Start</b>.
<br clear="all" />
<br clear="all" /></li>
	<li>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 <a href="http://localhost:8080/SampleWAR"
rel="nofollow">http://localhost:8080/SampleWAR</a>.
<br clear="all" />
<br clear="all" />  <img src="/confluence/download/attachments/93322/welcome.jpg"
align="absmiddle" border="0" />
<br clear="all" />
<br clear="all" /></li>
	<li>Type in any name you want, e.g. <tt>John Doe</tt> and press <b>Press
me&#33;</b> button.
<br clear="all" />
<br clear="all" />  <img src="/confluence/download/attachments/93322/result.jpg"
align="absmiddle" border="0" />
<br clear="all" /></li>
</ol>

     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/GMOxDOC22/5-minute+Tutorial+on+Enterprise+Application+Development+with+Eclipse+and+Geronimo">View
Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=93322&revisedVersion=34&originalVersion=33">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/GMOxDOC22/5-minute+Tutorial+on+Enterprise+Application+Development+with+Eclipse+and+Geronimo?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message