Java Solaris Communities My SDN Account Join SDN

Previous Previous     Contents     Next Next

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.

ProcedureCreate the Project in NetBeans

  1. Select File->New Project.

  2. Select Web in the Categories pane.

  3. Select Web Application in the Projects pane.

  4. Click Next.

  5. Set Project Name to firstcup-dukes-age.

  6. Set the Project Location to <INSTALL>/myexample, in which INSTALL is the location of the firstcup tutorial installation.

  7. Select your Application Server from the Server menu.

  8. Select Java EE 5 from the Java EE Version menu.

  9. Set Context Path to /DukesAgeService

  10. Click Finish.

    You should now see the module you created in the Projects pane.

  11. From the Projects pane, right-click on the index.jsp file and select Delete. Click Yes in the dialog.

ProcedureCreate the DukesAge Class

  1. Select File->New File.

  2. Make sure firstcup-dukes-age is selected in the Project menu.

  3. Select Java Classes in the Categories pane.

  4. Select Java Class in the File Types pane.

  5. Click Next.

  6. Set Class Name to DukesAge.

  7. Set Package to com.sun.firstcup.webservice.

  8. 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.

ProcedureAnnotate the DukesAge Class as a Web Service

  • Add a @WebService annotation to the class.

    @WebService
    public class DukesAge {
    ...
    }

ProcedureRemove the Default Constructor

  • Highlight the following default constructor and delete it, as web service endpoints do not require a default constructor.

    public DukesAge() {
    }

ProcedureAdd the getDukesAge Method

  1. Create a public getDukesAge method with a return type of int.

    public int getDukesAge() {
    }

  2. Add a @WebMethod annotation to getDukesAge.

    @WebMethod
    public int getDukesAge() {
    }

  3. 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;

ProcedureResolve the Import Statements

  1. Right-click in the Editor.

  2. Select Fix Imports.

  3. In the Fix Imports dialog, choose the javax.jws.WebService package for the WebService class.

  4. Select File->Save from the menu to save the file.

Previous Previous     Contents     Next Next
Company Info Contact Terms of Use Privacy Copyright 1994-2006 Sun Microsystems, Inc.