不要為了位置,而放棄追求未來。

          位置可以放棄,未來必須努力!
          posts - 6, comments - 3, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          簡單的webservice開發例子

          Posted on 2011-09-28 15:47 賀瑞龍 閱讀(400) 評論(0)  編輯  收藏 所屬分類: 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);  
                         }  
                 }  
          }  


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 平舆县| 邛崃市| 土默特右旗| 新建县| 肇庆市| 蓝山县| 安吉县| 辽阳市| 开化县| 湘潭市| 荔浦县| 仪陇县| 阳东县| 茂名市| 武冈市| 咸宁市| 江陵县| 梨树县| 苏尼特右旗| 祁门县| 永清县| 巫溪县| 高密市| 深州市| 上蔡县| 葫芦岛市| 岐山县| 长岭县| 湖南省| 文登市| 葵青区| 漳平市| 黔东| 余干县| 四会市| 西充县| 电白县| 宁乡县| 乐平市| 麻城市| 诏安县|