js调用WebService服务

1:建立的webservice工程正确运行。
a: 定义接口类
public interface ITestWebService {
public String GetTextInfo(String message);
}
b:实现类
public class TestWebServiceImpl implements ITestWebService {
   public String GetTextInfo(String message) {
      message = "Hello : " + message ;
      System.out.println(message);
      return message;
   }
}
写个测试类,利用xfire相关函数测试当前webservice是否可以正确的调用
public static void main(String[] args) {
  // TODO Auto-generated method stub
     Service srvcModel = new ObjectServiceFactory().create(ITestWebService.class);
     XFireProxyFactory factory =  new XFireProxyFactory(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.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </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" />

 <SCRIPT LANGUAGE="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 )  {  return this.__extendPool(); }
   return xmlhttp;
  },
     
  __extendPool : function()
  {
   if ( this.m_XmlHttpPool.length < this.m_MaxPoolLength )
   {
    var xmlhttp = null;
    try
    {
     xmlhttp = new ActiveXObject('MSXML2.XMLHTTP');
    }
    catch(e)
    {
     try  {  xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); } catch(e2) {}
    }
    if ( xmlhttp ) { this.m_XmlHttpPool.push(xmlhttp); }
    return xmlhttp;
   }
  },
     RequestWebService: function(URL,method,value,callback)
  {
   this.__requestWebService(URL,method,value,callback);
  },
  __requestWebService: function(URL,method,value,callback)
  {
   var data;
   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() 
  
   var extendPool = this.__extendPool; 
   this.__extendPool = function()  return null;   
   for ( var i=0 ; i < this.m_XmlHttpPool.length ; ++i )  this.m_XmlHttpPool[i].abort(); 
   this.__extendPool = extendPool; 
  }
 };

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

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

 </Script>

 </BODY>
</HTML>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值