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

📄 cb4.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 4 页
字号:
&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;} }<a name="wp72948"> </a></pre></div><a name="wp68274"> </a><p class="pBody">The method <code class="cCode">onMessage</code> is the application code for responding to the message sent by <code class="cCode">PriceListRequest</code> and internalized into <code class="cCode">msg</code>. It uses the static <code class="cCode">MessageFactory</code> object <code class="cCode">fac</code> to create the <code class="cCode">SOAPMessage</code> object <code class="cCode">message</code> and then populates it with the distributor's current coffee prices.</p><a name="wp68696"> </a><p class="pBody">The method <code class="cCode">doPost</code> invokes <code class="cCode">onMessage</code> and passes it <code class="cCode">msg</code>. In this case, <code class="cCode">onMessage</code> does not need to use <code class="cCode">msg</code> because it simply creates a message containing the distributor's price list. The <code class="cCode">onMessage</code> method in <code class="cCode">ConfirmationServlet (</code><a  href="CB4.html#wp65933">Returning the Order Confirmation</a>), on the other hand, uses the message passed to it to get the order ID.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public SOAPMessage onMessage(SOAPMessage msg) { &nbsp;&nbsp;SOAPMessage message = null; &nbsp;&nbsp;try { &nbsp;&nbsp;&nbsp;&nbsp;message = fac.createMessage();&nbsp;&nbsp;&nbsp;&nbsp;SOAPPart part = message.getSOAPPart(); &nbsp;&nbsp;&nbsp;&nbsp;SOAPEnvelope envelope = part.getEnvelope(); &nbsp;&nbsp;&nbsp;&nbsp;SOAPBody body = envelope.getBody();&nbsp;&nbsp;&nbsp;&nbsp;Name bodyName = envelope.createName(&quot;price-list&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;PriceList&quot;, &quot;http://sonata.coffeebreak.com&quot;);&nbsp;&nbsp;&nbsp;&nbsp;SOAPBodyElement list = body.addBodyElement(bodyName);&nbsp;&nbsp;&nbsp;&nbsp;Name coffeeN = envelope.createName(&quot;coffee&quot;);&nbsp;&nbsp;&nbsp;&nbsp;SOAPElement coffee = list.addChildElement(coffeeN);&nbsp;&nbsp;&nbsp;&nbsp;Name coffeeNm1 = envelope.createName(&quot;coffee-name&quot;);&nbsp;&nbsp;&nbsp;&nbsp;SOAPElement coffeeName = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;coffee.addChildElement(coffeeNm1);&nbsp;&nbsp;&nbsp;&nbsp;coffeeName.addTextNode(&quot;Arabica&quot;);&nbsp;&nbsp;&nbsp;&nbsp;Name priceName1 = envelope.createName(&quot;price&quot;); &nbsp;&nbsp;&nbsp;&nbsp;SOAPElement price1 = coffee.addChildElement(priceName1);&nbsp;&nbsp;&nbsp;&nbsp;price1.addTextNode(&quot;4.50&quot;);&nbsp;&nbsp;&nbsp;&nbsp;Name coffeeNm2 = envelope.createName(&quot;coffee-name&quot;); &nbsp;&nbsp;&nbsp;&nbsp;SOAPElement coffeeName2 =&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;coffee.addChildElement(coffeeNm2);&nbsp;&nbsp;&nbsp;&nbsp;coffeeName2.addTextNode(&quot;Espresso&quot;);&nbsp;&nbsp;&nbsp;&nbsp;Name priceName2 = envelope.createName(&quot;price&quot;); &nbsp;&nbsp;&nbsp;&nbsp;SOAPElement price2 = coffee.addChildElement(priceName2);&nbsp;&nbsp;&nbsp;&nbsp;price2.addTextNode(&quot;5.00&quot;);&nbsp;&nbsp;&nbsp;&nbsp;Name coffeeNm3 = envelope.createName(&quot;coffee-name&quot;); &nbsp;&nbsp;&nbsp;&nbsp;SOAPElement coffeeName3 =&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;coffee.addChildElement(coffeeNm3);&nbsp;&nbsp;&nbsp;&nbsp;coffeeName3.addTextNode(&quot;Dorada&quot;);&nbsp;&nbsp;&nbsp;&nbsp;Name priceName3 = envelope.createName(&quot;price&quot;); &nbsp;&nbsp;&nbsp;&nbsp;SOAPElement price3 = coffee.addChildElement(priceName3);&nbsp;&nbsp;&nbsp;&nbsp;price3.addTextNode(&quot;6.00&quot;);&nbsp;&nbsp;&nbsp;&nbsp;Name coffeeNm4 = envelope.createName(&quot;coffee-name&quot;); &nbsp;&nbsp;&nbsp;&nbsp;SOAPElement coffeeName4 =&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;coffee.addChildElement(coffeeNm4);&nbsp;&nbsp;&nbsp;&nbsp;coffeeName4.addTextNode(&quot;House Blend&quot;);&nbsp;&nbsp;&nbsp;&nbsp;Name priceName4 = envelope.createName(&quot;price&quot;); &nbsp;&nbsp;&nbsp;&nbsp;SOAPElement price4 = coffee.addChildElement(priceName4);&nbsp;&nbsp;&nbsp;&nbsp;price4.addTextNode(&quot;5.00&quot;);&nbsp;&nbsp;&nbsp;&nbsp;message.saveChanges();&nbsp;&nbsp;} catch(Exception e) { &nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace(); &nbsp;&nbsp;} &nbsp;&nbsp;return message; } <a name="wp68275"> </a></pre></div><a name="wp65933"> </a><h4 class="pHeading3">Returning the Order Confirmation</h4><a name="wp68733"> </a><p class="pBody"><code class="cCode"><a  href="../examples/cb/saaj/src/com/sun/cb/ConfirmationServlet.java" target="_blank">ConfirmationServlet</a></code> creates the confirmation message that is returned to the <code class="cCode">call</code> method that is invoked in <code class="cCode">OrderRequest</code>. It is very similar to the code in <code class="cCode">PriceListServlet</code> except that instead of building a price list, its <code class="cCode">onMessage</code> method builds a confirmation with the order number and shipping date.</p><a name="wp70070"> </a><p class="pBody">The <code class="cCode">onMessage</code> method for this servlet uses the <code class="cCode">SOAPMessage</code> object passed to it by the <code class="cCode">doPost</code> method to get the order number sent in <code class="cCode">OrderRequest</code>. Then it builds a confirmation message with the order ID and shipping date. The shipping date is calculated as today's date plus two days.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public SOAPMessage onMessage(SOAPMessage message) {&nbsp;&nbsp;SOAPMessage confirmation = null;&nbsp;&nbsp;try {&nbsp;&nbsp;&nbsp;&nbsp;// Retrieve orderID from message received&nbsp;&nbsp;&nbsp;&nbsp;SOAPBody sentSB = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;message.getSOAPPart().getEnvelope().getBody();&nbsp;&nbsp;&nbsp;&nbsp;Iterator sentIt = sentSB.getChildElements();&nbsp;&nbsp;&nbsp;&nbsp;SOAPBodyElement sentSBE = (SOAPBodyElement)sentIt.next();&nbsp;&nbsp;&nbsp;&nbsp;Iterator sentIt2 = sentSBE.getChildElements();&nbsp;&nbsp;&nbsp;&nbsp;SOAPElement sentSE = (SOAPElement)sentIt2.next();&nbsp;&nbsp;&nbsp;&nbsp;// Get the orderID test to put in confirmation&nbsp;&nbsp;&nbsp;&nbsp;String sentID = sentSE.getValue();&nbsp;&nbsp;&nbsp;&nbsp;// Create the confirmation message&nbsp;&nbsp;&nbsp;&nbsp;confirmation = fac.createMessage();&nbsp;&nbsp;&nbsp;&nbsp;SOAPPart sp = confirmation.getSOAPPart();&nbsp;&nbsp;&nbsp;&nbsp;SOAPEnvelope env = sp.getEnvelope();&nbsp;&nbsp;&nbsp;&nbsp;SOAPBody sb = env.getBody();&nbsp;&nbsp;&nbsp;&nbsp;Name newBodyName = env.createName(&quot;confirmation&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Confirm&quot;, &quot;http://sonata.coffeebreak.com&quot;);&nbsp;&nbsp;&nbsp;&nbsp;SOAPBodyElement confirm = sb.addBodyElement(newBodyName);&nbsp;&nbsp;&nbsp;&nbsp;// Create the orderID element for confirmation&nbsp;&nbsp;&nbsp;&nbsp;Name newOrderIDName = env.createName(&quot;orderId&quot;);&nbsp;&nbsp;&nbsp;&nbsp;SOAPElement newOrderNo =&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;confirm.addChildElement(newOrderIDName);&nbsp;&nbsp;&nbsp;&nbsp;newOrderNo.addTextNode(sentID);&nbsp;&nbsp;&nbsp;&nbsp;// Create ship-date element&nbsp;&nbsp;&nbsp;&nbsp;Name shipDateName = env.createName(&quot;ship-date&quot;);&nbsp;&nbsp;&nbsp;&nbsp;SOAPElement shipDate = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;confirm.addChildElement(shipDateName);&nbsp;&nbsp;&nbsp;&nbsp;// Create the shipping date&nbsp;&nbsp;&nbsp;&nbsp;Date today = new Date();&nbsp;&nbsp;&nbsp;&nbsp;long msPerDay = 1000 * 60 * 60 * 24;&nbsp;&nbsp;&nbsp;&nbsp;long msTarget = today.getTime();&nbsp;&nbsp;&nbsp;&nbsp;long msSum = msTarget + (msPerDay * 2);&nbsp;&nbsp;&nbsp;&nbsp;Date result = new Date();&nbsp;&nbsp;&nbsp;&nbsp;result.setTime(msSum);&nbsp;&nbsp;&nbsp;&nbsp;String sd = result.toString();&nbsp;&nbsp;&nbsp;&nbsp;shipDate.addTextNode(sd);&nbsp;&nbsp;&nbsp;&nbsp;confirmation.saveChanges();&nbsp;&nbsp;} catch (Exception ex) {&nbsp;&nbsp;&nbsp;&nbsp;ex.printStackTrace();&nbsp;&nbsp;}&nbsp;&nbsp;return confirmation;}<a name="wp69951"> </a></pre></div>    </blockquote>   <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider">    <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"><p><font size="-1">All of the material in <em>The J2EE(TM) 1.4 Tutorial</em> is <a href="J2EETutorialFront2.html">copyright</a>-protected and may not be published in other workswithout express written permission from Sun Microsystems.</font>  </body></html>

⌨️ 快捷键说明

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