SOAP Simple Object Access Protocol
SOAP (Simple Object Access Protocol) is a protocol for exchanging structured information in the implementation of web services in computer networks. It relies on XML (Extensible Markup Language) for its message format and usually relies on other application layer protocols, most notably HTTP (Hypertext Transfer Protocol) or SMTP (Simple Mail Transfer Protocol), for message negotiation and transmission. SOAP can form the foundation layer of a web services protocol stack, providing a basic messaging framework that more abstract layers can build on.

Table of Contents
Key Features of SOAP:
- 1. Â Protocol Independence : SOAP can be used over various protocols, including HTTP, SMTP, TCP, and more.
- 2. Â Extensibility : SOAP is designed to be extensible, allowing for the addition of features such as security and reliability.
- 3. Â Neutrality : SOAP is neutral and can be used with a variety of programming models.
- 4. Â Interoperability : SOAP allows for communication between applications running on different operating systems and technologies.
Example in Java
To illustrate how SOAP works in Java, we can create a simple SOAP-based web service and a client to consume this service.
Step 1: Create the Web Service
We'll use JAX-WS (Java API for XML Web Services) to create a SOAP web service.
Service Endpoint Interface (SEI)
java
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
@WebMethod
String sayHello(String name);
}
Service Implementation
java
import javax.jws.WebService;
@WebService(endpointInterface = "com.example.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHello(String name) {
return "Hello, " + name + "!";
}
}
Endpoint Publisher
java
import javax.xml.ws.Endpoint;
public class HelloWorldPublisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/ws/hello", new HelloWorldImpl());
System.out.println("Service is published!");
}
}
Step 2: Create the Client
java
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
public class HelloWorldClient {
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/ws/hello?wsdl");
// Qualified name of the service:
// 1st argument is the service URI
// 2nd argument is the service name published in the WSDL
QName qname = new QName("http://example.com/", "HelloWorldImplService");
Service service = Service.create(url, qname);
// Extract the endpoint interface, the service "port"
HelloWorld hello = service.getPort(HelloWorld.class);
System.out.println(hello.sayHello("World"));
}
}
Explanation of the Java Example
- 1. Â Service Endpoint Interface (SEI) : The HelloWorld interface defines a web service with a method sayHello, which takes a String as a parameter and returns a String.
- 2. Â Service Implementation : The HelloWorldImpl class implements the HelloWorld interface. The sayHello method simply returns a greeting message.
- 3. Â Endpoint Publisher : The HelloWorldPublisher class publishes the web service at the specified URL.
- 4. Â Client Code : The HelloWorldClient class demonstrates how to consume the published web service. It creates a service object using the WSDL URL, extracts the port for the HelloWorld interface, and invokes the sayHello method.