欢迎光临
我们一直在努力

简单的webservice开发例子-.NET教程,Web Service开发

建站超值云服务器,限时71元/月

 

作者:wjhdtx

摘自csdn

简单的webservice开发例子

axis支持三种web  service的部署和开发,分别为:  
 
1、dynamic  invocation  interface  (  dii)  
 
2、stubs方式  
 
3、dynamic  proxy方式  
 
二、编写dii(dynamic  invocation  interface  )方式web服务  
 
1.编写服务端程序helloclient  
 
 
public  class  helloclient  
{  
       public  string  getname(string  name)  
       {  
               return  “hello  “+name;  
       }  
}  
   
 
 
 
2、将源码拷贝到axis_home下,重命名为  helloclient.jws    
 
3、访问连接http://localhost:8080/axis/helloclient.jws?wsdl,页面显示axis自动生成的wsdl    
 
4、编写访问服务的客户端  testhelloclient.java    
 
 
import  org.apache.axis.client.call;  
import  org.apache.axis.client.service;  
import  javax.xml.namespace.qname;  
import  javax.xml.rpc.serviceexception;  
import  java.net.malformedurlexception;  
import  java.rmi.remoteexception;  
 
public  class  sayhelloclient2  
{  
       public  static  void  main(string[]  args)  
           {  
               try  
                       {  
   string  endpoint  =  
   “http://localhost:8080/axis/helloclient.jws”;  
 
       service  service  =  new  service();  
                       call  call  =  null;  
 
                       call  =  (call)  service.createcall();  
 
                       call.setoperationname(new  qname(  
                             “http://localhost:8080/axis/helloclient.jws”,    
                                         “getname”));  
                       call.settargetendpointaddress  
                                   (new  java.net.url(endpoint));  
 
                       string  ret  =  (string)  call.invoke(new  object[]    
                                   {“zhangsan”});  
                       system.out.println(“return  value  is  ”  +  ret);  
               }    
                       catch  (exception  ex)  
                       {  
             ex.printstacktrace();  
               }  
       }  
}  
   
 
 
 
三、编写dynamic  proxy方式访问服务    
 
1、编写部署服务端程序,同上边dii方式,本次仍使用上边部署的helloclient    
 
2、编写代理接口    
 
 
public  interface  helloclientinterface  
extends  java.rmi.remote  
{  
       public  string  getname(string  name)  
           throws  java.rmi.remoteexception;  
}  
   
 
 
 
3、编写并执行客户端程序testhelloclient.java    
 
 
import  javax.xml.rpc.service;  
import  javax.xml.rpc.servicefactory;  
import  java.net.url;  
import  javax.xml.namespace.qname;  
 
public  class  testhelloclient    
{  
       public  static  void  main(string[]  args)  
           {  
               try  
               {  
                       string  wsdlurl  =    
                                   “http://localhost:8080/axis/helloclient.jws?wsdl”;  
                       string  namespaceuri  =    
                                   “http://localhost:8080/axis/helloclient.jws”;  
                       string  servicename  =  “helloclientservice”;  
                       string  portname  =  “helloclient”;  
 
                       servicefactory  servicefactory  =    
                                   servicefactory.newinstance();  
                       service  afservice  =  
                                   servicefactory.createservice(new  url(wsdlurl),  
                 new  qname(namespaceuri,  servicename));  
                       helloclientinterface  proxy  =  (helloclientinterface)  
               afservice.getport(new  qname(  
                                       namespaceuri,  portname),    
                                                           helloclientinterface.class);  
                       system.out.println  
                                   (“return  value  is  “+proxy.getname(“john”)  )  ;  
               }catch(exception  ex)  
               {  
                       ex.printstacktrace()  ;  
               }  
       }  
}  
   
 
 
 
四、编写wsdd发布web服务,编写stub  client访问web服务    
 
