夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks

              下面介紹以JWS的方式在TOMCAT上的部署WEBSERVICE。
              前言:
              相信有越來越多的程序員將接觸WEBSERVICE這一新的領域。但是,總感覺很迷茫。買本書來看看,介紹的好象都是XML的知識,什么WSDL等等。搞的我們云里霧里的。那么,我們不仿自己來試試看,自己寫一些WEBSERVICE程序,看看它的運行結果是怎么樣子的。本文檔就是介紹怎么樣在TOMCAT上部署簡單的WEBSERVICE服務,客戶端又是怎么樣去訪問這個服務,并得到自己想要的信息的。
              AXIS是Apache SOAP的第三代產品。
              JWS 文件是語法正確的 Java。但 JWS 文件還具有一些特性,即可利用 WebLogic Workshop 的 Web Service 強大功能。

              下面介紹一下以JWS的方式在TOMCAT上的部署WEBSERVICE。
              本文檔的適用對象:
              本文檔適合于WEBSERVICE的初學者。但是,對TOMCAT有點了解,至少知道怎么啟動TOMCAT。
              關于該文檔的約定:
              TOMCAT.DIR   表示TOMCAT的安裝路徑。
              webapps.dir  表示tomcat.dir/webapps路徑。
              axis.dir     表示tomcat.dir/webapps/axis
              步驟:
              一、下載并安裝axis。
              下載地址:http://xml.apache.org。下載并解壓。把AXIS目錄拷貝到webapps.dir目錄下。一般,axis.dir的目錄結構如下所示:
              axis
              --web-inf
              ----attachments
              ----classes
              ----jwsClasses
              ----lib
              在axis.dir/web-inf目錄下還有一個重要的文件server-config.wsdd。安裝好AXIS之后,運行TOMCAT,即啟動服務。啟動完成之后,我門就可以使用我門的瀏覽器訪問我門的服務。在地址欄里輸入http://localhost:8080/axis,這時我們就可以看到Apache-AXIS的歡迎界面。假如,沒有看到這個歡迎界面,那么,安裝AXIS是失敗的。

              二、寫服務端代碼:
           MyService.java:
          public class MyService {
              public String sayHello(String name,boolean isMan) {
                  if(isMan) {
                      return "Hello,Mr "+name+"! Welcome to Webservice";
                  } else {
                      return "Hello,Miss "+name+"! Welcome to Webservice";
                  }
              }
          }
              注意:不要將MyService.java放在一個PACKAGE里面。

              三、寫客戶端程序
          package com.unimas.datacollection.webservices.client;
          import org.apache.axis.client.Call;
          import org.apache.axis.client.Service;
          import org.apache.axis.encoding.XMLType;

          import javax.xml.namespace.QName;
          import javax.xml.rpc.ParameterMode;

          public class Client
          {
              public static void main(String [] args)
              {
                  try {
                      String endpoint="http://localhost:8080/axis/MyService.jws";

                      Service  service = new Service();
                      Call     call    = (Call) service.createCall();

                      call.setTargetEndpointAddress( new java.net.URL(endpoint) );
                      call.setOperationName( new QName("MyService", "sayHello") );
                      call.addParameter( "arg1", XMLType.XSD_STRING, ParameterMode.IN);
                      call.addParameter( "arg2", XMLType.XSD_BOOLEAN, ParameterMode.IN);
                      call.setReturnClass(String.class);

                      String man = (String) call.invoke( new Object[] { "syc",new Boolean(true) } );
                      String women = (String) call.invoke( new Object[] { "yll",new Boolean(false) } );
                      System.out.println("if is a man you will see:"+man);
                      System.out.println("if is a women you will see:"+women);
                  } catch (Exception e) {
                      e.printStackTrace();
                      System.err.println(e.toString());
                  }
              }
          }
              假如用addParameter方法來設置參數的話,必須使用setReturnType來設置返回類型。與之相等效的方法為setReturnClass。
              還有一種客戶端程序的寫法,如下所示:
          package com.unimas.datacollection.webservices.client;

          import org.apache.axis.client.Service;
          import org.apache.axis.client.Call;

          import javax.xml.namespace.QName;

          public class AnothClient {
             public static void main(String [] args) {
                 try {
                     String endpoint="http://localhost:8080/axis/MyService.jws";

                     Service  service = new Service();
                     Call     call    = (Call) service.createCall();

                     call.setTargetEndpointAddress( new java.net.URL(endpoint) );
                     QName qname = new QName("sayHello");
                     call.setOperationName(qname);

                      String man = (String) call.invoke( new Object[] { "syc",new Boolean(true) } );
                      String women = (String) call.invoke( new Object[] { "yll",new Boolean(false) } );
                      System.out.println("if is a man you will see:"+man);
                      System.out.println("if is a women you will see:"+women);
                 } catch (Exception e) {
                     System.err.println(e.toString());
                     e.printStackTrace();
                 }
             }
          }
              這兩中寫法,所達到的效果是一樣的。
              四:部署
              將MyService.java拷貝到axis.dir目錄下面。并將起重命名為MyService.jws。這個時候,我門就把我們的服務器端部署好了,啟動我們的服務器(執行startup.bat)。
              五、運行客戶端程序:
                  1、運行客戶端程序Client。你將看到如下結果:
           if is a man you will see:Hello,Mr syc! Welcome to Webservice
           if is a women you will see:Hello,Miss yll! Welcome to Webservice
                 2、運行客戶端程序AnothClient。你將看到如下結果:
           if is a man you will see:Hello,Mr syc! Welcome to Webservice
           if is a women you will see:Hello,Miss yll! Welcome to Webservice
              總結:以上就是一個簡單的WEBSERVICE部署的全過成。非常簡單是不?但是,這里有個致命的確定,就是服務器程序不能放在一個包里面,也就是說不能使用PACKATE。優點就是簡單實用。
          參考資料:
              Axis入門:www.csdn.net
              Axis Documentation:  http://ws.apache.org/axis/java/index.html
              The Java Web Services Tutorial http://java.sun.com/webservices/docs/1.1/tutorial/doc/index.html

          posted on 2008-03-12 15:42 HUIKK 閱讀(6828) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 灌南县| 山丹县| 华蓥市| 梁山县| 吴江市| 德安县| 镇赉县| 宜良县| 双江| 新昌县| 荣成市| 福贡县| 鄂伦春自治旗| 宁国市| 宝鸡市| 商洛市| 尚志市| 花垣县| 永川市| 南岸区| 新兴县| 错那县| 高要市| 夏津县| 卢湾区| 永修县| 克东县| 宜君县| 林州市| 清河县| 临澧县| 嘉定区| 盐边县| 泰来县| 绵竹市| 琼中| 安陆市| 黔南| 正镶白旗| 湘阴县| 错那县|