相关下载:
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.修改系统环境变量classpath,加入c:/soaptest/classes/test.jar
6.启动tomca4.0.4,方法有两个:a.执行c:/program files/apache tomcat 4.0/bin/startup.ba;b.通过系统管理的服务管理开启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);
//设置方法调用的参数
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服务了!
参考书籍: javaweb服务 应用开发详解
新闻热点
疑难解答