Document Information


1.  Introduction

2.  Understanding Java Platform, Enterprise Edition

3.  Creating Your First Java EE Application

4.  Creating Your Second Web Application

Creating the firstcup Project

Create the Web Application Project

Creating the Java Persistence API Entity

Create the FirstcupUser Entity Class

Add Properties to the FirstcupUser Entity

Add Constructors to the FirstcupUser Entity

Add a Named Query to the FirstcupUser Entity

Creating the Web Client

Creating a Resource Bundle

Create a Resource Bundle

Configuring the Resource Bundle in the Configuration File

Create a Configuration File

Configure the Resource Bundle

Creating the DukesBDay Managed Bean Class

Create the Managed Bean Class

Add an Enterprise Bean Reference

Add Properties to the Bean

Get the Age Difference from the DukesBirthdayBean Enterprise Bean

Creating the Facelets Client

Resource Libraries in firstcup

The inputDate Composite Component

Create the inputDate Composite Component

The Facelets Web Interface

Create the XHTML Files

Set the Welcome File in the web.xml Deployment Descriptor

Modify the XHTML Files

Add the Form to greeting.xhtml

Add the Form to response.html

Building, Packaging, Deploying, and Running the firstcup Web Application

Build, Package, and Deploy the firstcup Web Application

Run the firstcup Application

5.  Next Steps


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 an application requires conversational state, use stateful session beans.

To create DukesBirthdayBean, create one Java class: 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 coding artifact needed to create a local, no-interface enterprise bean.

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

Create the DukesBirthdayBean Enterprise Bean Class

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

  1. Select the firstcup project in the Projects tab.
  2. From the File menu, choose New File.
  3. In the Categories pane, select Enterprise JavaBeans.
  4. In the File Types pane, select Session Bean.
  5. Click Next.
  6. In the EJB Name field, type DukesBirthdayBean.
  7. In the Package field, type firstcup.ejb.
  8. Make sure Stateless is selected under Session Type.
  9. Click Finish.

    You should now see the file inside the firstcup.ejb package in the Projects tab. The file should also be open in the editor pane.

Add a Logger Instance to

Add a java.util.logging.Logger instance to the session bean for logging events.

  1. Directly after the class declaration, paste in the following field definition:
    private static final Logger logger = 

    This code creates a logger for the session bean.

  2. Right-click in the editor window and select Format.
  3. Right-click in the editor window and select Fix Imports, then click OK.

Add a Business Method to DukesBirthdayBean that Gets the Average Age Difference of firstcup Users

Add a business method to the DukesBirthdayBean session bean to call the findAverageAgeDifferenceOfAllFirstcupUsers named query in FirstcupUser that returns the average age difference of all users.

  1. Below the class definition, add a @PersistenceContext annotation and field of type EntityManager:
    private EntityManager em;
  2. Right-click in the editor window and select Fix Imports.
  3. Add a business method called getAverageAgeDifference by copying and pasting the following code:
        public Double getAverageAgeDifference() {
            Double avgAgeDiff =
                    (Double) em.createNamedQuery("findAverageAgeDifferenceOfAllFirstcupUsers")
  "Average age difference is: " + avgAgeDiff);
            return avgAgeDiff;

    The named query in FirstcupUser is called by using the EntityManager's createNamedQuery method. Because this query returns a single number, the getSingleResult method is called on the returned Query object. The query returns a Double.

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

Add a Business Method for Calculating the Age Difference Between Duke and the User

Add a business method that calculates the difference in age in years between Duke and the user and creates a new FirstcupUser entity.

  1. Add a business method called getAgeDifference by copying and pasting the following code:
        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)) {
            // create and store the user's birthday in the database
            FirstcupUser user = new FirstcupUser(date, ageDifference);
  "Final ageDifference is: " + ageDifference);
            return ageDifference;

    This method creates the Calendar objects used to calculate the difference in age between the user and Duke and performs 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 user's, and the raw difference is more than 0, it subtracts one year from the age difference.

    A new FirstcupUser entity is created with the user's birthday and age difference, then stored in the Java DB database by calling the EntityManager's persist method.

    The final age difference is returned as an int.

  2. Right-click in the editor window and select Format.
  3. Right-click in the editor window and select Fix Imports, then OK.
  4. From the File menu, choose Save.