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. -
If not already running, start the IBM WebSphere Studio Site Developer; click Start -> Programs -> IBM WebSphere Studio -> Site Developer 5.0 . -
Select File -> New -> Other . Figure 9-3. New project creation -
Select Web -> Web Project and click Next . Figure 9-4. Selection of Web Project -
Enter PrimesWebService for the Project name. Click Next . Figure 9-5. New Project wizard -
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 -
A class file is provided for this sample scenario (Prime.java); follow these steps to import the Java file: -
Import the file by selecting File -> Import , select File system and click Next . Figure 9-7. Importing a file -
In the Import File system window, enter the following information: -
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. -
For the destination, browse to the folder: PrimesWebService/Java Source . Click Finish . Figure 9-8. Importing the provided Java file 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. -
Select File -> New -> Other . Figure 9-9. Starting creation of Portlet project -
Select Web Services -> Web Service . Figure 9-10. Selection of Web Service -
Click Next . -
Enter the following information in the Web Services window: -
Web Service type: Java bean Web Service . -
Check the box Generate a proxy . -
Client proxy type: Java proxy . -
Check the box Test the generate proxy . Click Next . Figure 9-11. New Project wizard -
In Web Service Deployment Settings, examine and accept default values. Click Next . Figure 9-12. Web Service Deployment Settings -
In the Web Service Java Bean Selection window, click Browse Files . -
Select the Primes.java class. Click OK . Figure 9-13. Primes.java selection -
Click Next to generate the Web Service. -
In the Web Service Bean Identity, examine and accept the default values. Click Next . Figure 9-14. Web Service Java Bean Identity -
In the Web Service Java Bean Methods : -
Select the getPrime(int) method. -
Deselect other methods as shown in Figure 9-15 on page 304. Figure 9-15. Web Service Java Bean Methods -
Click Next . -
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 -
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 -
In the Web Server Publication window, accept the defaults and click Finish to publish the Web Service. Figure 9-18. Web Service Publication -
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. -
Click the method getPrime(int) . Figure 9-19. Available methods -
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 -
Every time you invoke this Web Service to generate a prime number, you will probably get a different result. |