To invoke Web Service in .htm file

2008-02-23 09:32:33来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

With some ActiveXObject, we can invoke Web service in the pure html, also need help of VB Script or JavaScript.

Here, i want to use Javascript to show an example:

The first part a to send the SOAP message:

<script language="javascript1.2">

//creat a Object to send SOAP
var XMLhttp = new ActiveXObject("Msxml2.XMLHTTP");

//the function used to send request
function sendRequest(){

//also can use other services
var url = "http://localhost:8080/axis/services/TestService";

var SoapRequest="<?xml version=\"1.0\" encoding=\"utf-8\"?>"

//add required nameSpace
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" "
"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>"
" <q1:test xmlns:q1='"somenamespace">"

//you can add the SOAP message manually here
//you also can use Script to get the input string from form, like document.form1.content.value

" </q1:test>"
"</soap:Body>"
"</soap:Envelope>";

//ready to send SOAP,need a little change
xmlhttp.Open("POST",url,false);
xmlhttp.setRequestHeader("Content-Type", "text/xml;charset=utf-8");
xmlhttp.setRequestHeader("HOST","127.0.0.1");
xmlhttp.setRequestHeader("Content-Length",SoapRequest.length);
xmlhttp.setRequestHeader("SOAPAction", "someNamespace");
xmlhttp.Send(SoapRequest);

//If the SOAP has been successfully
alert("ok");
}

//the function for receive the SOAP
function receiveResponse(){

//the SOAP has been sent , the status will set to 200
if(xmlhttp.status == 200){

//create a Object to solve XML
var xmlDOC = new ActiveXObject("MSXML.DOMDocument");
xmlDOC.load(xmlhttp.responseXML);
xmlStr = xmlDOC.xml;

//here i just to print the SOAP content, you can read the XML with the MSXML.DOMDocument
//for more detail about how to operate, you can find information from its guide when installed

xmlStr = xmlStr.replace(/</g,"&lt;");
xmlStr = xmlStr.replace(/>/g,"&gt;");
document.write(xmlStr);


xmlDOC = null;
}else{
alert("fail in sending SOAP");
}
}

</script>

//the next step is to add some simple input field and button
<div align="center">
search Content<input name="content" type="text"><input name="go" type="button" value="Go" onClick="sendRequest();receiveResponse();">
<hr>
</div>

Some Notes:
You can add a DIV like:

<div id=result>
</div>

and use result.innerHTML=... to display the result returned by web service

Conclusion

We you have finished them, you can invoke yout webservice by click the button.

The way to invoke web service is just a example on tech. But it is not suggected in used in your site, since the visited can see all the code include the SOAP detail, which may cause security problem. Instead, you can copy the code to ASP in the <%%>. the code will be compile can can not be seen. Through the example, i see how web service is powerful and useful.

上一篇: 用eclipse wtp 开发JSP
下一篇: 设计迷踪:给JAVA设计开发新手的一些建议和意见(一)

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:在java应用程序中显示数据库的blob图像

下一篇:java.io.ObjectInput翻译