国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

簡單的webservice開發例子

2024-07-21 02:15:11
字體:
來源:轉載
供稿:網友
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);  
               }  
       }  
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 靖州| 灵璧县| 甘南县| 德兴市| 奎屯市| 息烽县| 哈密市| 上思县| 宝应县| 台东市| 河津市| 扶余县| 芒康县| 夏邑县| 阿拉尔市| 黄大仙区| 红河县| 马公市| 伊宁市| 吴忠市| 北流市| 茌平县| 大悟县| 海阳市| 屏南县| 曲阜市| 芒康县| 都昌县| 富阳市| 大渡口区| 黄冈市| 凉城县| 梓潼县| 宁化县| 尉犁县| 安福县| 天台县| 南平市| 无棣县| 前郭尔| 德令哈市|