Friday, August 08, 2008

The world of Web Services

A web service is a piece of business logic, located somewhere on the Internet, that is accessible through standard Internet protocols such as HTTP or SMTP.
Let’s see the definition for web service as provided by W3C. According to them, a web service is a software application which is identified by a URI, whose interfaces and binding are capable of being defined, described and discovered by XML using XML-based messages via Internet-based protocols.
A web service is usually identified by a URI (Unified Recourse Identifier). A web service has WSDL (Web Service Description Language) definitions. To communicate with web services we need to use SOAP messages, which are XML based messages transported over Internet protocols like HTTP, SMTP, and FTP. Web services can be better described with the following diagram.
Here, the service requester is the client of the web service, and the service provider is the host of the web service. A requester makes SOAP requests to the provider and the provider responses accordingly. A service registry can be thought of as a database of web services. It has the definitions and URIs for web services. Developers of web services can publish their services to a service registry if they wish. After that someone can query the registry and choose the service from the registry. Since a registry has all the information required to develop a web client, developers can search and find necessary information from a service registry using UDDI (Universal Description, Discovery and Integration).
A developer usually publishes the WSDL for their services in the registry. A client can query the registry using UDDI and get the WSDL from the registry. After that the client can establish a connection with the service and send SOAP requests to the service, whose response will also be SOAP messages.

SOAP is used for communication between applications via the Internet. SOAP is platform independent. SOAP is also language independent; it is based on XML. Each SOAP message has a mandatory SOAP envelope, and multiple optional attachments. A SOAP envelope consists of an optional SOAP header, a mandatory body, and optional fault sections. Usually a SOAP body contains the original requests or the responses.

WSDL is an XML document, which is used to describe web services. WSDL is also used to locate web services.

Let’s define Web services platform elements.

SOAP
The basic Web services platform is XML plus HTTP.

  • SOAP stands for Simple Object Access Protocol
  • SOAP is a communication protocol
  • SOAP is for communication between applications
  • SOAP is a format for sending messages
  • SOAP is designed to communicate via Internet
  • SOAP is platform independent
  • SOAP is language independent
  • SOAP is based on XML
  • SOAP is simple and extensible
  • SOAP allows you to get around firewalls
  • SOAP will be developed as a W3C standard

WSDL

WSDL is an XML-based language for describing Web services and how to access them.

  • WSDL stands for Web Services Description Language
  • WSDL is written in XML
  • WSDL is an XML document
  • WSDL is used to describe Web services
  • WSDL is also used to locate Web services
  • WSDL is not yet a W3C standard

UDDI

UDDI is a directory service where businesses can register and search for Web services.

  • UDDI stands for Universal Description, Discovery and Integration
  • UDDI is a directory for storing information about web services
  • UDDI is a directory of web service interfaces described by WSDL
  • UDDI communicates via SOAP
  • UDDI is built into the Microsoft .NET platform

Using Apache Axis for developing web services..

Axis is essentially a SOAP engine -- a framework for constructing SOAP processors such as clients, servers, gateways, etc.
Or we can simply say that Axis is an implementation of SOAP which turns Java code into Web services.
The latest version of Axis can be downloaded from their Web site, http://ws.apache.org/axis.

After downloading the latest version of Axis Unzip the package downloaded to a folder in your system. Further we can create on axis.war and copy it to tomcat webapps folder.

To validate the proper installation try http://localhost:8080/axis in explorer.. you should see the Apache-Axis start page. If you are not able to see the page then probably your web application is not deployed properly.
We can create one sample web service using axis in few steps..
(1) first create a normal project in Eclipse IDE by simple importing the axis.war
use file --> import option and then select War file.

(2) Create a simple Hello.jws and save it in the WebContent folder of Axis project. see the code below.

public class Hello {
public String sayHello(String name){

System.out.println("in hello service");

return "Hello"+name;

}

}

Note: any .java file can be simply be saved as .jws
(3) Now create another java project (a client) which will use our hello web service. See the sample code below .it shows how we can call our service.
import java.net.URL;

import javax.xml.namespace.QName;

import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

import org.apache.axis.encoding.XMLType;

public class HelloClient {

public static void main(String[] args) {

try {

Service service=new Service();

Call call=(Call) service.createCall();

call.setTargetEndpointAddress(new URL(http://localhost:8080/axis/Hello.jws));

call.setOperation(new QName("Hello"), "sayHello");

call.addParameter("in0", XMLType.XSD_STRING, ParameterMode.IN);

call.setReturnType(XMLType.XSD_STRING);

Object returnval=call.invoke(new Object[] {"India"});

System.out.println(returnval.toString());

} catch(Exception e)

{ e.printStackTrace();

}

}

}

Now we can run our client which will call our service internally. Make sure that your tomcat server is up and running when you run the client program.We can even use other tools like BEA Weblogic Workshop to create web services. These tools generate most of the code automatically. It also provides features to provide security and test our web services as well.

Popular Posts