ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

What Is Geronimo?
Pages: 1, 2, 3

Sample Application

For an introductory article like this, we'll create a simple web application and deploy it on Geronimo. Please make sure that the following environment variables are set correctly.

Environment variable Purpose
JAVA_HOME Indicates the base directory of your standard Java SE installation.
GERONIMO_HOME Set to the root directory of your Geronimo installation.
ANT_HOME Set to the root directory of your Ant installation.
GERONIMO_DEV Indicates the base directory of application development space.
PATH Ensure that ANT_HOME/bin is in your PATH.

Within GERONIMO_DEV, create the following directories:



  • etc: All deployment plans are kept inside this directory.
  • src: This directory is used for placing any servlets and other Java source files.
  • web: All public JSP documents are placed inside this directory.

Deployment Plans

We'll need two deployment plans, namely web.xml and geronimo-web.xml. Before we dive into these, I'll clarify their differences. The following table will help differentiate between the two deployment plans and their roles.

Deployment Plan Type Location Purpose
web.xml Standard Java EE web deployment descriptor This must be stored as part of the WAR file under the WEB-INF subdirectory. Used for specifying declarative security, configuring error and welcome pages, servlet declaration and mapping, filter definition and mapping, configuring initialization parameters, registering listener classes, etc.
geronimo-web.xml Geronimo-specific deployment plan Under the private WEB-INF subdirectory. The geronimo-web.xml file defines Geronimo-specific parts such as resource references and security mapping configuration.

Code Your First Geronimo App

We'll begin by creating a simple JSP page to greet the user and display the current date and time. Create hello.jsp as shown below and place it in the GERONIMO_DEV/web directory:

<html>
<head>
<jsp:useBean id="currentdate"
    class="java.util.Date" />
<title>Geronimo JSP App</title>
</head>
<body>
<h3>Hello world from Geronimo!! on
${currentdate}</h3>
</body>
</html>

Next, create the following two deployment plans and place them inside the GERONIMO_DEV/etc directory. The first deployment plan, web.xml, is the standard Java EE deployment descriptor as shown here:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

    <display-name> Hello JSP Web App
    </display-name>
    <description>First Geronimo App
    </description>
</web-app>

The following code snippet shows the Geronimo-specific deployment plan, geronimo-web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.0"
xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.0"
configId="com/onjava/safari/myApp">


  <context-root>/myApp</context-root>
  <context-priority-classloader>false\
  </context-priority-classloader>
</web-app>

The configId attribute specifies the name of our application, com/onjava/safari/myApp. Next, we set up the context (/myApp), which is the directory where our application is placed once deployed to Geronimo.

Build the App

Next, we'll do an incremental build with Ant. We need an Ant build file (build.xml), placed in GERONIMO_DEV. The build.xml is a well-formatted XML file that describes the build process of our application as shown below:

<project name="GeronimoApp" default="war"
basedir=".">

  <property environment="env"/>

 <path id="cp">
      <pathelement path="${java.class.path}"/>
      <fileset dir="${env.GERONIMO_HOME}/
      repository/org.apache.geronimo.specs/jars">
        <include name="*.jar"/>
      </fileset>
    </path>

 <target name="prepare">
    <mkdir dir="dist"/>
    <mkdir dir="classes"/>
    <mkdir dir="build"/>

    <delete>
     <fileset dir="classes"
       includes="**/*.class"/>
    </delete>

    <mkdir dir="build/WEB-INF"/>
    <mkdir dir="build/WEB-INF/lib"/>

    <copy file="etc/web.xml" todir="build/WEB-INF"/>
    <copy file="etc/geronimo-web.xml"
    todir="build/WEB-INF" />

    <copy todir="build">
      <fileset dir="web"/>
    </copy>

 </target>

<target name="compile" depends="prepare">
    <javac srcdir="src" destdir="classes"
    debug="on" deprecation="off">
      <classpath refid="cp"/>
    </javac>
</target>

<target name="war" depends="compile">
   <war destfile="dist/safari.war"
   webxml="build/WEB-INF/web.xml">
    <classes dir="classes" />
      <fileset dir="build">
        <exclude name="WEB-INF/web.xml"/>
      </fileset>
    </war>
 </target>

</project>

Following is the output we'll get after running the Ant script:

D:\>cd %GERONIMO_DEV%

D:\gdev>ant
Buildfile: build.xml

prepare:
  [mkdir] Created dir: D:\gdev\dist
  [mkdir] Created dir: D:\gdev\classes
  [mkdir] Created dir: D:\gdev\build
  [mkdir] Created dir: D:\gdev\build\WEB-INF
  [mkdir] Created dir: D:\gdev\build\WEB-INF\lib
   [copy] Copying 1 file to D:\gdev\build\WEB-INF
   [copy] Copying 1 file to D:\gdev\build\WEB-INF
   [copy] Copying 1 file to D:\gdev\build

compile:

war:
      [war] Building war: D:\gdev\dist\safari.war

BUILD SUCCESSFUL
Total time: 3 seconds

This creates a .war file (placed inside the /dist directory), which will be deployed inside the Tomcat container shipped with Geronimo app server.

Pages: 1, 2, 3

Next Pagearrow