The Currency Converter Web Service XML

The Currency Converter Web service is used to convert one currency into another. This Web service accepts units of a particular currency and then converts them to another currency. This currency is then displayed as a result. The steps involved in the creation of the Currency Converter Web service are listed next:

  • Create the directory that will contain the files for the service.
  • Create a Web service.
  • Create and implement a class for performing the conversion to the Web service.
  • Add XML operations.
  • Add a reference of the implementation method and the XML operator to the service.
  • Compile the Java files.
  • Add a client to the Web service.
  • Run the Web service.

We will now explain how to perform each of these steps.

Creating the Directory

You need to create a directory that will store the files of the Web service. To create a directory, perform the following steps:

  1. Start Sun ONE Studio. The main interface that is displayed is shown in Figure.
  2. The Main Interface of Sun ONE Studio.

    The Main Interface of Sun ONE Studio.

  3. Open the Explorer window by selecting the Explorer command on the View menu.
  4. The Explorer window is displayed as in Figure.

    The Explorer Window

    The Explorer Window

  5. Create a new folder from the Explorer by right-clicking and selecting the Folder command on the New menu, as displayed.
  6. The New Wizard—Folder dialog box is displayed, as shown in Figure. Specify the folder name and then click on the Finish button. In this example, we have specified the folder name as Currency_Converter.
  7. The New Wizard—Folder Dialog Box

    The New Wizard—Folder Dialog Box

  8. Click on the Finish button. Notice that the Currency_Converter node is added to the Explorer window, as shown in Figure.
  9. The Explorer Window

    The Explorer Window

Creating the Web Service

As discussed, the next step is to create the Web service. To do this, perform the following steps:

  1. Right-click the Currency_Converter folder and select the New, Web Services, Web Service command from the shortcut menu, as shown in Figure.
  2. The Web Services Command on the New Submenu

    The Web Services Command on the New Submenu

  3. The New Wizard—Web Service dialog box is displayed. In this dialog box, specify the Web service name as well as other details, as shown in Figure.
  4. The New Wizard—Web Service Dialog Box

    The New Wizard—Web Service Dialog Box

  5. Click on the Finish button to complete the process. When you click the Finish button, the Explorer window is again displayed, as shown in Figure.
  6. The Explorer Window

    The Explorer Window

Creating and Implementing the Class

The next step is to add the implementation class to the service. The class contains methods that will implement the logic of the tasks that the Web service will perform. Carry out the following steps to add the implementation method:

  1. Select the File, New command. In the New Wizard dialog box that is displayed, select Empty under the Classes folder, as shown in Figure.
  2. The Selection of the Empty Class Option

    The Selection of the Empty Class Option

  3. Click the Next button. The Target Location page of the New Wizard—Empty dialog box is displayed.
  4. Specify the class name as CurrConverter in the Name text box, and then select the package as Currency_Converter, as shown in Figure.
  5. The Target Location Page

    The Target Location Page

  6. Click the Finish button to close the dialog box. The Explorer window is displayed again with the class name added to it.

    The Explorer Window Displays the Class that you Created

    The Explorer Window Displays the Class that you Created
  7. Add the code to the implementation class. You can do this by adding the code in the Source Editor dialog box, as shown in Figure.
  8. The Source Editor Window

    The Source Editor Window

  9. To save the code, right-click and select the Save command from the shortcut menu. Notice in Figure that the class name is added to the Explorer window.
  10. The Class Name in the Explorer Window

    The Class Name in the Explorer Window

  11. To compile the class, right-click the class name and select the Compile command from the shortcut menu. The output of the compilation process is displayed in the Output window, as shown in Figure.

    The Output Window Shows the Compilation Result

    The Output Window Shows the Compilation Result

Adding the XML Operation

Next, you need to add the XML operations to the Web service. The XML operation encapsulated the business method. It specifies how a request to the Web service will be processed.

  1. Right-click the Currency_Converter node and then select the New, Web Services, XML Operation command from the shortcut menu that is displayed, as shown in Figure
  2. The Selection of the XML Operation Command

    The Selection of the XML Operation Command

  3. The New Wizard—XML Operation dialog box is displayed. Specify the operation name as CurrencyOperation in the Name text box, as shown in Figure.
  4. The New Wizard—XML Operation Dialog Box

    The New Wizard—XML Operation Dialog Box

  5. Click the Browse button next to the Method text box. Select the method name, as shown in Figure.
  6. The Select Method Dialog Box

    The Select Method Dialog Box

  7. Click the OK button to close the Select Method dialog box. The New Wizard—XML Operation dialog box is displayed again.
  8. Click on the Finish button to close the dialog box. In the Explorer window, as shown in Figure, notice that the CurrencyOperation node is added.
  9. The Explorer Window Displays the XML Operation that you Added

    The Explorer Window Displays the XML Operation that you Added

Adding a Reference to the Web Service

The next step is to add a reference to the method and XML operations that you created for the Web service. To add the reference, you need to perform the following steps:

  1. Right-click CurrConvertService and select the Add Reference command from the shortcut menu.
  2. In the Add Reference dialog box, select the method name and the XML operation name. The Add Reference dialog box is displayed, as shown in Figure.
  3. The Add Reference Dialog Box

    The Add Reference Dialog Box

  4. Click the OK button to close the Add Reference dialog box.

Compiling the Java Files

You need to compile the Java files. You create some of these files, whereas Sun ONE Studio creates others. To compile the files, right-click CurrConvertService and then select Generate/ Compile Java File from the shortcut menu. The output window shows the result, as shown in Figure.

The Output Window

The Output Window

Adding a Client to the Web Service

The last step in developing the Web service is to create the client that will access the service. To create the client, perform the following steps:

  1. Right-click CurrConvertService and then select the New Client command from the shortcut menu. The New Client dialog box is shown, as displayed in Figure.
  2. The New Client Dialog Box

    The New Client Dialog Box

  3. Click on the OK button to close the New Client dialog box.

Executing the Web Service

Now that you have created the client, you're ready to execute the Web service. To execute the Web service, right-click CurrConvertService and then click the Execute command. The progress bar displays the progress, as shown in Figure.

The Progress Monitor

The Progress Monitor

Running the Web Service

Sun ONE Studio automatically opens the browser and displays the client to you. You can now enter a value to convert and click the Invoke button to view the result, as shown in Figure.

The Web Browser

The Web Browser

For example, if you enter 100 in the Pound text box and then click the Invoke button, the result is displayed in Figure.

The Result of the Currency Conversion

The Result of the Currency Conversion

You have now learned how to create the Java Web service in the Sun ONE Studio IDE.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

XML Topics