材料:java sdk、Tomcat、mail.jar、soap.jar、activation.jar、xerces.jar、soap.war
安装java sdk,
环境变量:
变量:JAVAHOME=C:\Program Files\Java\jdk1.6.0_22
path=%JAVAHOME%\bin;%JAVAHOME%\jre\bin;
classpath=.;%JAVAHOME%\lib\dt.jar;%JAVAHOME%\lib\tools.jar;
Path使得系统可以在任何路径下识别java命令,编译相关!
classpath只跟java运行有关,跟编译无关,
?java 中的tools.jar和dt.jar分别是干什么的 ?
rt.jar是JAVA基础类库,dt.jar是关于运行环境的类库,tools.jar是工具类库
设置在classpath里是为了让你 import *
rt.jar 默认就在 根classloader的加载路径里面 放在claspath是多此一举
不光rt.jar jre\lib下面的大部分jar 都在这个路径里
tools.jar 是系统用来编译一个类的时候用到的 也就是javac的时候用到
javac XXX.java
实际上就是运行
java -Calsspath=%JAVA_HOME%\lib\tools.jar xx.xxx.Main XXX.java
javac就是对上面命令的封装 所以tools.jar 也不用加到classpath里面
dt.jar是关于运行环境的类库,主要是swing的包 你要用到swing时最好加上
安装Tomcat(不赘述)
将mail.jar、soap.jar、activation.jar、xerces.jar放到tomcat/lib下面,
tomcat运行时会自动加载tomcat/lib下的所有类库
将soap.war放到tomcat/webapps下,重启tomcat。
会看到tomcat已经解压soap.war
到此搭建好了tomcat下的soap协议服务
测试:
输入:http://localhost:8080/soap/servlet/rpcrouter
成功则输出:
Sorry, I don't speak via HTTP GET- you have to use HTTP POST to talk to me.
简单的soap程序
SOAPService.java
1 package HelloWorld;
2 public class SOAPService {
3 public String sayHi(String x) {
4 return("Hello my friend, " + x + "! Glad to see you!");
5 }
6 }
编译SOAPService.java。将目录 HelloWorld/拷贝到tomcat/webapps/soap/WEB-INF/classes/目录下
用SOAP Service Manager来部署
访问http://localhost:8080/soap/admin/点Deploy
填入
ID: | urn:HelloWorld_SOAPService |
Scope: | Application |
Methods: | sayHi |
Provider Type: | java |
Java Provider - Provider Class: | HelloWorld.SOAPService |
Java Provider - Static? | No |
部署成功,点点击 List ,将会看见你的服务urn:HelloWorld_SOAPService已经被部署进来。点击urn:HelloWorld_SOAPService,会出现
'urn:HelloWorld_SOAPService' Service Deployment Descriptor |
|
---|---|
Property | Details |
ID | urn:HelloWorld_SOAPService |
Scope | Application |
Provider Type | java |
Provider Class | HelloWorld.SOAPService |
Use Static Class | false |
Methods | sayHi |
Type Mappings | |
Default Mapping Registry Class |
则说明部署成功,否则检查(填写是否正确、有没有多填上了空格等),
soap客服端:
1 import java.net.*;
2 import java.util.*;
3 import org.apache.soap.*;
4 import org.apache.soap.rpc.*;
5 public class HelloWorldClient {
6 public static void main(String[] arg) throws Exception {
7 Call c = null;
8 URL url = null;
9 Vector params = null;
10 Response rep = null;
11 String ourName = "Superman";
12 String ourUrn = "urn:HelloWorld_SOAPService";
13 String ourMethod = "sayHi";
14 url = new URL("http://localhost:8080/soap/servlet/rpcrouter");
15 System.out.println("Passing to our deployed "+ourUrn+"our name ("+ourName+"): ");
16 c = new Call();
17 c.setTargetObjectURI(ourUrn);
18 c.setMethodName(ourMethod);
19 c.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
20 params = new Vector();
21 params.addElement(new Parameter("ourName", String.class,ourName, null));
22 c.setParams(params);
23 System.out.print("and its answer is: ");
24 rep = c.invoke(url, "");
25 if (rep.generatedFault()) {
26 Fault fault = rep.getFault();
27 System.out.println("\nCall failed!");
28 System.out.println("Code = " + fault.getFaultCode());
29 System.out.println("String = " + fault.getFaultString());
30 } else {
31 Parameter result = rep.getReturnValue();
32 System.out.print(result.getValue());
33 System.out.println();
34 }
35 }
36 }
注:需要引入mail.jar和soap.jar
运行客服端将会出现:
Passing to our deployed urn:HelloWorld_SOAPServiceour name (Superman):
and its answer is: Hello my friend, Superman! Glad to see you!
则整个soap通信圆满完成
参考:http://www.cnblogs.com/mingzi/archive/2009/03/23/1419385.html