作者:疯傻 madfool@163.net
相关下载:
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服务 应用开发详解