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

📄 saaj3.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<?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>Tutorial</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="SAAJ2.html" />    <link rel="Next" href="SAAJ4.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="SAAJ2.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="SAAJ4.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="wp72105"> </a><h2 class="pHeading1">Tutorial</h2><a name="wp64004"> </a><p class="pBody">This tutorial will walk you through how to use the SAAJ API. First, it covers the basics of creating and sending a simple SOAP message. Then you will learn more details about adding content to messages, including how to create SOAP faults and attributes. Finally, you will learn how to send a message and retrieve the content of the response. After going through this tutorial, you will know how to perform the following tasks:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp77156"> </a><div class="pSmartList1"><li><a  href="SAAJ3.html#wp78641">Creating and Sending a Simple Message</a></li></div><a name="wp77159"> </a><div class="pSmartList1"><li><a  href="SAAJ3.html#wp78691">Adding Content to the Header</a></li></div><a name="wp77160"> </a><div class="pSmartList1"><li><a  href="SAAJ3.html#wp64112">Adding Content to the SOAP Body</a></li></div><a name="wp77161"> </a><div class="pSmartList1"><li><a  href="SAAJ3.html#wp64119">Adding Content to the SOAPPart Object</a></li></div><a name="wp77162"> </a><div class="pSmartList1"><li><a  href="SAAJ3.html#wp78963">Adding a Document to the SOAP Body</a></li></div><a name="wp77163"> </a><div class="pSmartList1"><li><a  href="SAAJ3.html#wp80734">Manipulating Message Content Using SAAJ or DOM APIs</a></li></div><a name="wp80845"> </a><div class="pSmartList1"><li><a  href="SAAJ3.html#wp80876">Adding Attachments</a></li></div><a name="wp80853"> </a><div class="pSmartList1"><li><a  href="SAAJ3.html#wp77353">Adding Attributes</a></li></div><a name="wp80860"> </a><div class="pSmartList1"><li><a  href="SAAJ3.html#wp77454">Using SOAP Faults</a></li></div></ul></div><a name="wp72137"> </a><p class="pBody">In the section <a  href="SAAJ4.html#wp66012">Code Examples</a>, you will see the code fragments from earlier parts of the tutorial in runnable applications, which you can test yourself. To see how the SAAJ API can be used in server code, see the SAAJ part of the case study (<a  href="CB4.html#wp65335">SAAJ Distributor Service</a>), which shows an example of both the client and server code for a Web service application.</p><a name="wp64012"> </a><p class="pBody">A SAAJ client can send request-response messages to Web services that are implemented to do request-response messaging. This section demonstrates how you can do this.</p><a name="wp78641"> </a><h3 class="pHeading2">Creating and Sending a Simple Message</h3><a name="wp78644"> </a><p class="pBody">This section covers the basics of creating and sending a simple message and retrieving the content of the response. It includes the following topics:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp78648"> </a><div class="pSmartList1"><li><a  href="SAAJ3.html#wp64023">Creating a Message</a></li></div><a name="wp78649"> </a><div class="pSmartList1"><li><a  href="SAAJ3.html#wp64028">Parts of a Message</a></li></div><a name="wp78650"> </a><div class="pSmartList1"><li><a  href="SAAJ3.html#wp64035">Accessing Elements of a Message</a></li></div><a name="wp78651"> </a><div class="pSmartList1"><li><a  href="SAAJ3.html#wp64044">Adding Content to the Body</a></li></div><a name="wp80812"> </a><div class="pSmartList1"><li><a  href="SAAJ3.html#wp78656">Getting a SOAPConnection Object</a></li></div><a name="wp80817"> </a><div class="pSmartList1"><li><a  href="SAAJ3.html#wp78665">Sending a Message</a></li></div><a name="wp80822"> </a><div class="pSmartList1"><li><a  href="SAAJ3.html#wp78677">Getting the Content of a Message</a></li></div></ul></div><a name="wp64023"> </a><h4 class="pHeading3">Creating a Message</h4><a name="wp64024"> </a><p class="pBody">The first step is to create a message, which you do using a <code class="cCode">MessageFactory</code> object. The SAAJ API provides a default implementation of the <code class="cCode">MessageFactory</code> class, thus making it easy to get an instance. The following code fragment illustrates getting an instance of the default message factory and then using it to create a message.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">MessageFactory factory = MessageFactory.newInstance();SOAPMessage message = factory.createMessage();<a name="wp64025"> </a></pre></div><a name="wp64026"> </a><p class="pBody">As is true of the <code class="cCode">newInstance</code> method for <code class="cCode">SOAPConnectionFactory</code>, the <code class="cCode">newInstance</code> method for <code class="cCode">MessageFactory</code> is static, so you invoke it by calling <code class="cCode">MessageFactory.newInstance</code>.</p><a name="wp64028"> </a><h4 class="pHeading3">Parts of a Message</h4><a name="wp64029"> </a><p class="pBody">A <code class="cCode">SOAPMessage</code> object is required to have certain elements, and, as stated previously, the SAAJ API simplifies things for you by returning a new <code class="cCode">SOAPMessage</code> object that already contains these elements. So <code class="cCode">message</code>, which was created in the preceding line of code, automatically has the following:</p><a name="wp64030"> </a><p class="pBody">I.&nbsp;&nbsp;A <code class="cCode">SOAPPart</code> object that contains</p><a name="wp64031"> </a><p class="pBody">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A.&nbsp;&nbsp;A <code class="cCode">SOAPEnvelope</code> object that contains</p><a name="wp64032"> </a><p class="pBody">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1.&nbsp;&nbsp;An empty <code class="cCode">SOAPHeader</code> object</p><a name="wp64033"> </a><p class="pBody">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2.&nbsp;&nbsp;An empty <code class="cCode">SOAPBody</code> object</p><a name="wp64034"> </a><p class="pBody">The <code class="cCode">SOAPHeader</code> object is optional and may be deleted if it is not needed. However, if there is one, it must precede the <code class="cCode">SOAPBody</code> object. The <code class="cCode">SOAPBody</code> object can hold the content of the message and can also contain fault messages that contain status information or details about a problem with the message. The section <a  href="SAAJ3.html#wp77454">Using SOAP Faults</a> walks you through how to use <code class="cCode">SOAPFault</code> objects.</p><a name="wp64035"> </a><h4 class="pHeading3">Accessing Elements of a Message</h4><a name="wp64036"> </a><p class="pBody">The next step in creating a message is to access its parts so that content can be added. There are two ways to do this. The <code class="cCode">SOAPMessage</code> object <code class="cCode">message</code>, created in the previous code fragment, is the place to start. </p><a name="wp78060"> </a><p class="pBody">The first way to access the parts of the message is to work your way through the structure of the message. The message contains a <code class="cCode">SOAPPart</code> object, so you use the <code class="cCode">getSOAPPart</code> method of <code class="cCode">message</code> to retrieve it:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SOAPPart soapPart = message.getSOAPPart();<a name="wp64037"> </a></pre></div><a name="wp64038"> </a><p class="pBody">Next you can use the <code class="cCode">getEnvelope</code> method of <code class="cCode">soapPart</code> to retrieve the <code class="cCode">SOAPEnvelope</code> object that it contains.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SOAPEnvelope envelope = soapPart.getEnvelope();<a name="wp64039"> </a></pre></div><a name="wp64040"> </a><p class="pBody">You can now use the <code class="cCode">getHeader</code> and <code class="cCode">getBody</code> methods of <code class="cCode">envelope</code> to retrieve its empty <code class="cCode">SOAPHeader</code> and <code class="cCode">SOAPBody</code> objects.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SOAPHeader header = envelope.getHeader();SOAPBody body = envelope.getBody();<a name="wp64041"> </a></pre></div><a name="wp77996"> </a><p class="pBody">The second way to access the parts of the message is to retrieve the message header and body directly, without retrieving the <code class="cCode">SOAPPart</code> or <code class="cCode">SOAPEnvelope</code>. To do so, use the <code class="cCode">getSOAPHeader</code> and <code class="cCode">getSOAPBody</code> methods of <code class="cCode">SOAPMessage</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SOAPHeader header = message.getSOAPHeader();SOAPBody body = message.getSOAPBody();<a name="wp77997"> </a></pre></div><a name="wp78092"> </a><p class="pBody">This example of a SAAJ client does not use a SOAP header, so you can delete it. (You will see more about headers later.) Because all <code class="cCode">SOAPElement</code> objects, including <code class="cCode">SOAPHeader</code> objects, are derived from the <code class="cCode">Node</code> interface, you use the method <code class="cCode">Node.detachNode</code> to delete <code class="cCode">header</code>.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">header.detachNode();<a name="wp78093"> </a></pre></div><a name="wp64044"> </a><h4 class="pHeading3">

⌨️ 快捷键说明

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