⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cb4.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  <head>    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />    <meta http-equiv="Content-Style-Type" content="text/css" />    <title>SAAJ Distributor Service</title>    <link rel="StyleSheet" href="document.css" type="text/css" media="all" />    <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" />    <link rel="Table of Contents" href="J2EETutorialTOC.html" />    <link rel="Previous" href="CB3.html" />    <link rel="Next" href="CB5.html" />    <link rel="Index" href="J2EETutorialIX.html" />  </head>  <body>    <table width="550" summary="layout" id="SummaryNotReq1">      <tr>	<td align="left" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a>	</td>        <td align="center" valign="center"><a accesskey="p" href="CB3.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="CB5.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a>        </td>	<td align="right" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font>	</font>	</td>      </tr>    </table>    <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider">    <blockquote><a name="wp65335"> </a><h2 class="pHeading1">SAAJ Distributor Service</h2><a name="wp65834"> </a><p class="pBody">In contrast to the JAX-RPC service, the SAAJ distributor service does not register in a publicly accessible registry. It simply implements the arrangements that the distributor and the Coffee Break have made regarding their exchange of XML documents. These arrangements include what kinds of messages they will send, the form of those messages, and what kind of messaging they will do. They have agreed to do request-response messaging using the SAAJ API (the <code class="cCode">javax.xml.soap</code> package). </p><a name="wp68194"> </a><p class="pBody">The Coffee Break server sends two kinds of messages:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp68207"> </a><div class="pSmartList1"><li>Requests for current wholesale coffee prices</li></div><a name="wp68210"> </a><div class="pSmartList1"><li>Customer orders for coffee </li></div></ul></div><a name="wp68211"> </a><p class="pBody">The SAAJ coffee supplier responds with two kinds of messages:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp68212"> </a><div class="pSmartList1"><li>Current price lists</li></div><a name="wp68213"> </a><div class="pSmartList1"><li>Order confirmations</li></div></ul></div><a name="wp65911"> </a><p class="pBody">All of the messages they send conform to an agreed-upon XML structure, which is specified in a DTD for each kind of message. This allows them to exchange messages even though they use different document formats internally.</p><a name="wp66349"> </a><p class="pBody">The four kinds of messages exchanged by the Coffee Break server and the SAAJ distributor are specified by the following DTDs:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp66290"> </a><div class="pSmartList1"><li><code class="cCode">request-prices.dtd</code></li></div><a name="wp66291"> </a><div class="pSmartList1"><li><code class="cCode">price-list.dtd</code></li></div><a name="wp66292"> </a><div class="pSmartList1"><li><code class="cCode">coffee-order.dtd</code></li></div><a name="wp66293"> </a><div class="pSmartList1"><li><code class="cCode">confirm.dtd</code></li></div></ul></div><a name="wp66304"> </a><p class="pBody">These DTDs may be found at <code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/cb/saaj/dtds/</code>. The <code class="cCode">dtds</code> directory also contains a sample of what the XML documents specified in the DTDs might look like. The corresponding XML files for each of the DTDs are as follows:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp66317"> </a><div class="pSmartList1"><li><code class="cCode">request-prices.xml</code></li></div><a name="wp66318"> </a><div class="pSmartList1"><li><code class="cCode">price-list.xml</code></li></div><a name="wp66319"> </a><div class="pSmartList1"><li><code class="cCode">coffee-order.xml</code></li></div><a name="wp66320"> </a><div class="pSmartList1"><li><code class="cCode">confirm.xml</code></li></div></ul></div><a name="wp66366"> </a><p class="pBody">Because of the DTDs, both parties know ahead of time what to expect in a particular kind of message and can therefore extract its content using the SAAJ API.</p><a name="wp66372"> </a><p class="pBody">Code for the client and server applications is in the directory:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;<code class="cVariable">INSTALL</code>&gt;/j2eetutorial14/examples/cb/saaj/src/<a name="wp66373"> </a></pre></div><a name="wp66194"> </a><h3 class="pHeading2">SAAJ Client</h3><a name="wp66195"> </a><p class="pBody">The Coffee Break server, which is the SAAJ client in this scenario, sends requests to its SAAJ distributor. The SAAJ client application uses the <code class="cCode">SOAPConnection</code> method <code class="cCode">call</code> to send messages.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SOAPMessage response = con.call(request, endpoint);<a name="wp66196"> </a></pre></div><a name="wp66197"> </a><p class="pBody">Accordingly, the client code has two major tasks. The first is to create and send the request; the second is to extract the content from the response. These tasks are handled by the classes <code class="cCode"><a  href="../examples/cb/saaj/src/com/sun/cb/PriceListRequest.java" target="_blank">PriceListRequest</a></code> and <code class="cCode"><a  href="../examples/cb/saaj/src/com/sun/cb/OrderRequest.java" target="_blank">OrderRequest</a></code>. </p><a name="wp66201"> </a><h5 class="pHeading4">Sending the Request</h5><a name="wp66202"> </a><p class="pBody">This section covers the code for creating and sending the request for an updated price list. This is done in the <code class="cCode">getPriceList</code> method of <code class="cCode">PriceListRequest</code>, which follows the DTD <code class="cCode">price-list.dtd</code>.</p><a name="wp66394"> </a><p class="pBody">The <code class="cCode">getPriceList</code> method begins by creating the connection that will be used to send the request. Then it gets the default <code class="cCode">MessageFactory</code> object so that it can create the <code class="cCode">SOAPMessage</code> object <code class="cCode">msg</code>.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SOAPConnectionFactory scf = &nbsp;&nbsp;SOAPConnectionFactory.newInstance();SOAPConnection con = scf.createConnection();MessageFactory mf = MessageFactory.newInstance();SOAPMessage msg = mf.createMessage();<a name="wp66398"> </a></pre></div><a name="wp66406"> </a><p class="pBody">The next step is to access the message's <code class="cCode">SOAPEnvelope</code> object, which will be used to create a <code class="cCode">Name</code> object for each new element that is created. The <code class="cCode">SOAPEnvelope</code> object is also used to access the <code class="cCode">SOAPBody</code> object, to which the message's content will be added.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SOAPPart part = msg.getSOAPPart();SOAPEnvelope envelope = part.getEnvelope();SOAPBody body = envelope.getBody();<a name="wp66411"> </a></pre></div><a name="wp66415"> </a><p class="pBody">The file <code class="cCode">price-list.dtd</code> specifies that the top-most element inside the body is <code class="cCode">request-prices</code> and that it contains the element <code class="cCode">request</code>. The text node added to <code class="cCode">request</code> is the text of the request being sent. Every new element that is added to the message must have a <code class="cCode">Name</code> object to identify it, which is created by the <code class="cCode">Envelope</code> method <code class="cCode">createName</code>. The following lines of code create the top-level element in the <code class="cCode">SOAPBody</code> object <code class="cCode">body</code>. The first element created in a <code class="cCode">SOAPBody</code> object is always a <code class="cCode">SOAPBodyElement</code> object.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Name bodyName = envelope.createName(&quot;request-prices&quot;,&nbsp;&nbsp;&quot;RequestPrices&quot;, &quot;http://sonata.coffeebreak.com&quot;);SOAPBodyElement requestPrices =&nbsp;&nbsp;body.addBodyElement(bodyName);<a name="wp66423"> </a></pre></div><a name="wp66691"> </a><p class="pBody">In the next few lines, the code adds the element <code class="cCode">request</code> to the element <code class="cCode">request-prices</code> (represented by the <code class="cCode">SOAPBodyElement</code> <code class="cCode">requestPrices</code>). Then the code adds a text node containing the text of the request. Next, because there are no other elements in the request, the code calls the method <code class="cCode">saveChanges</code> on the message to save what has been done.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Name requestName = envelope.createName(&quot;request&quot;);SOAPElement request = &nbsp;&nbsp;requestPrices.addChildElement(requestName);request.addTextNode(&quot;Send updated price list.&quot;);msg.saveChanges();<a name="wp66428"> </a></pre></div><a name="wp66434"> </a><p class="pBody">With the creation of the request message completed, the code sends the message to the SAAJ coffee supplier. The message being sent is the <code class="cCode">SOAPMessage</code> object <code class="cCode">msg</code>, to which the elements created in the previous code snippets were added. The endpoint is the URI for the SAAJ coffee supplier, <code class="cCode">http://localhost:</code>8080<code class="cCode">/saaj-coffee-supplier/getPriceList</code>. The <code class="cCode">SOAPConnection</code> object <code class="cCode">con</code> is used to send the message, and because it is no longer needed, it is closed.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">URL endpoint = new URL(url);SOAPMessage response = con.call(msg, endpoint);con.close();<a name="wp66435"> </a></pre></div><a name="wp68526"> </a><p class="pBody">When the <code class="cCode">call</code> method is executed, the J2EE server executes the servlet <code class="cCode">PriceListServlet</code>. This servlet creates and returns a <code class="cCode">SOAPMessage</code> object whose content is the SAAJ distributor's price list. (<code class="cCode">PriceListServlet</code> is discussed in <a  href="CB4.html#wp65908">Returning the Price List</a>.) The J2EE server knows to execute <code class="cCode">PriceListServlet</code> because we map the given endpoint to that servlet.</p><a name="wp66826"> </a><h5 class="pHeading4">Extracting the Price List</h5><a name="wp66441"> </a><p class="pBody">This section demonstrates (1) retrieving the price list that is contained in <code class="cCode">response</code>, the <code class="cCode">SOAPMessage</code> object returned by the method <code class="cCode">call</code>, and (2) returning the price list as a <code class="cCode">PriceListBean</code>.</p><a name="wp66975"> </a><p class="pBody">The code creates an empty <code class="cCode">Vector</code> object that will hold the <code class="cCode">coffee-name</code> and <code class="cCode">price</code> elements that are extracted from <code class="cCode">response</code>. Then the code uses <code class="cCode">response</code> to access its <code class="cCode">SOAPBody</code> object, which holds the message's content. Notice that the <code class="cCode">SOAPEnvelope</code> object is not accessed separately because it is not needed for creating <code class="cCode">Name</code> objects, as it was in the previous section.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Vector list = new Vector();SOAPBody responseBody = 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -