Working with Web Services - Using Web Services from Standalone Applications
(Page 6 of 9 )
As you've seen, using Web services from within a Workshop application is easy. But what about using Workshop-created Web services from within a standalone Java client or JSP page?
Preparing to Run client.java - Windows developers can run the client example by modifying the prompt.cmd file found in chapter09standalone and pointing the BEA_HOME directory to the location of your WebLogic Platform install. Double-clicking the command file opens a property-configured DOS window with the appropriate classpath and path settings. Unix developers need only run the commEnv.sh shell script found in weblogic81/common/bin and add the provided JAR files to the classpath.
Workshop-based Web services provide proxy JAR files that enable standalone clients to call Web services directly. Listing 9.1 details a simple Java client that uses the checkcredit method of the client service's Web service.
Listing 9.1 client.java import weblogic.jws.proxies.*;
public class client {
public static void main(String[] args) {
try {
System.out.println("Attempting to get credit services proxy");
CreditServices_Impl proxy = new CreditServices_Impl();
System.out.println("Using proxy to get service object instance");
CreditServicesSoap sp = proxy.getcreditServicesSoap();
System.out.println("Calling service method");
System.out.println("check credit(Al,100) returned:" +
+ sp.checkcredit("Al",100));
System.out.println("check credit(Tom,1000) returned:" +
sp.checkcredit("Tom",1000));
System.out.println("check credit(Unknown,1000) returned:" +
sp.checkcredit("Unknown",1000));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Using a Web service from a Java client requires first downloading the client proxy JAR and its associated Web service's support JAR file (in this example, creditServices.jar and webservicesclient.jar), and then adding them to the application's classpath.
After you've obtained the required JARs, using the Web service is a three-step coding process:
- Obtain a handle to the proxy implementation of the service. Using the CreditServices Web service, you simply append _Impl to the name of the Web service. Notice that Workshop capitalizes the first letter of the name:CreditServices_Impl proxy = new CreditServices_Impl();
- Obtain a handle to a SOAP proxy:CreditServicesSoap sp = proxy.getcreditServicesSoap();
For all practical purposes, you can think of the SOAP proxy as though it were an instance of a local Java object implementing the actual business method you're interested in.
- Finally, call the method of interest: System.out.println("check credit(Al,100) returned:" + sp.checkcredit("Al",100));
Of course, you can also use the client normally from a JSP by writing similar code, but embedding it appropriately within <% and %> JSP tags.
Java Clients and Web Service URLs - It is assumed that the URL of the Web service implementation the client is using is the same as the URL where the client JAR file was downloaded. However, the actual URL is contained in the client JAR file WSDL within the WSDL address element. You can change this element if needed to access the same Web service at a different location, perhaps when moving from development to testing or from testing to production.
This chapter is from BEA WebLogic Workshop 8.1, by Albert J. Saganich, Jr., et al. (Sams, 2004, ISBN: 0-672-32622-1). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Conversations >>
More Graphic Design Articles
More By Albert J. Saganich, Jr.