相關下載:
apache soap http://xml.apache.org/soap/index.html
tomcat4.0.4 http://jakarta.apache.org/builds/jakarta-tomcat-4.0/release/v4.0.4/
1.安裝apache soap到c盤根目錄,即c:/soap-2_3_1
2.安裝tomcat4.0.4到c:/program files/apache tomcat 4.0
3.復制c:/soap-2_3_1/webapps/soap.war文件到c:/program files/apache tomcat 4.0/webapps目錄下
4.建立測試目錄c:/soaptest和c:/soaptest/classes
5.修改系統(tǒng)環(huán)境變量classpath,加入c:/soaptest/classes/test.jar
6.啟動tomca4.0.4,方法有兩個:a.執(zhí)行c:/program files/apache tomcat 4.0/bin/startup.ba;b.通過系統(tǒng)管理的服務管理開啟apache tomcat4.0.4
7.建立測試java源文件,源文件應放置在目錄c:/soaptest下
//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);
//設置方法調用的參數(shù)
vector params = new vector ();
params.addelement (new parameter("input", double.class, new double (value), null));
call.setparams (params);
//發(fā)送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服務的發(fā)布
使用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
確認發(fā)布,可以點list查看已發(fā)布服務!
10.運行測試程序
a. cd c:/soaptest
b. java test.simplemathclien
應該可以看到返回的數(shù)值,則證明配置和測試成功,以后就可以發(fā)布自己的soap服務了!
參考書籍: javaweb服務 應用開發(fā)詳解
新聞熱點
疑難解答