天天看點

Web Services執行個體

 Web Services

     Web Service是基于網絡的、分布式的子產品化元件,它執行特定的任務,遵守具體的技術規範,這些規範使得Web Service能與其他相容的元件進行互操作。Internet Inter-Orb Protocol(IIOP)都已經釋出了很長時間了,但是這些模型都依賴于特殊對象模型協定,而 Web Services 利用 SOAP 和 XML對這些模型在通訊方面作了進一步的擴充以消除特殊對象模型的障礙。Web Services 主要利用 HTTP 和 SOAP 協定使商業資料在 Web 上傳輸,SOAP通過 HTTP 調用商業對象執行遠端功能調用,Web 使用者能夠使用 SOAP 和 HTTP通過 Web 調用的方法來調用遠端對象.

    Axis是一套實作了Web services的标準,參考http://ws.apache.org/axis/。 下載下傳位址:http://ws.apache.org/axis/java/releases.html

1、伺服器端步驟

 将axis-bin-1_4.zip檔案解壓,将解壓後的webapps下面的axis考貝到tomcat中的TOMCAT-HOME/webapps下 

在TOMCAT-HOME/webapps/axis下建立MyMath.jws檔案,内容如下: 

public class MyMath {

public int squared(int x) {

int result = x * x;

System.out.println("the squared of " + x + " is " + result);

return result;

}

啟動Tomcat,通路如下http://localhost:8080/axis/MyMath.jws,如果可以正常通路Web Service部署成功!!

2、用戶端調用

在Eclipse中建立java項目,引入Axis中的所有的包。建立MyMathClient.java檔案,編寫調用web service的用戶端代碼:  

public class MyMathClient {

   private static final String endPoint = "http://localhost:8080/axis/MyMath.jws?wsdl";

    public static void main(String args[]){

        Service service = new Service();

        try {

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

            call.setTargetEndpointAddress(new URL(endPoint));

            Integer result =(Integer)call.invoke("squared", new Object[]{10}); 

            System.out.println(result);

} catch (ServiceException e) {

            e.printStackTrace();

        } catch (MalformedURLException e) {

        }catch(RemoteException e){

        }

    }

}

 3、調用外部開放的web services

http://www.ayandy.com/

繼續閱讀