Child pages
  • Very simple Session EJB example
Skip to end of metadata
Go to start of metadata

The Time Bean Example

This is an example of a JSP-page calling a Session Bean. The result looks like this:
I have tried to strip of everything just to make this example as easy as possible to understand. This is an example using Geronimo 2.0, Java 1.5 and EJB 3.0.

Application Contents is an EJB that can tell time. I have put my EJB in a package that I call 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. 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.

openejb-jar.xml does nothing but specifies the module's information.

xmlsolidopenejb-jar.xml ${pom.groupId} ${pom.artifactId} ${version} jar ]]>

index.jsp utilizes the MyTimeBean to tell time.

javasolidindex.jsp Time <% 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%> ]]>

geronimo-web.xml specifies the module's information and the url for the web-app.

xmlsolidgeronimo-web.xml ${pom.groupId} ${pom.artifactId} ${version} war /mytime ]]>

web.xml references the EJB present in the WEB-INF/classes/org/apache/geronimo/samples/mytimepak directory.

xmlsolidweb.xml MyTimeWeb index.jsp ejb/MyTimeBean Session org.apache.geronimo.samples.mytimepak.MyTimeLocal ]]>

Tools Used

Apache Maven 2

Maven is a popular open source build tool for enterprise Java projects, designed to take much of the hard work out of the build process. Maven uses a declarative approach, where the project structure and contents are described, rather than the task-based approach used in Ant or in traditional make files, for example. This helps enforce company-wide development standards and reduces the time needed to write and maintain build scripts. The declarative, lifecycle-based approach used by Maven 1 is, for many, a radical departure from more traditional build techniques, and Maven 2 goes even further in this regard. Maven 2 can be download from the following URL:

Building, and Deploying the application

Download the mytime application from the following link:
MyTime (Note: This may not be the latest. Checkout the source code below for the latest.)

After decompressing the given file, the mytime directory will be created.

Source Code

You can checkout the source code of this sample from SVN:

svn checkout

(Hint: If you are on Windows you will need to obtain svn from )


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.0-SNAPSHOT.ear under the mytime folder. Now, you are ready to deploy mytime application in the Geronimo Application server.

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.0-SNAPSHOT.ear from mytime folder in to the Archive input box.
  3. 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.

  • No labels