Creating a Web Service

The starting point for developing a web service to use the WSIT technologies is a Java class file annotated with the javax.jws.WebService annotation. The WebService annotation defines the class as a web service endpoint. The following Java code shows a web service. The IDE will create most of this Java code for you.


import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

public class Calculator {
  public String operation(@WebParam(name="param_name") 
      String param) {
    // implement the web service operation here
    return param;

  public int add(@WebParam(name = "i") int i, 
      @WebParam(name = "j") int j) {
    int k = i + j; 
    return k;

Notice that this web service performs a very simple operation. It takes two integers, adds them, and returns the result.

Perform the following steps to use the IDE to create this web service:

  1. Click on the Runtime tab in the left pane and verify that GlassFish is listed in the left pane. If it is not listed, refer to Registering GlassFish with the IDE and register it.
  2. Choose FileRight ArrowNew Project, select Web Application from the Web category, and click Next.
  3. Assign the project a name that is representative of services that will be provided by the web service (for example, CalculatorApplication), set the Project Location to the location of the Sun application server, and click Finish.

Note: As of this writing, when creating the web service project be sure to define a Project Location that does not include spaces in the directory name. Spaces in the directory might cause the web service and web service clients to fail to build and deploy properly. To avoid this problem, Sun recommends that you create a directory, for example C:\work, and put your project there.

  1. Right-click the CalculatorApplication node and choose NewRight ArrowWeb Service.
  2. Enter the web service name (CalculatorWS) and the package name ( in the Web Service Name and the Package fields respectively.
  3. Select Create an Empty Web Service.
  4. Click Finish.
  5. The IDE then creates a skeleton file for the web service that includes an empty WebService class with annotation @Webservice.

  6. Right-click within the body of the class and choose Web ServiceRight ArrowAdd Operation.
  7. In the upper part of the Add Operation dialog box, type add in Name and choose int from the Return Type drop-down list.
  8. In the lower part of the Add Operation dialog box, click Add and create a parameter of type int named i. Click OK. Click Add again and create a parameter of type int called j. Click OK and close the Enter Method Parameter dialog box.
  9. Click OK at the bottom of the Add Operation dialog box.
  10. Notice that the add method has been added to the Source Editor:
  11.   @WebMethod
      public int add(@WebParam(name = "i") int i,
          @WebParam(name = "j") int j) {
        // TODO implement operation
        return 0;

  12. Change the add method to the following (changes are in bold):
  13.   @WebMethod(action="add")
      public int add(@WebParam(name = "i") int i,
          @WebParam(name = "j") int j) {
    int k = i + j;
        return k;

Note: To ensure interoperability with Windows Communication Foundation (WCF) clients, you must specify the action element of @WebMethod in your endpoint implementation classes. WCF clients will incorrectly generate an empty string for the Action header if you do not specify the action element.

  1. Save the file.

You have successfully coded the web service.