Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
 
Training Index

Step 4: Writing the Enterprise JavaBeanTM Class

This step shows you how to code of the application (business logic) is done. Up until now, you have declared interfaces that the container tools will generate code for, but here is where the functionality of the JavaBean is coded.

One important thing to notice about the bean implementation, is just how little code you have to author. Most of this code is simply implementing methods from the Enterprise JavaBeans specification. It is easy to envisage templates and tools that do most of the implementation work for you. This is very comparable to the way tools have evolved for the GUI beans environment.

  1. Read the description of the DemoBean code below and note the comments in the example code, particularly under the heading, "Business Logic."
  2. Save the example source code to the indicated file, and move to the next step.

Here is the source code for the DemoBean Enterprise JavaBeans DemoBean.java:

   /**
    * DemoBean -- This is implemented by the EnterPrise
    * Bean author This class must extend
    * javax.ejb.SessionBean and implement
    * the methods in this interface as well as providing
    * the implementation of the business methods.
    *
    */
package ejb.demo;
  
import javax.ejb.*;
import java.io.Serializable;
import java.util.*;
import java.rmi.*;
  
public class DemoBean implements SessionBean {
  static final boolean verbose = true;
  
  private transient SessionContext ctx;
  private transient Properties props;
  
  // Implement the methods in the SessionBean
  // interface
  public void ejbActivate() {
    if (verbose)
      System.out.println("ejbActivate called");
  }
  
  public void ejbRemove() {
    if (verbose)
      System.out.println("ejbRemove called");
  }
  
  public void ejbPassivate() {
    if (verbose)
      System.out.println("ejbPassivate called");
  }
  
    /**
     * Sets the session context.
     *
     * @param SessionContext
     */
  public void setSessionContext(SessionContext ctx) {    
    if (verbose)
      System.out.println("setSessionContext called");
    this.ctx = ctx;
    props = ctx.getEnvironment();
  }
    
     /**
     * This method corresponds to the create method in
     * the home interface DemoHome.java. 
     * The parameter sets of the two methods are 
     * identical. When the client calls 
     * DemoHome.create(), the container allocates an 
     * instance of the EJBean and calls ejbCreate(). 
     */ 
  public void ejbCreate () {
    if (verbose)
      System.out.println("ejbCreate called");
  }
  
    /**
     * **** HERE IS THE BUSINESS LOGIC *****
     * Do the demoSelect() but don't even go to
     * the database in this eg but instead just
     * return a String.
     * The really BIG thing to notice here is that
     * this is the only code we have invented at all
     * the rest of the code has been declarations
     * or simply implementing methods which are
     * part of the EJB interfaces and in this example
     * are not even used.
     */
  public String demoSelect()
    throws RemoteException
  {
    return("hello world");
  }
  
}

<< BACK NEXT >>


[ This page was updated: 5-Nov-99 ]

Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-99 Sun Microsystems, Inc.
All Rights Reserved. Legal Terms. Privacy Policy.
1