You want to use SAAJ on the server side to return a response to a SOAP request.
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 easier 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.
Here are the basic steps to follow to create a SAAJ-based service:
Create a regular web application structure as you would to create any WAR.
Create a servlet class that extends
In a static constructor, instantiate a static
doPost method, and create the SOAP
message for the response using SAAJ.
Return the response in the servlet’s output stream using the
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 response.
Example 5-12. Servlet SOAP provider
package saaj.provider; import java.io.IOException; ...