1、编写服务端程序server,sayhello.java,编译server.sayhello.java    
 
 
package  server;  
public  class  sayhello  
{  
       public  string  getname(string  name)  
       {  
               return  “hello  “+name;  
       }  
}  
2.编写loghandler.java  
import  org.apache.axis.axisfault;  
import  org.apache.axis.handler;  
import  org.apache.axis.messagecontext;  
import  org.apache.axis.handlers.basichandler;  
 
import  java.util.date;  
 
public  class  loghandler    
extends  basichandler    
{  
 public  void  invoke  
(messagecontext  msgcontext)    
throws  axisfault  
       {  
               /**  log  an  access  each  time    
                       we  get  invoked.  
                 */  
               try  {  
                       handler  servicehandler  
                                   =  msgcontext.getservice();  
 
                       integer  numaccesses  =  
   (integer)servicehandler.getoption(“accesses”);  
                       if  (numaccesses  ==  null)  
                               numaccesses  =  new  integer(0);  
numaccesses  =  new  integer  
(numaccesses.intvalue()  +  1);  
date  date  =  new  date();  
 string  result  =    
 date  +  “:  service  ”  +  
msgcontext.gettargetservice()  +  
”  accessed  ”  +  numaccesses  +  ”  time(s).”;  
servicehandler.setoption  
(“accesses”,  numaccesses);  
system.out.println(result);  
               }  catch  (exception  e)  
                       {  
                       throw  axisfault.makefault(e);  
               }  
       }  
}  
   
 
 
 
3、编写wsdd文件    
 
 
deploy.wsdd  
<deployment  xmlns=  
“http://xml.apache.org/axis/wsdd/”  
   xmlns:java=  
                                   “http://xml.apache.org/axis/wsdd/providers/java”>                        
     <handler  name=”print”  type=”java:loghandler”/>    
 <service  name=”sayhello”  
 provider=”java:rpc”>  
     <requestflow>  
         <handler  type=”print”/>  
     </requestflow>  
   <parameter  name=”classname”    
   value=”server.sayhello”/>  
   <parameter  name=”allowedmethods”  
   value=”*”/>      
 </service>  
</deployment>  
   
 
 
 
3、将编译后的文件拷贝到axis_home/web-inf/classes下,如:d:\tomcat\webapps\axis\web-inf\classes    
 
4、发布服务:    
 
java  org.apache.axis.client.adminclient  deploy.wsdd    
 
5、生成client  stub文件    
 
a:方式1    
 
将sayhello.java拷贝到axis_home/下,重命名为sayhello.jws,    
 
执行下面的命令生存client  stub    
 
 
java  org.apache.axis.wsdl.wsdl2java    
-p  client    http://localhost:8080  
/axis/services/sayhello.jws?wsdl  
   
 
 
 
b:方式2    
 
执行如下命令生成sayhello.wsdl    
 
 
java  org.apache.axis.wsdl.java2wsdl  
-osayhello.wsdl  -lhttp://localhost:8080  
/axis/services/sayhello  -nsayhello  server.sayhello  
   
 
 
 
执行如下命令生成client  stub    
 
 
java  org.apache.axis.wsdl.wsdl2java    
sayhello.wsdl    -p  client  
   
 
 
 
生成的stub  client文件列表为:    
 
1.sayhello.java    
 
2.sayhelloservice.java。    
 
3.sayhelloservicelocator.java    
 
4.sayhellosoapbindingstub.java    
 
6、编写客户端程序,编译并执行    
 
 
public  class  sayhelloclient  
{  
       public  static  void  main(string[]  args)  
           {  
               try  
                       {  
       sayhelloservice  service  =  new  client.  
             sayhelloservicelocator();  
                   client.sayhello_porttype    
                           client  =  service.getsayhello();  
                       string  retvalue=client.getname(“zhangsan”);  
                       system.out.println(retvalue);  
}    
catch  (exception  e)  
{  
 system.err.println  
 (“execution  failed.  exception:  ”  +  e);  
               }  
       }  
}  

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 简单的webservice开发例子-.NET教程,Web Service开发
分享到: 更多 (0)