Sun Java System Application Server 

Samples Index

The Java EE 5 Annotation Sample Application

This simple application demonstrates the new Annotation facility introduced in Java EE 5. The application is a lottery simulation in which the user selects the Lottery to play and the application generates a "quick pick" number.

This sample consist of:

The main HTML page, index.html is an entry point to the application. It prompts for user input and then invokes the PlayLotteryServlet servlet. The servlet PlayLotteryServlet generates the required data (lottery number, lottery date), sets date values in the request object, and dispatches the JSP, LotteryView.jsp for output. PlayLotteryServlet obtains the lottery date using the stateless session bean, Date. It uses the stateful session bean, Lottery, to form the lottery number from the individual digits obtained from the stateless session bean, Dice.

Dice Stateless Session Bean

The Dice bean is a very simple EJB 3.0 stateless session bean with a remote business interface.

Business Interface

This EJB has a remote business interface with a single business method:

import javax.ejb.Remote;

  @Remote
  public interface Dice {
    public int play();
}

Unlike prior versions of EJB, the remote interface is not required to extend java.rmi.Remote and its business methods are not required to throw java.rmi.RemoteException. The business interface is designated as a remote business interface via the @javax.ejb.Remote annotation.

Dice Bean Class

The implementation of the Dice bean is:

import java.util.Random;
import javax.ejb.Stateless;

@Stateless
public class DiceBean implements Dice {
    public int play() {
        Random random = new Random();
        return random.nextInt(10);
    }
}

In EJB 3.0, a stateless session bean is just a plain Java object with a class-level annotation @Stateless. It does not have a home interface, as in previous versions of EJB. No deployment descriptor entries are needed to define the Bean. The JNDI name for the bean will default to the class name of its business interface.

Date stateless session bean

This is a very simple EJB 3.0 stateless session bean with a local business interface.

Business Interface

The stateless session bean has a local business interface with a single business method.

public interface Date {
    public String today();
}

Implementation

The bean implementation is:

import java.util.Calendar;
import javax.ejb.Stateless;

@Stateless
public class DateBean implements Date {
    public String today() {
        StringBuffer date = new StringBuffer();
        Calendar calendar = Calendar.getInstance();

        date.append(calendar.get(Calendar.MONTH)+1);
        date.append("/");
        date.append(calendar.get(Calendar.DAY_OF_MONTH));
        date.append("/");
        date.append(calendar.get(Calendar.YEAR));

        return date.toString();
    }
}

Lottery Stateful Session Bean

This is a very simple EJB 3.0 stateful session bean with a remote business interface.

Business Interface

The stateful session bean has a remote business interface with multiple business methods.

import javax.ejb.Remote;

@Remote
public interface Lottery {
    public String getName();
    public String getNumber();
    public String getDate();

    public void select(int number);
    public void setName(String name);
}

Unlike prior versions of EJB, the remote interface is not required to extend java.rmi.Remote and its business methods are not required to throw
java.rmi.RemoteException. The business interface is designated as a remote business interface via the @javax.ejb.Remote annotation.

Stateful Session Bean Class

The bean implementation is:

import javax.ejb.Stateful;
import javax.ejb.EJB;

@Stateful
public class LotteryBean implements Lottery {
    public String getName() {
        return name;
    }

    public String getNumber() {
        return number;
    }

    public String getDate() {
        return date.today();
    }

    public void select(int number) {
        if( (number > -1) && (number < 10) ) {
            this.number = this.number + SPACE +
                java.lang.Integer.toString(number);
        }
    }

    public void setName(String name) {
        this.name = name;
    }

    //Dependency injection to get an instance of the Date EJB.
    @EJB(name="Date")
    private Date date;

    private String name = "Super Lotto";
    private String number = "";
    private static final String SPACE = " ";
}

In EJB 3.0, a stateful session bean is just a plain Java object with a class-level annotation @Stateful. It does not have a home interface. No deployment dscriptor entries are needed to define the Bean. The JNDI name for the bean will default to the class name of its business interface. @EJB is used to get the local bean Date, eliminating the error-prone string-based JNDI-lookup.

PlayLotteryServlet Servlet

This is a very simple Servlet that access EJB using JAVA EE 5 servlet 2.5 feature. Only the relevant code snippet is depicted below.

public class PlayLotteryServlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    ...

      ResourceBundle rb = ResourceBundle.getBundle("LocalStrings", Locale.getDefault());            
      try {
        InitialContext initContext  = new InitialContext();
          lottery = (Lottery) initContext.lookup("enterprise.lottery_annotation_ejb_stateful.Lottery");
          dice = (Dice) initContext.lookup("enterprise.lottery_annotation_ejb_stateless.Dice");
      }
    ...
  }
}    

The bean's business interface class name is used for its lookup. There is no need to assign explicit JNDI name for the bean.

Building, Deploying, and Running the Application

Following are the instructions for building, deploying, and running this sample application.

  1. Set up your build environment and Configure the application server with which the build system has to work by following the common build instructions.
  2. app_dir is the sample application base directory: samples_install_dir/javaee5/enterprise/lottery-annotation-ear
  3. Change directory to app_dir.
  4. Build, Deploy and Run the sample application using the target all
  5. app_dir> ant all

  6. It is possible to do the above separately by issuing separate commands as given below.
  7. app_dir> ant default compiles and packages the application

    app_dir> ant deploy deploys it to application server

    app_dir> ant run runs the application. It launches the browser with the correct url.

  8. You can run the java client using the target run-client.
app_dir> ant run-client

run-client target invokes the java client code under app_dir/lottery-annotation-appclient directory.

  1. Use the target clean to remove the temporary directories like build and dist.
app_dir> ant clean

Building, Deploying, and Running the Application in NetBeans IDE

Follow these instructions to build, deploy, and run this sample application using NetBeans IDE.

  1. Refer to common build instructions. for setting up NetBeans IDE and the application server with which the IDE will use.
  2. In NetBeans IDE, select File->OpenProject and select samples_install_dir/javaee5/enterprise/lottery-annotation-ear as the project.
  3. Right click on lottery-annotation-ear and select Run Project which will build, deploy and run the project. If configured properly, the IDE will open the browser with appropriate url. Sample output is given below.
  4. NetBeans IDE output
      Browsing: http://localhost:8080/lottery-annotation-ear/
      run-display-browser:
      pre-init:
      init-private:
      init-userdir:
      init-user:
      init-project:
      do-init:
      post-init:
      init-check:
      init:
      run-ac:
      BUILD SUCCESSFUL (total time: 11 seconds)
      
    Browser output
      http://localhost:8080/lottery-annotation-ear/
    
      Simple Annotation Application
        Super Lotto
        Quick Pick
      When you click Quick Pick, your lottery number will be generated.
      

Troubleshooting

If you have problems when running the application, refer to troubleshooting document.

 


Copyright © 2006 Sun Microsystems, Inc. All rights reserved.