Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
java
java
package mytimepak;
public class MyTimeBean implements javax.ejb.SessionBean {
	
	public void ejbCreate() {}
	public void ejbActivate() {}
   	public void ejbPassivate() {} 
   	public void setSessionContext(javax.ejb.SessionContext ctx) {}
   	public void unsetSessionContext() {}
   	public void ejbRemove() {}

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

I have put my EJB in a package that I call mytimepak. The first 6 methods are implementations of the javax.ejb.SessionBean interface. I don't realy know what good they are, but the ejbCreate method is like a constructor, in this case an empty constructor. To enable clients to call the EJB one must provide so called home and remote interfaces. The first one, home interface, is used to locate the EJB, the other, the remote interface is used to invoke methods on the EJB. The remote interface is just like a normal interface used for RMI. As this EJB will only be used from a JSP-page that is run in the same server (same JVM) I use another type of interfaces that don't make use of the network. Thay are called LocalHome and Local interface instead, but they are just the same.
This is the code for the local home interface (coresponding to the home interface):

...

Code Block
xml
xml
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar id="ejb-jar_1" 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/ejb-jar_2_1.xsd" version="2.1">
   <description>Example of a session bean</description>
   <display-name>MyTimeBeanEJBName</display-name>
   <enterprise-beans>
      <session id="Session_MyTime">
         <description>An EJB named MyTimeBean</description>
         <display-name>MyTimeBeanName</display-name>
         <ejb-name>MyTimeBean</ejb-name>
         <local-home>mytimepak.MyTimeLocalHome</local-home>
         <local>mytimepak.MyTimeLocal</local>
         <ejb-class>mytimepak.MyTimeBean</ejb-class>
         <session-type>Stateless</session-type>
         <transaction-type>Container</transaction-type>
      </session>
   </enterprise-beans>
</ejb-jar>

...

Code Block
xml
xml
<?xml version="1.0" encoding="UTF-8"?>
<openejb-jar xmlns="http://www.openejb.org/xml/ns/openejb-jar-2.1" xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.1" 
xmlns:pkgen="http://www.openejb.org/xml/ns/pkgen-2.0" 
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>default</sys:groupId>
      <sys:artifactId>TimeBean_artifact_in_openejb</sys:artifactId>
      <sys:version>1.0</sys:version>
      <sys:type>car</sys:type>
    </sys:moduleId>
  </sys:environment>
  <enterprise-beans>
	<session>
	<ejb-name>MyTimeBean</ejb-name>
	<ejb-ref>
		<ref-name>ejb/MyTimeBean</ref-name>
		<ejb-link>MyTimeBean</ejb-link>
	</ejb-ref>
	</session>
  </enterprise-beans>
</openejb-jar>

...

Code Block
java
java
<%@ page contentType="text/html" import="mytimepak.*, javax.naming.* " %>
<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 rference to the EJB                        
		MyTimeLocalHome myTimeHomeLocal = (MyTimeLocalHome)context.lookup("java:comp/env/ejb/MyTimeBean");
		// java:comp:env is like a reference to the local server. ejb is where you find the EJBs and
		// MyTimeBean is the name of the EJB that is written in the deployment descriptors.
		
		// This is like a constructor returning a MyTimeLocal, an interface for the EJB on which you
		// can call methods (but not access variables as with any interface) 
		MyTimeLocal myTimeLocal = myTimeHomeLocal.create();
		// So, just go ahead and call a method (in this case the only method).
		s =  myTimeLocal.getTime();
	}
	catch (Exception e) {
		s=e.toString();
	}
%>

...

Code Block
xml
xml
<?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>default</sys:groupId>
      <sys:artifactId>MyTimeWeb</sys:artifactId>
      <sys:version>1.0</sys:version>
      <sys:type>car</sys:type>
    </sys:moduleId>
  </sys:environment>
  <context-root>/mytime</context-root>
</web-app>

...

Code Block
xml
xml
<?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-home>mytimepak.MyTimeLocalHome</local-home>
		<local>mytimepak.MyTimeLocal</local>
		<ejb-link>MyTimeBean</ejb-link>
	</ejb-local-ref>
</web-app>

...

Code Block
xml
xml
<?xml version="1.0" encoding="UTF-8"?>
<application id="Application_ID" version="1.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/application_1_4.xsd">
	<display-name>MyTimeEJBApp</display-name>
	<module>
		<ejb>mytime-ejb.jar</ejb>
	</module>
	<module id="WebModule_1158667626422">
		<web>
			<web-uri>mytime-web.war</web-uri>
			<context-root>/mytime</context-root>
		</web>
	</module>
</application>

...