Your First Cup: An Introduction to the Java EE Platform

Creating the DukesBDay Managed Bean Class

The DukesBDay JavaBeans component is a backing bean. A backing bean is a JavaServer Faces managed bean that acts as a temporary data storage for the values of the components included on a particular JavaServer Faces page. A managed bean is a JavaBeans component that a JavaServer Faces application instantiates and stores in scope. The section following this one describes more about managed beans and how to configure them.

This section describes how to create the DukesBDay class. To create the class you need to do the following:

ProcedureCreating the Managed Bean Class.

Create a JavaServer Faces managed bean class that will subsequently be modified.

  1. Right-click the firstcup.web package in the Projects pane.

  2. Select New -> Other.

  3. Select JavaServer Faces under Categories, then JSF Managed Bean under File Types.

  4. Enter DukesBDay in the Class Name field.

  5. Select firstcup.web in the Package field.

  6. Select session under Scope.

  7. Click Finish.

ProcedureAdding an Enterprise Bean Reference

Add a javax.ejb.EJB annotation to inject a reference to the DukesBirthdayBean enterprise bean. This session bean will be called from the methods in DukesBDay.

  1. Right-click in the editor window and select Call Enterprise Bean.

  2. In the Call Enterprise Bean dialog expand the firstcup application, select DukesBirthdayBean, and click OK. The following field will be added:

    private DukesBirthdayBean dukesBirthdayBean;

ProcedureAdding Properties to the Bean

During this task, you will add the following properties to the DukesBDay bean:

  1. Right-click in the Editor window, select Insert Code, then Add Property.

  2. Enter age under Name, and set the Type to int, click OK.

  3. Repeat the above steps to create the following properties: yourBD of type java.util.Date, ageDiff of type int, absAgeDiff of type int, and averageAgeDifference of type Double.

  4. After the newly created property fields, add the following Logger instance:

    private static Logger logger = Logger.getLogger("firstcup.web.DukesBDay");
  5. Initialize the variables in the default constructor:

    public DukesBDay() {
    	age = -1;
    	yourBD = null;
    	ageDiff = -1;
    	absAgeDiff = -1;
  6. Right-click in the editor and select Format.

  7. Right-click in the editor and select Fix Imports.

  8. Select java.util.logging.Logger for the Logger class, and click OK.

ProcedureGetting Duke's Current Age

While performing this task, you will add some code to the getAge method to access Duke's current age from the JAX-RS web service.

Use the and classes to create an HTTP connection to the Duke's Age web service and read in the result.

  1. Add the following code to the getAge method.

    public int getAge() {
        // Use the* APIs to access the Duke's Age RESTful web service
        HttpURLConnection connection = null;
        BufferedReader rd = null;
        StringBuilder sb = null;
        String line = null;
        URL serverAddress = null;
        try {
            serverAddress = new URL(
            connection = (HttpURLConnection) serverAddress.openConnection();
            // Make the connection to Duke's Age
            // Read in the response
            rd = new BufferedReader(
    				new InputStreamReader(connection.getInputStream()));
            sb = new StringBuilder();
            while ((line = rd.readLine()) != null) {
            // Convert the response to an int
            age = Integer.parseInt(sb.toString());
        } catch (MalformedURLException e) {
            logger.warning("A MalformedURLException occurred.");
        } catch (ProtocolException e) {
            logger.warning("A ProtocolException occurred.");
        } catch (IOException e) {
            logger.warning("An IOException occurred");
    	return age;
  2. Right-click in the editor window and select Format.

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

  4. Click OK.

ProcedureGetting the Age Difference From the DukesBirthdayBean Enterprise Bean

During this task, you will create a processBirthday method to get the difference in age between the user's age and Duke's age from the EJB, set the absAgeDiff variable to the absolute value of the age difference, and set a result string that is will forward the user to the display page..

  1. Add a processBirthday method with the following code:

        public String processBirthday() {
  "age diff from dukesbday " + ageDiff);
  "absAgeDiff " + absAgeDiff);
            return new String("success");

    This method calls the getAgeDifference method of DukesBirthdayBean to get the age difference and store it in the ageDiff property, sets the absolute age difference stored in the absAgeDiff property, and returns a status code of success. This status code will be used by the JavaServer Faces runtime to forward the user to the appropriate page.

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

  3. Select File -> Save.