HomeDocumentation > Developing > Tutorials > Developing Web services > Developing a simple Calculator Web service

This tutorial will walk you through building and developing the JAX-WS calculator that ships with the samples package for Geronimo. After completing this tutorial, you should be able to understand how to build simple Web services with Apache Geronimo
using the Eclipse development environment.

To run this tutorial, as a minimum you will be required to have installed the following prerequisite software.

  1. Sun JDK 6.0+ (J2SE 1.6)
  2. Eclipse IDE for Java EE Developers, which is platform specific
  3. Apache Geronimo Eclipse Plugin 2.2.x
  4. Apache Geronimo Server 2.2.x
    Icon

    Geronimo version 2.2, Java 1.6 runtime, and Eclipse Ganymede are used is used in this tutorial but other versions can be used instead (for example, Geronimo version 2.1, Java 1.5, Eclipse Europa)

Details on installing eclipse are provided in the Development environment section. This tutorial is organized in the following sections:

Setting Eclipse for application development

Create a Dynamic Web project

  1. Select File -> New, select Other....



  2. In the popup window select Web -> Dynamic Web Project category and click Next.



  3. Type in jaxws-calculator as the Project name and select Next.



  4. Make sure Generate Deployment Descriptor is selected and click Next.



  5. Modify the Group Id to: org.apache.geronimo.samples.jws and Artifact Id to: Calculator.



    Useful Information

    Icon
    • Group ID: A name that identifies the group of a module. The default value is default.
    • Artifact ID: A name of the module in a group. The default value is <blank>.
    • Version: The version of module. The default value is 1.0
    • Type: The type of module, such as system module (car), jar module (jar), web archive (war), enterprise archive (ear), and so on. The default value is car. A module is stored inside a repository as repository/groupId/artifactId/version/artifactIdversion.
      type. This repository structure gives a more standardized and easily maintainable
      structure to Geronimo applications.

Axis2 configuration

  1. Select Window -> Preferences.



  2. Select Web Services -> Axis2 Preferences.
  3. Under Axis2 Runtime tab, browse to the installation root of the Axis2 runtime installation to be used in Eclipse. Select OK.

    Useful Information

    Icon

    A separate Axis2 runtime is required for use by the Eclipse Axis2 project facet. You can use the pre-integrated Axis2 1.4 binaries (axis-1.4.jar) from the local Geronimo repository under <Geronimo_home>/repository/axis/axis/1.4

  4. Righ click on the jaxws-calculator project and select Properties, then select Project Facets.
  5. On the next screen check the box for Axis2 Web Services.



  6. Select Finish.
    This completes the configuration of Eclipse for application development.

Creating the Web services implementation code

To implement the Calculator Web service we are going to create a package called org.apache.geronimo.samples.jws. This package will contain two classes: a Calculator interface, and a CalculatorService which implements the Calculator interface. Let's go step by step with creating the package, interface class and implementation class.

  1. Right click on Java Resources: src and select New -> Package.



  2. Name the package as org.apache.geronimo.samples.jws. Select Finish.



  3. Right click on the new package and Select New -> Interface.



  4. Name the interface as Calculator. Select Finish.



  5. Add the following code to the Calculator interface class:
    Calculator.class
  6. Right click on the package org.apache.geronimo.samples.jws and select New -> Class.
  7. Name the class CalculatorService.
  8. Accept all the defaults and Select Finish..



  9. Add the following code to CalculatorService class:
    CalculatorService.class

Let us try to understand each annotation.

  • @WebService- This annotation can be used with a Java class as well as with interface. In our case we used it with both interface as well as the POJO. This annotation declares the POJO as a WebService. @WebService annotation is utilized in generating the WSDL file.
    • serviceName is same as the WSDL element service
    • name is same as the WSDL element <portType name>
    • endpointInterface suggests the user defined name for the Service Endpoint Interface (SEI).
    • portName is the element portName
    • targetNamespace is the XML namespace of the WSDL and some of the XML elements generated from the WebService
  • @WebMethod- This annotation is applied to a method to expose it as a WebService method. In case you have multiple methods you can use this annotation to selectively expose methods as WebService method. If you donot use this annotation all the public methods will be exposed as WebService.
  • @WebParam- This annotation is used along with @WebMethod annotation to define the WebService. It is used to customize parameter used in the message part of the WSDL.

This completes the development of the Web Service Implementation code.

Generating the WSDL for the Web service

Geronimo provides a new jaxws-tools.bat which helps in generating the WSDL file from service endpoint interface.

  • Export the source files to a jar as shown in the following three figures.









  • Open a command prompt and point it to the bin directory of server installation.
    1. Run the command:
      WSDL Generation command
      In this command -classpath is used to set the source.jar exported from Eclipse in the classpath, -d defines the location where all the generated artifacts will be placed, -wsdl:soap1.1 suggests a WSDL generation following soap1.1 protocol, and org.apache.geronimo.samples.jws.CalculatorService is the SEI used to generate the WSDL.
    2. Once the above command is run Calculator_schema1.xsd and Calculator.wsdl will be generated at C:/WSDL/.

    3. Rename Calculator.wsdl as CalculatorService.wsdl and add both the generated files to WEB-INF directory of the project.

Developing a Web client for Calculator

This section will take you through the creation of two JSP's: index.jsp and result.jsp. index.jsp will prompt the user to enter two whole number values to add together. After submitting the form, the action will be forwarded to the result.jsp, which will call the Calculator Web Service.

  1. Right click on the Web Content folder and Select New --> JSP.

  2. Name the JSP as index.jsp and select Finish.



  3. Add the following code to index.jsp:
    index.jsp
  4. Right click on the Web Content folder and Select New -> JSP.

  5. Name the JSP as result.jsp and select Finish.

  6. Add the following code to result.jsp:
    result.jsp
    This finishes the development of Web client.

Setting up the Deployment Descriptor and Deployment Plan

  1. Double click WEB-INF/web.xml and add the following code:
    web.xml
  2. Similarly double click geronimo-web.xml and add the following code:
    geronimo-web.xml

Deploy and Run

  1. Right click on jaxws-calculator project. Select Run As -> Run On Server.



  2. On the next window select Apache Geronimo V2.2 and Select Finish.



  3. Once the application is deployed on the server launch a browser and run the following URL http://localhost:8080/jaxws-calculator-1.0/index.jsp.




  4. Give the values as 5 and 7 and then select the = button:




  5. The result is 12 as shown in the figure: