To invoke Web Service in .htm file
2008-02-23 09:32:33来源:互联网 阅读 ()
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,"<");
xmlStr = xmlStr.replace(/>/g,">");
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
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash