js调用WebService服务

来源:互联网 发布:重庆seo外包公司费用 编辑:程序博客网 时间:2024/05/16 10:45

1:建立的webservice工程正确运行。
a: 定义接口类
public interface ITestWebService {
public String GetTextInfo(String message);
}
b:实现类
public class TestWebServiceImpl implements ITestWebService {
   public StringGetTextInfo(String message) {
     message = "Hello : " + message ;
     System.out.println(message);
     return message;
   }
}
写个测试类,利用xfire相关函数测试当前webservice是否可以正确的调用
public static void main(String[] args) {
  // TODO Auto-generated methodstub
    Service srvcModel = newObjectServiceFactory().create(ITestWebService.class);
    XFireProxyFactory factory =  newXFireProxyFactory(XFireFactory.newInstance().getXFire());
    String ServerURL ="http://192.168.100.1/TermSerivce/services/GetTextInfo";
    try {
       ITestWebService srvc = (ITestWebService)factory.create(srvcModel,ServerURL);       
       String result=srvc.GetTextInfo("ha ha!");
       System.out.println(result);
        }catch (MalformedURLException e) {
       e.printStackTrace();
    }
   }

下面写js中调用

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> NewDocument </TITLE>
  <META NAME="Generator"CONTENT="EditPlus">
  <META NAME="Author"CONTENT="">
  <META NAME="Keywords"CONTENT="">
  <META NAME="Description"CONTENT="">
  <meta http-equiv="Content-Type"content="text/html; charset=UTF-8" />

 <SCRIPTLANGUAGE="JavaScript">
 <!--


 var __XmlHttpPool__ =
 {
  
  m_MaxPoolLength : 10,
  m_XmlHttpPool : [],
     
  __requestObject :function()
  {
   var xmlhttp =null;
   var pool =this.m_XmlHttpPool;
   for ( var i=0; i < pool.length ; ++i )
   {
    if( pool[i].readyState == 4 || pool[i].readyState == 0 ) { xmlhttp = pool[i];  break; }
   }
   if ( xmlhttp== null )  {  returnthis.__extendPool(); }
   returnxmlhttp;
  },
     
  __extendPool : function()
  {
   if (this.m_XmlHttpPool.length < this.m_MaxPoolLength)
   {
    varxmlhttp = null;
    try
    {
     xmlhttp= new ActiveXObject('MSXML2.XMLHTTP');
    }
    catch(e)
    {
     try {  xmlhttp = newActiveXObject('Microsoft.XMLHTTP'); } catch(e2) {}
    }
    if( xmlhttp ) { this.m_XmlHttpPool.push(xmlhttp); }
    returnxmlhttp;
   }
  },
    RequestWebService: function(URL,method,value,callback)
  {
   this.__requestWebService(URL,method,value,callback);
  },
  __requestWebService:function(URL,method,value,callback)
  {
   vardata;
   data ='<?xml version="1.0"encoding="utf-8"?>';
   data = data +'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
   data = data +'<soap:Body>';
   data = data +'<' + method + ' xmlns="http://tempuri.org/';
   data = data +method + '">';
   data = data +'<in0>'+value+'</in0>';
   
   data = data +'</' + method + '>';
   data = data +'</soap:Body>';
   data = data +'</soap:Envelope>';
   var xmlhttp =this.__requestObject();
   if ( !xmlhttp){ return null; }
  
   xmlhttp.Open("POST",URL,true);
   xmlhttp.SetRequestHeader("Content-Type","text/xml; charset=utf-8");
   xmlhttp.SetRequestHeader("SOAPAction","http://tempuri.org/example");

   xmlhttp.onreadystatechange= function()
   {
    if( xmlhttp.readyState == 4 || xmlhttp.readyState == 'complete' ) {callback(xmlhttp.responseXML); }
   };
   xmlhttp.send(data);
  },
  PostData : function(URL,method,data, callback)
  {
   this.__receiveRemoteData(URL,'POST','type='+method+'&item='+data,callback);
  }, 
  __receiveRemoteData :function(url,httpmethod,data,callback)
  {
   var xmlhttp =this.__requestObject();
   if ( !xmlhttp){ return null; }
  
   xmlhttp.open(httpmethod,URL, true);
   xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

   xmlhttp.onreadystatechange= function()
   {
    if( xmlhttp.readyState == 4 || xmlhttp.readyState == 'complete' ) {callback(xmlhttp.responseText); }
   };
   xmlhttp.send(data);
  },
  CancelAll :function() 
  
   varextendPool = this.__extendPool; 
   this.__extendPool= function()  returnnull;   
   for ( var i=0; i < this.m_XmlHttpPool.length ; ++i) this.m_XmlHttpPool[i].abort(); 
   this.__extendPool= extendPool; 
  }
 };

 function recv(xml)
 {
  varret=xml.getElementsByTagName_r("ns1:out")[0].firstChild.nodeValue;
  alert(ret);
 }

 var URL="http://192.168.100.1/TermService/services/TermInfo";
 __XmlHttpPool__.RequestWebService(URL,"GetTextInfo","haha",recv);

 </Script>

 </BODY>
</HTML>

原创粉丝点击