Java Reference
In-Depth Information
return msg;
}
private Service createService() throws MalformedURLException {
URL wsdl =
new URL("http://localhost:8080/SecureCalculatorApp/" +
"CalculatorWSService?WSDL");
//Create the Service name
String svcName = "CalculatorWSService";
QName svcQName = new QName(NS, svcName);
//Get a delegate wrapper
Service service = Service.create(wsdl, svcQName);
System.out.println("Created Service: " + service.getServiceName());
return service;
}
}
Let's examine this code to see what's going on. First, this is just a simple console-based Java
program with a main method. You don't have any libraries on your classpath, only what you
get with Java SE 6. When the program runs, the main method creates a new instance of the
class and calls its invoke method. This method, in turn, does the three things that make the
round-trip. First, it creates a reference to the service using the createService method; next,
it creates a conforming SOAP message using the SAAJ API that can be effectively sent to the
service; and finally, it wires these together and uses the message to call the service, printing
out the request and response from the server.
The primary element that we're concerned with in this program is the header. Get a header
object directly from the envelope, just as you do the body. That returns an object of type
SOAPHeader . Here you will add a single header element to the header, though you could add
more if necessary. You create a QName for the header element you want to add, which in this
case is passwordHeader . Then you'll call the addHeaderElement method on the header in
order to place this new element as its child. That will create and return the element as a type
of SOAPHeaderElement . Then you can add your XML data to it. In this case, it's just a text
node containing your password.
Assessing the results
Here is the result of executing the program, with a little formatting added for readability:
Created Service: {urn:soacookbook.saaj}CalculatorWSService
Created Message:
Search WWH ::




Custom Search