通訊錄查詢服務
以一通訊錄查詢服務說明流程:在 Java Client 或 ASP Client 輸入學號,經由 HTTP 至 Java Server 端提出通訊錄查詢服務需求;Java Server 端將查出之通訊資料經由 HTTP 回傳至 Java Client 或 ASP Client。
首先將通訊錄查詢服務建在 Java Axis 引擎上,產生 Java Server 端的 Web Service。
Axis 是以 Java 撰寫而成的 SOAP 引擎,但是功能到超過一個 SOAP Engine 的功能,它 也通過了 JAX-RPC 與 SAAJ 的 Technology Compatibility Test ;所 以 Axis 的功用,就 是可以將 Java 的應用程式透過 Axis 的轉譯,變成 Web Services 中的 WSDL 描述。
現 在 要 先 產 生 一 個 可 以 讓 Client 端 呼 叫 的 WSDL 檔 ( 正 確 說 法 應 該 是 SOAP+WSDL):
<?xml version="1.0" encoding="UTF-8" ?>
wsdl:definitions targetNamespace="http://e.ypu.edu.tw/axis/contact.jws"
xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://e.ypu.edu.tw/axis/contact.jws"
40
xmlns:intf="http://e.ypu.edu.tw/axis/contact.jws"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <!--
WSDL created by Apache Axis version: 1.4 Built on Apr 22, 2006 (06:55:48 PDT)
-->
wsdl:types>
schema targetNamespace="http://e.ypu.edu.tw/axis/contact.jws"
xmlns="http://www.w3.org/2001/XMLSchema">
import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
complexType name="ArrayOf_xsd_string">
complexContent>
restriction base="soapenc:Array">
attribute ref="soapenc:arrayType" wsdl:arrayType="xsd:string[]" />
</restriction>
</complexContent>
</complexType>
</schema>
</wsdl:types>
wsdl:message name="stdQueryRequest">
wsdl:part name="id" type="xsd:string" />
</wsdl:message>
wsdl:message name="getNameRequest" />
wsdl:message name="stdQueryResponse" />
wsdl:message name="getIdResponse">
wsdl:part name="getIdReturn" type="xsd:string" />
</wsdl:message>
wsdl:message name="getArrayRequest" />
wsdl:message name="getEmailRequest" />
wsdl:message name="getCellPhoneResponse">
wsdl:part name="getCellPhoneReturn" type="xsd:string" />
</wsdl:message>
wsdl:message name="getEmailResponse">
wsdl:part name="getEmailReturn" type="xsd:string" />
</wsdl:message>
41
wsdl:message name="getArrayResponse">
wsdl:part name="getArrayReturn" type="impl:ArrayOf_xsd_string" />
</wsdl:message>
wsdl:message name="getCellPhoneRequest" />
wsdl:message name="getNameResponse">
wsdl:part name="getNameReturn" type="xsd:string" />
</wsdl:message>
wsdl:message name="getIdRequest" />
wsdl:portType name="contact">
wsdl:operation name="getName">
wsdl:input message="impl:getNameRequest" name="getNameRequest" />
wsdl:output message="impl:getNameResponse" name="getNameResponse" />
</wsdl:operation>
wsdl:operation name="getArray">
wsdl:input message="impl:getArrayRequest" name="getArrayRequest" />
wsdl:output message="impl:getArrayResponse" name="getArrayResponse" />
</wsdl:operation>
wsdl:operation name="getId">
wsdl:input message="impl:getIdRequest" name="getIdRequest" />
wsdl:output message="impl:getIdResponse" name="getIdResponse" />
</wsdl:operation>
wsdl:operation name="getEmail">
wsdl:input message="impl:getEmailRequest" name="getEmailRequest" />
wsdl:output message="impl:getEmailResponse" name="getEmailResponse" />
</wsdl:operation>
wsdl:operation name="stdQuery" parameterOrder="id">
wsdl:input message="impl:stdQueryRequest" name="stdQueryRequest" />
wsdl:output message="impl:stdQueryResponse" name="stdQueryResponse" />
</wsdl:operation>
wsdl:operation name="getCellPhone">
wsdl:input message="impl:getCellPhoneRequest" name="getCellPhoneRequest" />
wsdl:output message="impl:getCellPhoneResponse" name="getCellPhoneResponse" />
</wsdl:operation>
</wsdl:portType>
wsdl:binding name="contactSoapBinding" type="impl:contact">
wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
wsdl:operation name="getName">
42
wsdlsoap:operation soapAction="" />
wsdl:input name="getNameRequest">
wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://DefaultNamespace" use="encoded" />
</wsdl:input>
wsdl:output name="getNameResponse">
wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://e.ypu.edu.tw/axis/contact.jws" use="encoded" />
</wsdl:output>
</wsdl:operation>
wsdl:operation name="getArray">
wsdlsoap:operation soapAction="" />
wsdl:input name="getArrayRequest">
wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://DefaultNamespace" use="encoded" />
</wsdl:input>
wsdl:output name="getArrayResponse">
wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://e.ypu.edu.tw/axis/contact.jws" use="encoded" />
</wsdl:output>
</wsdl:operation>
wsdl:operation name="getId">
wsdlsoap:operation soapAction="" />
wsdl:input name="getIdRequest">
wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://DefaultNamespace" use="encoded" />
</wsdl:input>
wsdl:output name="getIdResponse">
wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://e.ypu.edu.tw/axis/contact.jws" use="encoded" />
</wsdl:output>
</wsdl:operation>
wsdl:operation name="getEmail">
wsdlsoap:operation soapAction="" />
wsdl:input name="getEmailRequest">
wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://DefaultNamespace" use="encoded" />
43
</wsdl:input>
wsdl:output name="getEmailResponse">
wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://e.ypu.edu.tw/axis/contact.jws" use="encoded" />
</wsdl:output>
</wsdl:operation>
wsdl:operation name="stdQuery">
wsdlsoap:operation soapAction="" />
wsdl:input name="stdQueryRequest">
sdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://DefaultNamespace" use="encoded" />
</wsdl:input>
wsdl:output name="stdQueryResponse">
wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://e.ypu.edu.tw/axis/contact.jws" use="encoded" />
</wsdl:output>
</wsdl:operation>
wsdl:operation name="getCellPhone">
wsdlsoap:operation soapAction="" />
wsdl:input name="getCellPhoneRequest">
wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://DefaultNamespace" use="encoded" />
</wsdl:input>
wsdl:output name="getCellPhoneResponse">
wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://e.ypu.edu.tw/axis/contact.jws" use="encoded" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
wsdl:service name="contactService">
wsdl:port binding="impl:contactSoapBinding" name="contact">
wsdlsoap:address location="http://e.ypu.edu.tw/axis/contact.jws" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
44
接著寫一支可以呼叫 Java Server Web Services 的 ASP 程式,在 ASP 端傳遞一個 學號參數至 Java Server Web Services 端,即可回傳一組通訊資料。
ASP 程式呼叫 Java Server Web Services 的寫法如下:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="950"%>
<%
dim id
id = request.Form("id")
set init = Server.CreateObject("MSSOAP.SoapClient30") init.ClientProperty("ServerHTTPRequest") = True
init.mssoapinit("http://e.ypu.edu.tw/axis/contact.jws?wsdl") init.stdQuery(id)
set aD1 = Server.CreateObject("MSSOAP.SoapClient30") aD1.ClientProperty("ServerHTTPRequest") = True
aD1.mssoapinit("http://e.ypu.edu.tw/axis/contact.jws?wsdl")
set aD2 = Server.CreateObject("MSSOAP.SoapClient30") aD2.ClientProperty("ServerHTTPRequest") = True
aD2.mssoapinit("http://e.ypu.edu.tw/axis/contact.jws?wsdl")
set aD3 = Server.CreateObject("MSSOAP.SoapClient30") aD3.ClientProperty("ServerHTTPRequest") = True
aD3.mssoapinit("http://e.ypu.edu.tw/axis/contact.jws?wsdl")
set aD4 = Server.CreateObject("MSSOAP.SoapClient30") aD4.ClientProperty("ServerHTTPRequest") = True
aD4.mssoapinit("http://e.ypu.edu.tw/axis/contact.jws?wsdl")
%>
也可使用 JSP 程式呼叫 Java Server Web Services 的方式,在 JSP 端傳遞一個學號 參數至 Java Server Web Services 端,同樣也可回傳一組通訊資料。
JSP 程式呼叫 Java Server Web Services 的寫法如下:
45
<%@ page import="javax.xml.namespace.QName,java.util.*,java.util.Date,java.text.*,
java.io.*,org.apache.axis.client.Call,org.apache.axis.client.Service" errorPage="../errorMessage.jsp"
contentType="text/html;charset=MS950"%>
<%
response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
request.setCharacterEncoding("MS950");
String id=request.getParameter("id")==null?"":(String)request.getParameter("id");
String status=request.getParameter("status")==null?"":(String)request.getParameter("status");
String []aa= new String[4];
if(status.equals("query")){
try {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress("http://e.ypu.edu.tw/axis/contact.jws");
call.setOperationName(new QName("contact", "stdQuery"));
Object[] params = {id};
Object obj = call.invoke(params);
call.setOperationName(new QName("contact", "getArray"));
aa = (String[]) call.invoke( new Object[]{});
} catch (Exception e) {};
}
%>
46