//SimpleMath.java package test; public class SimpleMath { public double getSinValue(double input) { double ret = Math.sin(input); return ret; } } ---第二個JAVA源文件---------------- //SimpleMathClIEnt.java package test; import java.io.*; import Java.Net.*; import Java.util.*; import org.apache.soap.util.XML.*; import org.apache.soap.*; import org.apache.soap.rpc.*; public class SimpleMathClient { public static void main(String[] args) throws Exception { System.err.println("SOAP call testing"); double value = Math.random(); SimpleMathClient smc = new SimpleMathClIEnt(); //調用遠程的SOAP服務 double returnValue = smc.doRequest(value); System.err.println("the sin value of " value "is: " returnValue); } public double doRequest(double value) throws Exception { // Build the call. Call call = new Call (); //設置遠程對象的URI call.setTargetObjectURI ("urn:test.math.sin"); //設置調用的方法名 call.setMethodName ("getSinValue"); //設置編碼風格 call.setEncodingStyleURI (Constants.NS_URI_SOAP_ENC); //設置方法調用的參數 Vector params = new Vector (); params.addElement (new Parameter ("input", double.class, new Double (value), null)); call.setParams (params); //發送RPC請求 Response resp = call.invoke (new URL("http://127.0.0.1:8080 /soap/servlet/rpcrouter"),""); if (resp.generatedFault ()) { //遠程調用出錯處理 Fault fault = resp.getFault (); System.out.println ("the call failed: "); System.out.println (" Fault Code = " fault.getFaultCode ()); System.out.println (" Fault String = " fault.getFaultString ()); return 0.0d; } else { //調用成功,獲取返回值 Parameter result = resp.getReturnValue (); return ((Double)result.getValue()).doubleValue(); } } }
8、編譯Java源文件,並打包:
軟件開發網
a. cd c:\soaptest b. javac -d classes *.Java c. cd classes d. jar -cvf test.jar test 9、SOAP服務的發布
使用IE浏覽器浏覽http://localhost:8080/soap/admin/index.Html,進入Deploy,
填寫如下信息: 軟件開發網
ID: urn:test.math.sin Scope: Request Methods getSinValue Provider Type: Java Java Provider Provider Class: test.SimpleMath Java Provider Static: NO 軟件開發網 確認發布,可以點List查看已發布服務。
10、運行測試程序:
a. cd c:\soaptest b. Java test.SimpleMathClIEn 應該可以看到返回的數值,則證明配置和測試成功,以後就可以發布自己的SOAP服務了。