Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

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.

Code Block
java
java
borderStylesolid
titleMyTimeBean.javajava
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.

Code Block
java
java
borderStylesolid
titleMyTimeLocal.javajava
package org.apache.geronimo.samples.mytimepak;
public interface MyTimeLocal {
   public java.lang.String getTime() ;
}

index.jsp utilizes the MyTimeBean to tell time.

Code Block
java
java
borderStylesolid
titleindex.jspjava
<%@ 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>

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

Code Block
xml
xml
titlegeronimo-web.xmlxml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.1"
         xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.1"
         xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.1"
         xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.1">
  <sys:environment>
    <sys:moduleId>
      <sys:groupId>${pom.groupId}</sys:groupId>
      <sys:artifactId>${pom.artifactId}</sys:artifactId>
      <sys:version>${version}</sys:version>
      <sys:type>war</sys:type>
    </sys:moduleId>
  </sys:environment>
  <context-root>/mytime</context-root>
</web-app>

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

Code Block
xml
xml
titleweb.xmlxml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    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">
	<display-name>MyTimeWeb</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<!-- To refer local EJB's  -->
	<ejb-local-ref>
		<ejb-ref-name>ejb/MyTimeBean</ejb-ref-name>
		<ejb-ref-type>Session</ejb-ref-type>
		<local>org.apache.geronimo.samples.mytimepak.MyTimeLocal</local>
	</ejb-local-ref>
</web-app>

...

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.

Code Block
xml
xml
borderStylesolid
titleplan.xmlxml
<?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>

...