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:
Create an empty class.
Decorate the bean with a managed bean annotation and an enterprise bean reference.
Add a property that accesses Duke's current age from the JAX-RS web service.
Add a property that accesses the user's current birth date.
Add a property that accesses the age difference from the DukesBirthdayBean enterprise bean.
Add a property that accesses the absolute value of the age difference.
Create an empty Java source file that will subsequently be modified.
Right-click the firstcup module in the Projects pane.
Select New -> Java Class
Enter DukesBDay in the Class Name field.
Select firstcup.web in the Package field.
Click Finish.
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.
Directly above the class declaration, add the @ManagedBean and @SessionScoped annotations.
@ManagedBean @SessionScoped public class DukesBDay{ ... }
Right-click in the editor and select Fix Imports.
In the Fix All Imports dialog, select javax.faces.bean.SessionScoped as the fully-qualified class name for SessionScoped and click OK.
Use the javax.ejb.EJB annotation to inject a reference to the DukesBirthdayBean enterprise bean.
Directly after the class declaration, add a private variable to hold a reference to the enterprise bean using the @EJB annotation:
@EJB private DukesBirthdayBean dukesBirthday; |
Right-click in the editor and select Fix Imports.
During this task, you will add the following properties to the DukesBDay bean:
age for getting Duke's age from the web service.
yourBD to hold the user's birth date.
ageDiff to get the age difference from the enterprise bean.
absAgeDiff to hold the absolute value of the age difference.
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"); |
Initialize the variables by adding a default constructor:
public DukesBDay() { age = -1; yourBD = null; ageDiff = -1; absAgeDiff = -1; } |
Right-click in the editor and select Format.
Right-click in the editor and select Fix Imports.
Select java.util.Date for the Date class, java.util.logging.Logger for the Logger class, and click OK.
Use the NetBeans 6.7 IDE Encapsulate Fields wizard to create getters and setters for the properties.
Right-click in the editor window.
Select Refactor->Encapsulate Fields from the popup window.
In the Encapsulate Fields dialog, select the Create Getter and Create Setter checkboxes for age, yourBD, ageDiff, and absAgeDiff.
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.
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 java.net and java.io classes to create an HTTP connection to the Duke's Age web service and read in the result.
Add the following code to the getAge method.
public int getAge() { // Use the java.net.* 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( "http://localhost:8080/DukesAgeService/resources/dukesAge"); connection = (HttpURLConnection) serverAddress.openConnection(); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.setReadTimeout(10000); // Make the connection to Duke's Age connection.connect(); // Read in the response rd = new BufferedReader( new InputStreamReader(connection.getInputStream())); sb = new StringBuilder(); while ((line = rd.readLine()) != null) { sb.append(line); } // Convert the response to an int age = Integer.parseInt(sb.toString()); } catch (MalformedURLException e) { logger.warning("A MalformedURLException occurred."); e.printStackTrace(); } catch (ProtocolException e) { logger.warning("A ProtocolException occurred."); e.printStackTrace(); } catch (IOException e) { logger.warning("An IOException occurred"); e.printStackTrace(); } return age; }
Right-click in the editor window and select Format.
Right-click in the editor window and select Fix Imports.
Choose the java.net.HttpURLConnection fully-qualified name for the HttpURLConnection class.
Choose the java.net.ProtocolException fully-qualified name for the ProtocolException exception.
Choose the java.net.URL fully-qualified name for the URL class.
Click OK.
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.