总结WebService的使用


一、安装

jsp程序员应该不会装错。首先下载axis,解压缩。将axis目录拷贝到tomcat下webapps下。ok,对就这么简单。

启动tomcat,访问http://localhost:8080/axis 检查安装是否成功

二、部署(三种方式)

(1)、Dynamic Invocation Interface ( DII)

我认为这是其中最简单的方式,也是最不想介绍的方式。

1.编写服务端程序HelloClient
  
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> 1     public   class  HelloClient
2    {
3         public  String getName(String name)
4        {
5             return   " hello  " + name;
6        }
7    }

  
2、将源码拷贝到Axis_HOME下,重命名为 HelloClient.jws
  
3、访问连接http://localhost:8080/axis/HelloClient.jws?wsdl,页面显示Axis自动生成的wsdl
  
4、编写访问服务的客户端 TestHelloClient.java
  
  
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->  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)、Stubs方式

这种方式对自己来说记忆很深刻,做的第一个小项目,调用别人提供的ws就是这种方式。

    1、编写部署服务端程序,同上边DII方式,本次仍使用上边部署的HelloClient
  
  2、编写代理接口
  
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> 1     public   interface  HelloClientInterface
2     extends  java.rmi.Remote
3    {
4        public  String getName(String name)
5        throws  java.rmi.RemoteException;
6    }

  
  3、编写并执行客户端程序TestHelloClient.java
  
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->  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    }


  
(3)、Dynamic Proxy方式

(我比较喜欢用这种方式。)

1. 服务端同样使用一样的测试代码,不过打包成jar,然后放在axis目录web-inf/lib下。
2. 编写server-config.wsdd 放在axis web-inf下。
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->  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 >


测试程序仍然可以是DII方式中的测试程序。
不过附带vbs脚本测试:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> 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 

再附带一个asp连接程序:

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> 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 " )

不过注意,vb 和asp连接应该装 soapsdk.exe 和msxmlchs.msi软件。

你可能感兴趣的:(java,apache,xml,webservice,asp.net)