一、安裝
jsp程序員應(yīng)該不會(huì)裝錯(cuò)。首先下載axis,解壓縮。將axis目錄拷貝到tomcat下webapps下。ok,對(duì)就這么簡(jiǎn)單。
啟動(dòng)tomcat,訪問(wèn)http://localhost:8080/axis 檢查安裝是否成功
二、部署(三種方式)
(1)、Dynamic Invocation Interface ( DII)
我認(rèn)為這是其中最簡(jiǎn)單的方式,也是最不想介紹的方式。
1.編寫(xiě)服務(wù)端程序HelloClient
1 public class HelloClient
2 {
3 public String getName(String name)
4 {
5 return "hello "+name;
6 }
7 }
2 {
3 public String getName(String name)
4 {
5 return "hello "+name;
6 }
7 }
2、將源碼拷貝到Axis_HOME下,重命名為 HelloClient.jws
3、訪問(wèn)連接http://localhost:8080/axis/HelloClient.jws?wsdl,頁(yè)面顯示Axis自動(dòng)生成的wsdl
4、編寫(xiě)訪問(wèn)服務(wù)的客戶端 TestHelloClient.java
1 import org.apache.Axis.client.Call;
2 import org.apache.Axis.client.Service;
3 import javax.xml.namespace.QName;
4 import javax.xml.rpc.ServiceException;
5 import java.net.MalformedURLException;
6 import java.rmi.RemoteException;
7
8 public class SayHelloClient2
9 {
10 public static void main(String[] args)
11 {
12 try
13 {
14 String endpoint =
15 "http://localhost:8080/Axis/HelloClient.jws";
16
17 Service service = new Service();
18 Call call = null;
19
20 call = (Call) service.createCall();
21
22 call.setOperationName(new QName(
23 "http://localhost:8080/Axis/HelloClient.jws",
24 "getName"));
25 call.setTargetEndpointAddress
26 (new java.net.URL(endpoint));
27
28 String ret = (String) call.invoke(new Object[]
29 {"zhangsan"});
30 System.out.println("return value is " + ret);
31 }
32 catch (Exception ex)
33 {
34 ex.printStackTrace();
35 }
36 }
37 }
2 import org.apache.Axis.client.Service;
3 import javax.xml.namespace.QName;
4 import javax.xml.rpc.ServiceException;
5 import java.net.MalformedURLException;
6 import java.rmi.RemoteException;
7
8 public class SayHelloClient2
9 {
10 public static void main(String[] args)
11 {
12 try
13 {
14 String endpoint =
15 "http://localhost:8080/Axis/HelloClient.jws";
16
17 Service service = new Service();
18 Call call = null;
19
20 call = (Call) service.createCall();
21
22 call.setOperationName(new QName(
23 "http://localhost:8080/Axis/HelloClient.jws",
24 "getName"));
25 call.setTargetEndpointAddress
26 (new java.net.URL(endpoint));
27
28 String ret = (String) call.invoke(new Object[]
29 {"zhangsan"});
30 System.out.println("return value is " + ret);
31 }
32 catch (Exception ex)
33 {
34 ex.printStackTrace();
35 }
36 }
37 }
(2)、Stubs方式
這種方式對(duì)自己來(lái)說(shuō)記憶很深刻,做的第一個(gè)小項(xiàng)目,調(diào)用別人提供的ws就是這種方式。
1、編寫(xiě)部署服務(wù)端程序,同上邊DII方式,本次仍使用上邊部署的HelloClient
2、編寫(xiě)代理接口
1 public interface HelloClientInterface
2 extends java.rmi.Remote
3 {
4 public String getName(String name)
5 throws java.rmi.RemoteException;
6 }
2 extends java.rmi.Remote
3 {
4 public String getName(String name)
5 throws java.rmi.RemoteException;
6 }
3、編寫(xiě)并執(zhí)行客戶端程序TestHelloClient.java
1
2 import javax.xml.rpc.Service;
3 import javax.xml.rpc.ServiceFactory;
4 import java.net.URL;
5 import javax.xml.namespace.QName;
6
7 public class TestHelloClient
8 {
9 public static void main(String[] args)
10 {
11 try
12 {
13 String wsdlUrl =
14 "http://localhost:8080/Axis/HelloClient.jws?wsdl";
15 String nameSpaceUri =
16 "http://localhost:8080/Axis/HelloClient.jws";
17 String serviceName = "HelloClientService";
18 String portName = "HelloClient";
19
20 ServiceFactory serviceFactory =
21 ServiceFactory.newInstance();
22 Service afService =
23 serviceFactory.createService(new URL(wsdlUrl),
24 new QName(nameSpaceUri, serviceName));
25 HelloClientInterface proxy = (HelloClientInterface)
26 afService.getPort(new QName(
27 nameSpaceUri, portName),
28 HelloClientInterface.class);
29 System.out.println
30 ("return value is "+proxy.getName("john") ) ;
31 }catch(Exception ex)
32 {
33 ex.printStackTrace() ;
34 }
35 }
36 }
2 import javax.xml.rpc.Service;
3 import javax.xml.rpc.ServiceFactory;
4 import java.net.URL;
5 import javax.xml.namespace.QName;
6
7 public class TestHelloClient
8 {
9 public static void main(String[] args)
10 {
11 try
12 {
13 String wsdlUrl =
14 "http://localhost:8080/Axis/HelloClient.jws?wsdl";
15 String nameSpaceUri =
16 "http://localhost:8080/Axis/HelloClient.jws";
17 String serviceName = "HelloClientService";
18 String portName = "HelloClient";
19
20 ServiceFactory serviceFactory =
21 ServiceFactory.newInstance();
22 Service afService =
23 serviceFactory.createService(new URL(wsdlUrl),
24 new QName(nameSpaceUri, serviceName));
25 HelloClientInterface proxy = (HelloClientInterface)
26 afService.getPort(new QName(
27 nameSpaceUri, portName),
28 HelloClientInterface.class);
29 System.out.println
30 ("return value is "+proxy.getName("john") ) ;
31 }catch(Exception ex)
32 {
33 ex.printStackTrace() ;
34 }
35 }
36 }
(3)、Dynamic Proxy方式
(我比較喜歡用這種方式。)
1. 服務(wù)端同樣使用一樣的測(cè)試代碼,不過(guò)打包成jar,然后放在axis目錄web-inf/lib下。
2. 編寫(xiě)server-config.wsdd 放在axis web-inf下。
1
2 <?xml version="1.0" encoding="UTF-8"?>
3 <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
4 <globalConfiguration>
5 <parameter name="adminPassword" value="admin"/>
6 <parameter name="enableNamespacePrefixOptimization" value="true"/>
7 <parameter name="disablePrettyXML" value="true"/>
8 <parameter name="sendXsiTypes" value="true"/>
9 <parameter name="sendMultiRefs" value="true"/>
10 <parameter name="sendXMLDeclaration" value="true"/>
11 <requestFlow name="RequestFlow1">
12 <handler name="Handler1" type="java:org.apache.axis.handlers.JWSHandler">
13 <parameter name="scope" value="application"/>
14 </handler>
15 <handler name="Handler2" type="java:org.apache.axis.handlers.JWSHandler">
16 <parameter name="scope" value="request"/>
17 <parameter name="extension" value=".jwr"/>
18 </handler>
19 </requestFlow>
20 </globalConfiguration>
21 <responseFlow name="ResponseFlow1">
22 <handler name="Handler1" type="LocalResponder"/>
23 </responseFlow>
24 <handler name="Handler1" type="LocalResponder"/>
25 <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
26 <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
27 <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
28 <handler name="Handler2" type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
29 <requestFlow name="RequestFlow1">
30 <handler name="Handler1" type="URLMapper"/>
31 <handler name="Handler2" type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
32 </requestFlow>
33
34 <service name="TESTWS" style="wrapped">
35 <parameter name="allowedMethods" value="*"/>
36 <parameter name="scope" value="Application"/>
37 <parameter name="className" value="HelloClient"/>
38 </service>
39
40 <transport name="http">
41 <requestFlow name="RequestFlow1">
42 <handler name="Handler1" type="URLMapper"/>
43 <handler name="Handler2" type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
44 </requestFlow>
45 </transport>
46 <transport name="local">
47 <responseFlow name="ResponseFlow1">
48 <handler name="Handler1" type="LocalResponder"/>
49 </responseFlow>
50 </transport>
51 </deployment>
2 <?xml version="1.0" encoding="UTF-8"?>
3 <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
4 <globalConfiguration>
5 <parameter name="adminPassword" value="admin"/>
6 <parameter name="enableNamespacePrefixOptimization" value="true"/>
7 <parameter name="disablePrettyXML" value="true"/>
8 <parameter name="sendXsiTypes" value="true"/>
9 <parameter name="sendMultiRefs" value="true"/>
10 <parameter name="sendXMLDeclaration" value="true"/>
11 <requestFlow name="RequestFlow1">
12 <handler name="Handler1" type="java:org.apache.axis.handlers.JWSHandler">
13 <parameter name="scope" value="application"/>
14 </handler>
15 <handler name="Handler2" type="java:org.apache.axis.handlers.JWSHandler">
16 <parameter name="scope" value="request"/>
17 <parameter name="extension" value=".jwr"/>
18 </handler>
19 </requestFlow>
20 </globalConfiguration>
21 <responseFlow name="ResponseFlow1">
22 <handler name="Handler1" type="LocalResponder"/>
23 </responseFlow>
24 <handler name="Handler1" type="LocalResponder"/>
25 <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
26 <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
27 <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
28 <handler name="Handler2" type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
29 <requestFlow name="RequestFlow1">
30 <handler name="Handler1" type="URLMapper"/>
31 <handler name="Handler2" type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
32 </requestFlow>
33
34 <service name="TESTWS" style="wrapped">
35 <parameter name="allowedMethods" value="*"/>
36 <parameter name="scope" value="Application"/>
37 <parameter name="className" value="HelloClient"/>
38 </service>
39
40 <transport name="http">
41 <requestFlow name="RequestFlow1">
42 <handler name="Handler1" type="URLMapper"/>
43 <handler name="Handler2" type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
44 </requestFlow>
45 </transport>
46 <transport name="local">
47 <responseFlow name="ResponseFlow1">
48 <handler name="Handler1" type="LocalResponder"/>
49 </responseFlow>
50 </transport>
51 </deployment>
測(cè)試程序仍然可以是DII方式中的測(cè)試程序。
不過(guò)附帶vbs腳本測(cè)試:
1
2 Set SOAPClient = CreateObject("MSSOAP.SOAPClient30")
3 SOAPClient.MSSOAPInit("http://127.0.0.1:8080/axis/services/TESTWS?wsdl")
4 WScript.Echo SOAPClient.getName("devTest")
5
2 Set SOAPClient = CreateObject("MSSOAP.SOAPClient30")
3 SOAPClient.MSSOAPInit("http://127.0.0.1:8080/axis/services/TESTWS?wsdl")
4 WScript.Echo SOAPClient.getName("devTest")
5
再附帶一個(gè)asp連接程序:
1 Set SOAPClient = server.CreateObject("MSSOAP.SOAPClient30")
2 SOAPClient.ClientProperty("ServerHTTPRequest") = True
3 SOAPClient.MSSOAPInit("http://127.0.0.1:8080/axis/services/TESTWS?wsdl")
4 Dim wsrequest=SOAPClient.getName("zhangsan")
2 SOAPClient.ClientProperty("ServerHTTPRequest") = True
3 SOAPClient.MSSOAPInit("http://127.0.0.1:8080/axis/services/TESTWS?wsdl")
4 Dim wsrequest=SOAPClient.getName("zhangsan")
不過(guò)注意,vb 和asp連接應(yīng)該裝 soapsdk.exe 和msxmlchs.msi軟件。