📄 saaj4.html
字号:
<?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>Code Examples</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="SAAJ3.html" /> <link rel="Next" href="SAAJ5.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="SAAJ3.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="SAAJ5.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="wp66012"> </a><h2 class="pHeading1">Code Examples</h2><a name="wp66013"> </a><p class="pBody">The first part of this tutorial used code fragments to walk you through the fundamentals of using the SAAJ API. In this section, you will use some of those code fragments to create applications. First, you will see the program <code class="cCode">Request.java</code>. Then you will see how to run the programs <code class="cCode">MyUddiPing.java</code>, <code class="cCode">HeaderExample.java</code>, <code class="cCode">DOMExample.java</code>, <code class="cCode">Attachments.java</code>, and <code class="cCode">SOAPFaultTest.java</code>.</p><a name="wp89024"> </a><p class="pBody">You do not have to start the J2EE Application Server in order to run these examples.</p><a name="wp93454"> </a><p class="pBody">You do not have to start the J2EE Application Server in order to run these examples.</p><a name="wp66014"> </a><h3 class="pHeading2">Request.java</h3><a name="wp66273"> </a><p class="pBody">The class <code class="cCode">Request.java</code> puts together the code fragments used in <a href="SAAJ3.html#wp72105">Tutorial</a> and adds what is needed to make it a complete example of a client sending a request-response message. In addition to putting all the code together, it adds <code class="cCode">import</code> statements, a <code class="cCode">main</code> method, and a <code class="cCode">try</code>/<code class="cCode">catch</code> block with exception handling. </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">import javax.xml.soap.*;import java.util.*;import java.net.URL;public class Request { public static void main(String[] args) { try { SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection connection = soapConnectionFactory.createConnection(); SOAPFactory soapFactory = SOAPFactory.newInstance(); MessageFactory factory = MessageFactory.newInstance(); SOAPMessage message = factory.createMessage(); <code class="cCode">SOAPHeader header = message.getSOAPHeader();</code> SOAPBody body = message.getSOAPBody(); header.detachNode(); Name bodyName = soapFactory.createName( "GetLastTradePrice", "m", "http://wombats.ztrade.com"); SOAPBodyElement bodyElement = body.addBodyElement(bodyName); Name name = soapFactory.createName("symbol"); SOAPElement symbol = bodyElement.addChildElement(name); symbol.addTextNode("SUNW"); URL endpoint = new URL ("http://wombat.ztrade.com/quotes"); SOAPMessage response = connection.call(message, endpoint); connection.close(); SOAPBody soapBody = response.getSOAPBody(); Iterator iterator = soapBody.getChildElements(bodyName); SOAPBodyElement bodyElement = (SOAPBodyElement)iterator.next(); String lastPrice = bodyElement.getValue(); System.out.print("The last price for SUNW is "); System.out.println(lastPrice); } catch (Exception ex) { ex.printStackTrace(); } }}<a name="wp78754"> </a></pre></div><a name="wp64931"> </a><p class="pBody">In order for <code class="cCode">Request.java</code> to be runnable, the second argument supplied to the method <code class="cCode">call</code> would have to be a valid existing URI, which is not true in this case. However, the application in the next section is one that you can run.</p><a name="wp70889"> </a><h3 class="pHeading2">MyUddiPing.java</h3><a name="wp65194"> </a><p class="pBody">The program <code class="cCode">MyUddiPing.java</code> is another example of a SAAJ client application. It sends a request to a Universal Description, Discovery and Integration (UDDI) service and gets back the response. A UDDI service is a business registry and repository from which you can get information about businesses that have registered themselves with the registry service. For this example, the <code class="cCode">MyUddiPing</code> application is not actually accessing a UDDI service registry but rather a test (demo) version. Because of this, the number of businesses you can get information about is limited. Nevertheless, <code class="cCode">MyUddiPing</code> demonstrates a request being sent and a response being received. </p><a name="wp77489"> </a><h4 class="pHeading3">Setting Up</h4><a name="wp68506"> </a><p class="pBody">The <code class="cCode">myuddiping</code> example is in the following directory:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><<code class="cVariable">INSTALL</code>>/j2eetutorial14/examples/saaj/myuddiping/<a name="wp79002"> </a></pre></div><hr><a name="wp85304"> </a><p class="pNote">Note: <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">></code> is the directory where you installed the tutorial bundle. </p><hr><a name="wp75440"> </a><p class="pBody">In the <code class="cCode">myuddiping</code> directory, you will find two files and the <code class="cCode">src</code> directory. The <code class="cCode">src</code> directory contains one source file, <code class="cCode">MyUddiPing.java</code>. </p><a name="wp75454"> </a><p class="pBody">The file <code class="cCode">uddi.properties</code> contains the URL of the destination (a UDDI test registry) and the proxy host and proxy port of the sender. Edit this file to supply the correct proxy host and proxy port if you access the Internet from behind a firewall. If you are not sure what the values for these are, consult your system administrator or another person with that information. The typical value of the proxy port is 8080.</p><a name="wp68505"> </a><p class="pBody">The file <code class="cCode">build.xml</code> is the <code class="cCode">asant</code> build file for this example. It includes the file <<code class="cVariable">INSTALL</code>><code class="cCode">/</code>j2eetutorial14<code class="cCode">/examples/saaj/common/targets.xml</code>, which contains a set of targets common to all the SAAJ examples.</p><a name="wp68552"> </a><p class="pBody">The <code class="cCode">prepare</code> target creates a directory named <code class="cCode">build</code>. To invoke the <code class="cCode">prepare</code> target, you type the following at the command line:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">asant prepare<a name="wp68553"> </a></pre></div><a name="wp65536"> </a><p class="pBody">The target named <code class="cCode">build</code> compiles the source file <code class="cCode">MyUddiPing.java</code> and puts the resulting <code class="cCode">.class</code> file in the <code class="cCode">build</code> directory. So to do these tasks, you type the following at the command line:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">asant build<a name="wp68559"> </a></pre></div><a name="wp65226"> </a><h4 class="pHeading3">Examining MyUddiPing</h4><a name="wp68468"> </a><p class="pBody">We will go through the file <code class="cCode">MyUddiPing.java</code> a few lines at a time, concentrating on the last section. This is the part of the application that accesses only the content you want from the XML message returned by the UDDI registry.</p><a name="wp65227"> </a><p class="pBody">The first few lines of code import the packages used in the application.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">import javax.xml.soap.*;import java.net.*;import java.util.*;import java.io.*;<a name="wp79168"> </a></pre></div><a name="wp79028"> </a><p class="pBody">The next few lines begin the definition of the class <code class="cCode">MyUddiPing</code>, which starts with the definition of its <code class="cCode">main</code> method. The first thing it does is check to see if two arguments were supplied. If not, it prints a usage message and exits. The usage message mentions only one argument; the other is supplied by the <code class="cCode">build.xml</code> target.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public class MyUddiPing { public static void main(String[] args) { try { if (args.length != 2) { System.err.println("Argument required: " + "-Dbusiness-name=<name>"); System.exit(1); }<a name="wp79042"> </a></pre></div><a name="wp79030"> </a><p class="pBody">The following lines create a <code class="cCode">java.util.Properties</code> object that contains the system properties and the properties from the file <code class="cCode">uddi.properties</code> that is in the <code class="cCode">myuddiping</code> directory.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"> Properties myprops = new Properties(); myprops.load(new FileInputStream(args[0])); Properties props = System.getProperties(); Enumeration enum = myprops.propertyNames(); while (enum.hasMoreElements()) { String s = (String)enum.nextElement();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -