Java Reference
In-Depth Information
Providing a Web Service with SAAJ
Problem
You want to use SAAJ on the server side to return a response to a SOAP request.
Solution
Create a regular servlet and use the SAAJ API to create a SOAP message response, much as
you do on the client. This is rarely done in practice because annotations make it so much easi-
er and less brittle to do. But there are always times when you want to do things by hand, or
perhaps experiment to see what JAX-WS is doing under the hood.
Discussion
Here are the basic steps to follow to create a SAAJ-based service:
1. Create a regular web application structure as you would to create any WAR.
2. Create a servlet class that extends javax.servlet.http.HttpServlet .
3. In a static constructor, instantiate a static javax.xml.soap.MessageFactory field.
4. Override the doPost method, and create the SOAP message for the response using SAAJ.
5. Return the response in the servlet's output stream using the SOAPMessage.writeTo meth-
od.
In this example, we'll walk through creating a Hello World type application. To keep it as
simple as possible, you are just going to invoke the servlet with a regular HTTP request, but
return a SOAP response.
The code listing in Example 5-12 shows the servlet that implements the SAAJ SOAP provider.
There really is no magic here at all. You're just using standard SAAJ stuff that you've seen
throughout this chapter, and building a regular servlet that returns the SOAP message as a re-
sponse.
Example5-12.Servlet SOAP provider
package saaj.provider;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Search WWH ::




Custom Search