Creating the Web Service Endpoint
The DukesAgeService endpoint is a simple web service. Web services are web-based applications that use open, XML-based standards and transport protocols to exchange data with calling clients. Both the requests and responses are sent as XML documents, and are usually sent as HTTP packets. This makes interoperability between different systems and applications easy, as it is not necessary for the client to know the underlying architecture of the server and vice-versa to make a successful web service call.
Web services are designed to be independent of the client. Typically web service endpoints are publicly available to a wide variety of clients, and the clients are located throughout the internet. This is called "loose coupling," as the clients and servers are connected only by the standard XML-based requests and responses. For this reason, DukesAgeService will be developed in its own application module, and deployed separately from the DukesBirthdayBean enterprise bean and firstcup web client.
JAX-WS Endpoints
DukesAgeService is a JAX-WS endpoint implemented as a servlet. Servlets are web components that run in the web container.
We'll begin by creating a servlet class, then decorate the class with the @WebServiceannotation to make the class a web service endpoint, and finally we will add the getDukesAge method to calculate and return Duke's age.
Creating the Endpoint
In NetBeans or another editor, create a Java class source file called DukesAge.java in the com.sun.firstcup.webservice package.
Create the Project in NetBeans
Select File->New Project.
Select Web in the Categories pane.
Select Web Application in the Projects pane.
Click Next.
Set Project Name to firstcup-dukes-age.
Set the Project Location to <INSTALL>/myexample, in which INSTALL is the location of the firstcup tutorial installation.
Select your Application Server from the Server menu.
Select Java EE 5 from the Java EE Version menu.
Set Context Path to /DukesAgeService
Click Finish.
You should now see the module you created in the Projects pane.
From the Projects pane, right-click on the index.jsp file and select Delete. Click Yes in the dialog.
Create the DukesAge Class
Select File->New File.
Make sure firstcup-dukes-age is selected in the Project menu.
Select Java Classes in the Categories pane.
Select Java Class in the File Types pane.
Click Next.
Set Class Name to DukesAge.
Set Package to com.sun.firstcup.webservice.
Click Finish.
You should now see the DukesAge.java file inside the com.sun.firstcup.webservice package in the Projects pane. The DukesAge.java file should also be open in the editor pane.
Annotate the DukesAge Class as
a Web Service
Add a @WebService annotation to the class.
@WebService public class DukesAge { ... }
Remove the Default Constructor
Highlight the following default constructor and delete it, as web service endpoints do not require a default constructor.
public DukesAge() { }
Add the getDukesAge Method
Create a public getDukesAge method with a return type of int.
public int getDukesAge() { }Add a @WebMethod annotation to getDukesAge.
@WebMethod public int getDukesAge() { }Add the following code to getDukesAge:
Calendar dukesBirthday = new GregorianCalendar(1995, Calendar.MAY, 23); Calendar now = Calendar.getInstance(); int dukesAge = now.get(Calendar.YEAR) - dukesBirthday.get(Calendar.YEAR); dukesBirthday.add(Calendar.YEAR, dukesAge); if (now.before(dukesBirthday)) { dukesAge--; } return dukesAge;
Resolve the Import Statements
Right-click in the Editor.
Select Fix Imports.
In the Fix Imports dialog, choose the javax.jws.WebService package for the WebService class.
Select File->Save from the menu to save the file.

Previous