9.2 A simple Web Service project

 < Day Day Up > 

The project will be created using the Web Project wizard. In this section, you create a Web project with the name PrimesWebService . You will import a JavaBean that generates prime numbers into this project. The Web project will also be published and executed in a WebSphere Test Environment Express.

  1. If not already running, start the IBM WebSphere Studio Site Developer; click Start -> Programs -> IBM WebSphere Studio -> Site Developer 5.0 .

  2. Select File -> New -> Other .

    Figure 9-3. New project creation

    graphics/09fig03.gif

  3. Select Web -> Web Project and click Next .

    Figure 9-4. Selection of Web Project

    graphics/09fig04.jpg

  4. Enter PrimesWebService for the Project name. Click Next .

    Figure 9-5. New Project wizard

    graphics/09fig05.jpg

  5. Select New for the Enterprise application project and enter PrimesWebServiceEAR (was DefaultEAR) in the New project name field. Click Finish .

    Figure 9-6. New Project wizard

    graphics/09fig06.gif

  6. A class file is provided for this sample scenario (Prime.java); follow these steps to import the Java file:

    1. Import the file by selecting File -> Import , select File system and click Next .

      Figure 9-7. Importing a file

      graphics/09fig07.gif

    2. In the Import File system window, enter the following information:

      1. Directory: browse to C:\LabFiles\WebServicesClient\Java Source , then select Primes.java .

        Note : You can also download the sample code available as additional materials. See Appendix C, "Additional material" on page 543.

      2. For the destination, browse to the folder: PrimesWebService/Java Source . Click Finish .

Figure 9-8. Importing the provided Java file

graphics/09fig08.jpg

9.2.1 A sample Web Service

In this section of the sample scenario, you will review the Primes.java code. For example, to view the source code to Primes.java , double-click the file name. This file is located in the /Java Source/ folder.

  • The only important method for you in this code is the getPrime() method. This method receive a number of digits and returns a prime number with the specified length of digits.

  • When executing this method, try generating prime numbers of 20 or fewer digits to avoid long computations . A short version of the prime number generator is shown in Example 9-1.

Example 9-1. Primes.java - getPrime() method
 import java.math.BigInteger; import java.util.Vector; public class Primes {    private static final BigInteger ZERO = new BigInteger("0");    private static final BigInteger ONE = new BigInteger("1");    private static final BigInteger TWO = new BigInteger("2");    private String prime = "";    ......    ......    ......  public static String getPrime(int numDigits) {   BigInteger start = random(numDigits)  ;  start = nextPrime(start)  ;  return start.toString()  ;  }   }  
Creating a Web Service

In this section, you create a Web Service in the PrimesWebService project that you just created. You will use the wizards provided by WebSphere Studio Site Developer to do this. Once created, the Web Service will be published and invoked to be executed in a WebSphere Test Environment.

  1. Select File -> New -> Other .

    Figure 9-9. Starting creation of Portlet project

    graphics/09fig09.gif

  2. Select Web Services -> Web Service .

    Figure 9-10. Selection of Web Service

    graphics/09fig10.jpg

  3. Click Next .

  4. Enter the following information in the Web Services window:

    1. Web Service type: Java bean Web Service .

    2. Check the box Generate a proxy .

    3. Client proxy type: Java proxy .

    4. Check the box Test the generate proxy .

    Click Next .

    Figure 9-11. New Project wizard

    graphics/09fig11.jpg

  5. In Web Service Deployment Settings, examine and accept default values. Click Next .

    Figure 9-12. Web Service Deployment Settings

    graphics/09fig12.jpg

  6. In the Web Service Java Bean Selection window, click Browse Files .

  7. Select the Primes.java class. Click OK .

    Figure 9-13. Primes.java selection

    graphics/09fig13.jpg

  8. Click Next to generate the Web Service.

  9. In the Web Service Bean Identity, examine and accept the default values. Click Next .

    Figure 9-14. Web Service Java Bean Identity

    graphics/09fig14.jpg

  10. In the Web Service Java Bean Methods :

    1. Select the getPrime(int) method.

    2. Deselect other methods as shown in Figure 9-15 on page 304.

      Figure 9-15. Web Service Java Bean Methods

      graphics/09fig15.jpg

    3. Click Next .

  11. In the Web Service Bind Proxy Generation window, accept the defaults and click Next to generate the proxy.

    Figure 9-16. Web Service Binding Proxy Generation

    graphics/09fig16.jpg

  12. I In the Web Service Test window, make sure you select the Run test on Server option. Click Next .

    Figure 9-17. Web Service Test

    graphics/09fig17.jpg

  13. In the Web Server Publication window, accept the defaults and click Finish to publish the Web Service.

    Figure 9-18. Web Service Publication

    graphics/09fig18.jpg

  14. You will be presented with the available methods in the built-in browser. It is a Web test client interface to access and verify that the Web Service is running as expected.

  15. Click the method getPrime(int) .

    Figure 9-19. Available methods

    graphics/09fig19.jpg

  16. Enter the number of digits you want the prime number to be. Do not try long numbers. It will be time consuming.

    Figure 9-20. Accessing a Web Service to generate prime numbers

    graphics/09fig20.jpg

  17. Every time you invoke this Web Service to generate a prime number, you will probably get a different result.

 < Day Day Up > 


IBM WebSphere Portal V5 A Guide for Portlet Application Development
IBM Websphere Portal V5: A Guide for Portlet Application Development
ISBN: 0738498513
EAN: 2147483647
Year: 2004
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net