Building and Deploying the Web Service
Building DukesAgeService
Select firstcup-dukes-age in the Projects tab.
Right-click firstcup-dukes-age and select Build Project.
Deploying the Web Service Endpoint
The DukesAgeService endpoint was packaged in a WAR file, firstcup-dukes-age.war. Now you'll deploy firstcup-dukes-age.war to the Application Server. This task gives instructions on deploying firstcup-dukes-age.war in NetBeans.
Select firstcup-dukes-age in the Projects tab.
Right-click firstcup-dukes-age and select Deploy Project.
Creating the Enterprise Bean
DukesBirthdayBean is a stateless session bean. Stateless session beans are 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 two Java source files: DukesBirthdayBean, the enterprise bean class; and DukesBirthdayRemote, the enterprise bean business interface. The enterprise bean class and the business interface are the only files you need to create an enterprise bean.
Creating DukesBirthdayBean in NetBeans
This section has instructions for creating the enterprise application and DukesBirthdayBean enterprise bean.
Creating the Enterprise Application
In this task, you will create an enterprise application archive (EAR) that will contain the DukesBirthdayBean enterprise bean and firstup web client.
Select File->New Project.
Select Enterprise in the Categories pane.
Select Enterprise Application in the Projects pane.
Click Next.
Set Project Name to firstcup.
Set the Project Location to <INSTALL>/myexample.
By default the wizard creates an enterprise bean module firstcup-ejb and a web application module firstcup-war. Leave everything in the dialog as it is.
Click Finish.
Setting the Context Root
In this task, you will specify the context root to identify the web application in a J2EE server.
Expand the firstcup module in the Projects pane.
In the Configuration Files node, double-click the application.xml file.
In application.xml, locate the context-root element.
Change the context root from /firstcup-war to /firstcup.
Select File -> Save to save the file.
Right--click the firstcup-war module in the Projects pane.
Select Properties from the popup menu.
In the Categories tree, select Run.
Change the Context Path to /firstcup.
Click OK.
Creating the DukesBirthdayBeanEnterprise
Bean Class
Now you'll create the enterprise bean class and business interface source files in NetBeans. The DukesBirthdayRemote business interface is a remote interface.
Select firstcup-ejb project in the Projects tab.
Select File->New File.
Select Enterprise in the Categories pane.
Select Session Bean in the File Types pane.
Click Next.
Set EJB Name to DukesBirthdayBean.
Set the Package name to com.sun.firstcup.ejb.
Set the Session Type to Stateless.
Uncheck Local and check Remote under Create Interface.
Click Finish.
Modify DukesBirthdayBean.java
Now you'll add the code that calculates the difference in age in years between Duke and the user.
In DukesBirthdayBean.java, delete the empty default constructor that NetBeans generated.
Directly after the class declaration, paste in the following code:
private static Logger logger = Logger.getLogger("com.sun.firstcup.ejb.DukesBirthdayBean"); 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 theirBirthday.setTime(date); // Subtract the user's age from Duke's age ageDifference = dukesBirthday.get(Calendar.YEAR) - theirBirthday.get(Calendar.YEAR); logger.info("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)) { ageDifference--; } logger.info("Final ageDifference is: " + ageDifference); return ageDifference; }Right-click in the editor window and select Fix Imports.
Choose the java.util.logging.Logger fully-qualified name for the Logger class.
Choose the java.util.Date fully-qualified name for the Date class.
Click OK.
Right-click within the new getAgeDifference method and select EJB Methods->Add to Remote Interface.
Select File->Save.

Previous