background image

SAAJ Service

<< Retrieving the Order Confirmation | The SOAPMessage Object >>
<< Retrieving the Order Confirmation | The SOAPMessage Object >>

SAAJ Service

SAAJ Service
The SAAJ coffee supplier (the SAAJ server in this scenario) provides the response part of the
request-response paradigm. When SAAJ messaging is being used, the server code is a servlet.
The core part of each servlet is made up of three javax.servlet.HttpServlet methods: init,
doPost
, and onMessage. The init and doPost methods set up the response message, and the
onMessage
method gives the message its content.
Returning the Price List
This section takes you through the servlet PriceListServlet. This servlet creates the message
containing the current price list that is returned to the method call, invoked in
PriceListRequest
.
Any servlet extends a javax.servlet class. Being part of a web application, this servlet extends
HttpServlet
. It first creates a static MessageFactory object that will be used later to create the
SOAPMessage
object that is returned.
public class PriceListServlet extends HttpServlet {
static final Logger logger =
Logger.getLogger(
"com.sun.cb.saaj.PriceListServlet");
static MessageFactory messageFactory = null;
static {
try {
messageFactory = MessageFactory.newInstance();
} catch (Exception ex) {
logger.severe(
"Exception: " + ex.toString());
}
};
Every servlet has an init method. This init method initializes the servlet with the
configuration information that the Application Server passed to it.
public void init(ServletConfig servletConfig)
throws ServletException {
super.init(servletConfig);
}
The next method defined in PriceListServlet is doPost, which does the real work of the
servlet by calling the onMessage method. (The onMessage method is discussed later in this
section.) The Application Server passes the doPost method two arguments. The first argument,
the HttpServletRequest object req, holds the content of the message sent in
PriceListRequest
. The doPost method gets the content from req and puts it in the
SOAPMessage
object msg so that it can pass it to the onMessage method. The second argument,
the HttpServletResponse object resp, will hold the message generated by executing the
method onMessage.
SAAJ Coffee Supplier Service
The Java EE 5 Tutorial · September 2007
1040