Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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

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


advertisement

Create and Deploy a Java Web Service
First 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:


public String sayHello(String name) 
   throws Exception {
   String hello = "Hello " + name;
   return hello;
}

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.

The deployment descriptor for the service looks like this:


<isd:service 
   xmlns:isd=http://xml.apache.org/xml-soap/deployment 
   id="urn:hello-service [type="message"] 
   [checkMustUnderstands="true|false"]>
   <isd:provider type="java" scope="Application" 
       methods="sayHello">
      <isd:java class="helloservice.HelloService" 
          static="false"/>
   </isd:provider>
 
   <isd:faultListener>
      org.apache.soap.server.DOMFaultListener
   </isd:faultListener>

   <isd:mappings>
      <isd:map encodingStyle=
         http://schemas.xmlsoap.org/soap/encoding/ 
         xmlns:x="" qname="x:name"        
         xml2JavaClassName=
            "org.apache.soap.encoding.
            soapenc.StringDeserializer"/>    
      </isd:mappings>
   </isd:service>

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.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date