Cross Language Barriers with SOAP and a Java Web Service : Page 2

SOAP lives up to its promise of cross-language interaction.

WEBINAR:

On-Demand

Create and Deploy a Java Web ServiceFirst you need to create a simple Java class that exposes a sayHello() method, which takes one String input parameter, appends "Hello" to the start of the parameter, and then returns the result, which looks like "Hello xyz" where "xyz" is the input parameter. Here's the sayHello() code:

Put the method in a class named HelloService and in a package named helloservice. Next, you need to make this class act as a Web service by deploying it to a Web server or application server. I used Apache and Tomcat for the example, but any Java-compatible application server should work just as well.

Author Note: Before deploying this Web service you must install Apache SOAP on your server. Configuring Apache SOAP is simple, first download the Apache SOAP files and put the soap.jar file in your server's lib directory. For example, on Tomcat put the soap.jar file in the $HOME_TOMCAT\lib folder. If you're already using the Java Web Services Developer Pack Early Access 1 (JWSDP) you don't need to install Apache SOAP, because the soap.jar file installs with JWSDP in the $HOME_JWSDP\common\lib folder.

Apache SOAP uses XML documents called deployment descriptors to provide information to the SOAP runtime about the Web services available to clients. Deployment descriptors provide information such as the Universal Resource Name (URN) for the service (which is used to route the request when it comes in), the method, and (for Web services implemented by Java classes) the class details.

In the preceding deployment descriptor, hello-service is the URN that you want to give to a service; sayHello is a space-separated list of methods that you want to expose; and helloservice.HelloService is the fully qualified class name (the packagename.classname) that implements the methods being exposed.

The <java> element accepts an optional attribute called static, which you can set to "true" when the methods you want to expose are static. The <provider> element also accepts a scope attribute, which indicates the instantiation lifetime of the implementing class. The value "Request" indicates that the Web service destroys the object when the request is complete, and the value "Session" indicates that the object will persist for the lifetime of the current HTTP session. Finally, the value "Application" indicates that the object persists until the servlet that is servicing the requests terminates.