Your First Cup: An Introduction to the Java EE Platform

Creating the Enterprise Bean

DukesBirthdayBean is a stateless session bean. Stateless session beans are enterprise beans that do not maintain a conversational state with a client. With stateless session beans the client makes isolated requests that do not depend on any previous state or requests. If you require conversational state, you use stateful session beans.

To create DukesBirthdayBean you need to create one Java source file: DukesBirthdayBean, the enterprise bean class. DukesBirthdayBean is a local enterprise bean that uses a no-interface view, meaning two things. First, a local enterprise bean is only visible within the application in which it is deployed. Second, enterprise beans with a no-interface view do not need a separate business interface that the enterprise bean class implements. The enterprise bean class is the only file you need to create a local, no-interface enterprise bean.

DukesBirthdayBean will be packaged within the same WAR file as the Facelets front-end.

Creating DukesBirthdayBean in NetBeans 6.7 IDE

This section has instructions for creating the web application project and the DukesBirthdayBean enterprise bean.

ProcedureCreate the Web Application Project

Follow these steps to create a new web application project in NetBeans 6.7 IDE.

  1. Select File->New Project.

  2. Under Categories select Java Web, then under Projects select Web Application, and click Next.

  3. Set the Project Name to firstcup.

  4. Set the Project Location to tut-install/myexample, where tut-install is the location of the firstcup tutorial installation.

  5. Click Next.

  6. Select your Enterprise Server v3 instance from the Server menu.

  7. Select Java EE 5 from the Java EE Version menu.

    Note –

    NetBeans 6.7 IDE doesn't currently support Java EE 6 as a Java EE Version, but our application uses Java EE 6 features, and will run on Enterprise Server v3, a Java EE 6 server.

  8. Set Context Path to /firstcup and click Next.

  9. Under Frameworks select Java Server Faces.

  10. Under Servlet URL Pattern enter /firstcupWeb/* and click Finish.

  11. Under Web Pages right-click WelcomeJSF.jsp, select Delete, then Yes.

ProcedureCreating the DukesBirthdayBean Enterprise Bean Class

Follow these steps to create the enterprise bean class in NetBeans 6.7 IDE.

  1. Select firstcup project in the Projects tab.

  2. Select File->New File.

  3. Select Java in the Categories pane.

  4. Select Java Class in the File Types pane.

  5. Click Next.

  6. Set Class Name to DukesBirthdayBean.

  7. Set the Package name to firstcup.ejb.

  8. Click Finish.


Add the enterprise bean annotations and a method that calculates the difference in age in years between Duke and the user.

  1. Annotate the class with the javax.ejb.Stateless annotation.

    public class DukesAgeBean {

    The @Stateless annotation marks the class as a stateless session bean.

  2. Directly after the class declaration, paste in the following code:

    private static Logger logger = 
    public int getAgeDifference(Date date) {
    	int ageDifference;
    	Calendar theirBirthday = new GregorianCalendar();
    	Calendar dukesBirthday = new GregorianCalendar(1995, Calendar.MAY, 23);
    	// Set the Calendar object to the passed in Date
    	// Subtract the user's age from Duke's age
    	ageDifference = dukesBirthday.get(Calendar.YEAR) - 
    	theirBirthday.get(Calendar.YEAR);"Raw ageDifference is: " + ageDifference);
    	// Check to see if Duke's birthday occurs before the user's. If so, 
    	// subtract one from the age difference
    	if (dukesBirthday.before(theirBirthday) && (ageDifference > 0)) {
    	}"Final ageDifference is: " + ageDifference);
    	return ageDifference;

    This code creates a logger for the session bean, creates the Calendar objects used to calculate the difference in age between the user and Duke, and adds a method, getAgeDifference, that does the actual calculation of the difference in age.

    Similar to the DukesAgeResource.getText code, getAgeDifference subtracts Duke's birthday year from the user's birthday year to get a raw age difference. If Duke's birthday falls before the users, and the raw difference is more than 0, subtract one year from the age difference. The final age difference is returned as an int.

  3. Right-click in the editor window and select Format.

  4. Right-click in the editor window and select Fix Imports.

  5. Choose the java.util.logging.Logger fully-qualified name for the Logger class.

  6. Choose the java.util.Date fully-qualified name for the Date class.

  7. Click OK.

  8. Select File->Save.