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 an empty class.

Create an empty Java source file that will subsequently be modified.

  1. Right-click the firstcup module in the Projects pane.

  2. Select New -> Java Class

  3. Enter DukesBDay in the Class Name field.

  4. Select firstcup.web in the Package field.

  5. Click Finish.

ProcedureAnnotating the class with @ManagedBean and @SessionScoped.

The javax.faces.model.ManagedBean and javax.faces.model.SessionScoped annotations mark the class as a JSF managed bean that is valid for the length of a web session.

  1. Directly above the class declaration, add the @ManagedBean and @SessionScoped annotations.

    public class DukesBDay{
  2. Right-click in the editor and select Fix Imports.

  3. In the Fix All Imports dialog, select javax.faces.bean.SessionScoped as the fully-qualified class name for SessionScoped and click OK.

ProcedureAdding an Enterprise Bean Reference

Use the javax.ejb.EJB annotation to inject a reference to the DukesBirthdayBean enterprise bean.

  1. Directly after the class declaration, add a private variable to hold a reference to the enterprise bean using the @EJB annotation:

    private DukesBirthdayBean dukesBirthday;
  2. Right-click in the editor and select Fix Imports.

ProcedureAdding Properties to the Bean

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

  1. After the dukesBirthday variable declaration, add the following private variables:

    private int age;
    private Date yourBD;
    private int ageDiff;
    private int absAgeDiff;
    private static Logger logger = Logger.getLogger("firstcup.web.DukesBDay");
  2. Initialize the variables by adding a default constructor:

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

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

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

ProcedureGenerating the Accessor Methods for the Properties

Use the NetBeans 6.7 IDE Encapsulate Fields wizard to create getters and setters for the properties.

  1. Right-click in the editor window.

  2. Select Refactor->Encapsulate Fields from the popup window.

  3. In the Encapsulate Fields dialog, select the Create Getter and Create Setter checkboxes for age, yourBD, ageDiff, and absAgeDiff.

  4. Click Refactor.

    You should now see two methods for each property, one to set the value and one to get the value of the variable for that property.

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. Choose the fully-qualified name for the HttpURLConnection class.

  5. Choose the fully-qualified name for the ProtocolException exception.

  6. Choose the fully-qualified name for the URL class.

  7. Click OK.

ProcedureGetting the Age Difference From the DukesBirthdayBean Enterprise Bean

During this task, you will add code to the getAgeDiff method to get the difference in age between the user's age and Duke's age from the EJB and to set the absAgeDiff variable to the absolute value of the age difference.

  1. Inside the getAgeDiff method, directly before the return statement, add the following code:

    ageDiff = dukesBirthday.getAgeDifference(yourBD);
  2. Right-click in the editor window and select Format.

  3. Select File -> Save.