📄 cb4.html
字号:
} } }<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) { SOAPMessage message = null; try { message = fac.createMessage(); SOAPPart part = message.getSOAPPart(); SOAPEnvelope envelope = part.getEnvelope(); SOAPBody body = envelope.getBody(); Name bodyName = envelope.createName("price-list", "PriceList", "http://sonata.coffeebreak.com"); SOAPBodyElement list = body.addBodyElement(bodyName); Name coffeeN = envelope.createName("coffee"); SOAPElement coffee = list.addChildElement(coffeeN); Name coffeeNm1 = envelope.createName("coffee-name"); SOAPElement coffeeName = coffee.addChildElement(coffeeNm1); coffeeName.addTextNode("Arabica"); Name priceName1 = envelope.createName("price"); SOAPElement price1 = coffee.addChildElement(priceName1); price1.addTextNode("4.50"); Name coffeeNm2 = envelope.createName("coffee-name"); SOAPElement coffeeName2 = coffee.addChildElement(coffeeNm2); coffeeName2.addTextNode("Espresso"); Name priceName2 = envelope.createName("price"); SOAPElement price2 = coffee.addChildElement(priceName2); price2.addTextNode("5.00"); Name coffeeNm3 = envelope.createName("coffee-name"); SOAPElement coffeeName3 = coffee.addChildElement(coffeeNm3); coffeeName3.addTextNode("Dorada"); Name priceName3 = envelope.createName("price"); SOAPElement price3 = coffee.addChildElement(priceName3); price3.addTextNode("6.00"); Name coffeeNm4 = envelope.createName("coffee-name"); SOAPElement coffeeName4 = coffee.addChildElement(coffeeNm4); coffeeName4.addTextNode("House Blend"); Name priceName4 = envelope.createName("price"); SOAPElement price4 = coffee.addChildElement(priceName4); price4.addTextNode("5.00"); message.saveChanges(); } catch(Exception e) { e.printStackTrace(); } 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) { SOAPMessage confirmation = null; try { // Retrieve orderID from message received SOAPBody sentSB = message.getSOAPPart().getEnvelope().getBody(); Iterator sentIt = sentSB.getChildElements(); SOAPBodyElement sentSBE = (SOAPBodyElement)sentIt.next(); Iterator sentIt2 = sentSBE.getChildElements(); SOAPElement sentSE = (SOAPElement)sentIt2.next(); // Get the orderID test to put in confirmation String sentID = sentSE.getValue(); // Create the confirmation message confirmation = fac.createMessage(); SOAPPart sp = confirmation.getSOAPPart(); SOAPEnvelope env = sp.getEnvelope(); SOAPBody sb = env.getBody(); Name newBodyName = env.createName("confirmation", "Confirm", "http://sonata.coffeebreak.com"); SOAPBodyElement confirm = sb.addBodyElement(newBodyName); // Create the orderID element for confirmation Name newOrderIDName = env.createName("orderId"); SOAPElement newOrderNo = confirm.addChildElement(newOrderIDName); newOrderNo.addTextNode(sentID); // Create ship-date element Name shipDateName = env.createName("ship-date"); SOAPElement shipDate = confirm.addChildElement(shipDateName); // Create the shipping date Date today = new Date(); long msPerDay = 1000 * 60 * 60 * 24; long msTarget = today.getTime(); long msSum = msTarget + (msPerDay * 2); Date result = new Date(); result.setTime(msSum); String sd = result.toString(); shipDate.addTextNode(sd); confirmation.saveChanges(); } catch (Exception ex) { ex.printStackTrace(); } 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 + -