HomeDocumentation > User's guide > Sample Applications > mytime - Very Simple Session EJB Example

The Time Bean Example

This is a very simple example of a JSP-page calling a Session Bean. The result looks like this:
The primary purpose of the example is to provide a very simple demonstration of an EJB session bean. It is not intended to be a complete example or one that could be used as a template for creating your own EJB session bean. This is an example using Geronimo 2.1, Java 1.5 and EJB 3.0.

Application Contents

MyTimeBean.java is an EJB that can tell time. The EJB is in the package org.apache.geronimo.samples.mytimepak. By using the @Stateless annotation Geronimo will recognize that this is a stateless session bean. There is no need for a ejb-jar.xml.

MyTimeBean.java
package org.apache.geronimo.samples.mytimepak;

import javax.ejb.Stateless;

@Stateless
public class MyTimeBean implements MyTimeLocal {

	public String getTime() {
		String s = new java.util.Date().toString();
		return s;
	}
}

MyTimeLocal.java is the Local interface. As this EJB will only be used from a JSP-page that is running in the same server (same JVM) I use a Local interface that do not make use of the network.

MyTimeLocal.java
package org.apache.geronimo.samples.mytimepak;
public interface MyTimeLocal {
   public java.lang.String getTime() ;
}

index.jsp utilizes the MyTimeBean to tell time.

index.jsp
<%@ page contentType="text/html" import="javax.naming.Context, javax.naming.InitialContext " %>
<%@ page import="org.apache.geronimo.samples.mytimepak.MyTimeLocal" %>
<html
<head><title>Time</title></head>
<body>
<%
    String s = "-"; // Just declare a string
    try {
        // This creates a context, it can be used to lookup EJBs. Using normal RMI you would
        // have to know port number and stuff. The InitialContext holds info like
        // server names, ports and stuff I guess.
        Context context = new InitialContext();
        // MyTimeLocalHome is a reference to the EJB
        MyTimeLocal myTimeLocal = (MyTimeLocal) context.lookup("java:comp/env/ejb/MyTimeBean");
        // So, just go ahead and call a method (in this case the only method).
        s = myTimeLocal.getTime();
    }
    catch (Exception e) {
        s = e.toString();
    }
%>
This is the time returned from the EJB: <%=s%>
</body>
</html>

Deployment Plan for the Application

plan.xml is generated by building the sample and can be found under ./mytime-jetty/target/resources/META-INF/plan.xml to deploy on jetty or ./mytime-tomcat/target/resources/META-INF/plan.xml to deploy on tomcat following a successful build of the sample. Shown below is the deployment plan for tomcat.

plan.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at
    
     http://www.apache.org/licenses/LICENSE-2.0
    
    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.-->
<!--$Rev: 497879 $ $Date: 2007-01-19 12:11:01 -0500 (Fri, 19 Jan 2007) $-->
<application xmlns="http://geronimo.apache.org/xml/ns/j2ee/application-1.2">
  <dep:environment xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2">
    <dep:moduleId>
      <dep:groupId>org.apache.geronimo.samples</dep:groupId>
      <dep:artifactId>mytime-tomcat</dep:artifactId>
      <dep:version>2.1-SNAPSHOT</dep:version>
      <dep:type>car</dep:type>
    </dep:moduleId>
    <dep:dependencies>
      <dep:dependency>
        <dep:groupId>org.apache.geronimo.configs</dep:groupId>
        <dep:artifactId>jasper</dep:artifactId>
        <dep:version>2.1</dep:version>
        <dep:type>car</dep:type>
      </dep:dependency>
      <dep:dependency>
        <dep:groupId>org.apache.geronimo.configs</dep:groupId>
        <dep:artifactId>tomcat6</dep:artifactId>
        <dep:version>2.1</dep:version>
        <dep:type>car</dep:type>
      </dep:dependency>
      <dep:dependency>
        <dep:groupId>org.apache.geronimo.configs</dep:groupId>
        <dep:artifactId>openejb</dep:artifactId>
        <dep:version>2.1</dep:version>
        <dep:type>car</dep:type>
      </dep:dependency>
    </dep:dependencies>
    <dep:hidden-classes/>
    <dep:non-overridable-classes/>
  </dep:environment>
</application>

Building, Configuring, and Deploying the application

Source Code for Sample

Please reference Samples General Information for information on obtaining and building the source for this and other samples.

Building

Use a command prompt to navigate into the mytime directory and just give mvn clean install site command to build. It will create the mytime-ear-2.1-SNAPSHOT.ear under the mytime folder. Now, you are ready to deploy mytime application in the Geronimo Application server using either the plugins generated for tomcat and jetty or the deployment plan that was generated and the ear.

Deploying the Application

Deploying sample application is pretty straight forward as we are going to use the Geronimo Console.

  1. Scroll down to Deploy New from the Console Navigation panel.
  2. Load mytime-ear-2.1-SNAPSHOT.ear from mytime folder in to the Archive input box.
  3. Load plan.xml from ./mytime-jetty/target/resources/META-INF/ or ./mytime-tomcat/target/resources/META-INF/ depending on the target server (jetty or tomcat).
  4. Press Install button to deploy application in the server.

MyTime Web Application

To test the sample web application open a browser and type http://localhost:8080/mytime.