java Servlet 调用C++ gsoap服务端 简单加法

前几天整好了C++ gsoap 服务端、客户端,测试也基本通过,今天再用Java Servlet 做客户端测一下。

服务端代码地址

http://download.csdn.net/detail/qqwoaidushu88/9587879


测试要用到Java Web开发环境的搭建(JDK+Eclipse+Tomcat),可以参考下面这篇文章

http://blog.csdn.net/zhugexubin/article/details/41726587


环境搭建好后。


步骤1:创建一个testGsoap的web工程

打开Eclipse-》file-》Dynamic Web Project



步骤2:在刚刚创建的项目加一个servlet





步骤3:实现doGet函数参考以下代码

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		try {
			
			/*C++函数原型    
			 * int ns__add( int num1, int num2, int* sum );
			 */
			String endpoint = "http://127.0.0.1:8888"; 
			int n1=9,n2=21;
			Service service = new Service(); 
			Call call = (Call)service.createCall(); 
			
			//设置URL
			call.setTargetEndpointAddress(endpoint);	
			
			//设置要调用的函数名
			call.setOperationName(new QName( "urn:add", "add")); 
			
			//设置第一个参数
			call.addParameter( "num1",org.apache.axis.encoding.XMLType.XSD_INT,javax.xml.rpc.ParameterMode.IN); 
			//设置第二个参数
			call.addParameter( "num2",org.apache.axis.encoding.XMLType.XSD_INT,javax.xml.rpc.ParameterMode.IN);
			
			//设置返回类型
			call.setReturnType(org.apache.axis.encoding.XMLType.XSD_INT);
			
			//调用函数 传递参数
			int temp = (int)call.invoke(new Object[]{n1,n2});
			
			//输出结果
			System.out.println("result: "+ n1+"+"+n2+"="+ temp);
			response.getWriter().append("result: "+ n1+"+"+n2+"="+ temp);
			
			
		} catch (ServiceException e) {
			e.printStackTrace();
			response.getWriter().append("0"+e.toString());
		} catch (RemoteException e) {
			e.printStackTrace();
			response.getWriter().append("0"+e.toString());
		}
	}

这里要导入axis.jar 

http://download.csdn.net/download/xiaoyong8823/4391971

下载后解压,在lib文件夹内,把它们拷到自己工程的lib文件内



之前实现服务端时add.h文件 内容

//gsoap ns service name: add
//gsoap ns service namespace: http://localhost/add.wsdl
//gsoap ns service location: http://localhost
//gsoap ns service executable: add.cgi
//gsoap ns service encoding: encoded
//gsoap ns schema namespace: urn:add

int ns__add( int num1, int num2, int* sum );

add.wsdl文件内容

<?xml version="1.0" encoding="UTF-8"?>
<definitions name="add"
  targetNamespace="http://localhost/add.wsdl"
  xmlns:tns="http://localhost/add.wsdl"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:ns="urn:add"
  xmlns:SOAP="http://schemas.xmlsoap.org/wsdl/soap/"
  xmlns:HTTP="http://schemas.xmlsoap.org/wsdl/http/"
  xmlns:MIME="http://schemas.xmlsoap.org/wsdl/mime/"
  xmlns:DIME="http://schemas.xmlsoap.org/ws/2002/04/dime/wsdl/"
  xmlns:WSDL="http://schemas.xmlsoap.org/wsdl/"
  xmlns="http://schemas.xmlsoap.org/wsdl/">

<types>

  <schema targetNamespace="urn:add"
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:ns="urn:add"
    xmlns="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="unqualified"
    attributeFormDefault="unqualified">
    <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
    <!-- operation request element -->
    <element name="add">
      <complexType>
          <sequence>
            <element name="num1" type="xsd:int" minOccurs="1" maxOccurs="1"/><!-- ns__add::num1 -->
            <element name="num2" type="xsd:int" minOccurs="1" maxOccurs="1"/><!-- ns__add::num2 -->
          </sequence>
      </complexType>
    </element>
    <!-- operation response element -->
    <element name="addResponse">
      <complexType>
          <sequence>
            <element name="sum" type="xsd:int" minOccurs="0" maxOccurs="1" nillable="true"/><!-- ns__add::sum -->
          </sequence>
      </complexType>
    </element>
  </schema>

</types>

<message name="addRequest">
  <part name="Body" element="ns:add"/><!-- ns__add::ns__add -->
</message>

<message name="addResponse">
  <part name="Body" element="ns:addResponse"/>
</message>

<portType name="addPortType">
  <operation name="add">
    <documentation>Service definition of function ns__add</documentation>
    <input message="tns:addRequest"/>
    <output message="tns:addResponse"/>
  </operation>
</portType>

<binding name="add" type="tns:addPortType">
  <SOAP:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
  <operation name="add">
    <SOAP:operation soapAction=""/>
    <input>
          <SOAP:body parts="Body" use="literal"/>
    </input>
    <output>
          <SOAP:body parts="Body" use="literal"/>
    </output>
  </operation>
</binding>

<service name="add">
  <documentation>gSOAP 2.8.33 generated service definition</documentation>
  <port name="add" binding="tns:add">
    <SOAP:address location="http://localhost/add.cgi"/>
  </port>  <port name="add" binding="tns:add">
    <SOAP:address location="http://localhost"/>
  </port>
</service>

</definitions>


步骤4:Servlet 搞定后就先启动服务端


步骤5:

测试servlet








然后关闭服务端 再刷新浏览器




重新启动服务端后再测



简单测试基本搞定。






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值