转自:http://www.itkeyword.com/doc/7529577946427268306/Apache-Servlet-WebSOAPWebService

所谓Web Service就是客户端以标准的SOAP消息将服务请求发给服务提供者,不论服务提供者用什么样的技术,Java、EJB、或者.NET执行相应的程序得到结果,然后将结果以SOAP消息返回给服务请求者。以axis为例,创建一个简单的Web Service

首先到http://ws.apache.org/axis/上去下载最新的axis版本。最新版本是axis-src-1_4.zip

NEWS (April 22, 2006): Axis 1.4 Final is now available!

第一步:创建WebService项目,命名为Webservice_Begin

解压axis-src-1_4.zip后,拷贝lib目录下最基本的jar包至项目WEB-INF/lib目录下,如下列表:

axis.jar

commons-discovery-0.2.jar

commons-logging-1.1.jar

jaxrpc.jar

mailapi_1_3_1.jar

wsdl4j-1.5.1.jar

junit-4.9-SNAPSHOT-20100512-0041.jar

第二步:创建WebService服务器端接口和实现类

  1. package com.unis.p2p.server;
  2. /**
  3. * WebService服务器端业务逻辑接口
  4. *
  5. * @author Posey 2010-12-17
  6. */
  7. public interface Hello {
  8. /**
  9. * 接口方法:接收客户端WebService请求
  10. *
  11. * @author Posey 2010-12-17
  12. * @param message
  13. * @return
  14. */
  15. public String executeTaskList(String message);
  16. }
  1. package com.unis.p2p.server;
  2. /**
  3. * WebService服务器端业务逻辑实现类
  4. * @author Posey 2010-12-17
  5. *
  6. */
  7. public class HelloImpl implements Hello {
  8. /**
  9. * 接收客户端WebService请求,具体执行相关操作
  10. *
  11. * @author Posey 2010-12-17
  12. */
  13. public String executeTaskList(String message) {
  14. return "OK|调用成功! " + message;
  15. }
  16. }

第三步:修改WEB-INF/web.xml配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  6. <!-- ********************************************** -->
  7. <!-- WebService核心处理类 -->
  8. <servlet>
  9. <servlet-name>AxisServlet</servlet-name>
  10. <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
  11. </servlet>
  12. <servlet-mapping>
  13. <servlet-name>AxisServlet</servlet-name>
  14. <url-pattern>/services/*</url-pattern>
  15. </servlet-mapping>
  16. <!-- ********************************************** -->
  17. <welcome-file-list>
  18. <welcome-file>index.jsp</welcome-file>
  19. </welcome-file-list>
  20. </web-app>

第四步:创建WebRoot/WEB-INF/server-config.wsdd文件,定义服务的名称,具体的实现类,以及发布的方法和属性等等

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
  3. <handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper" />
  4. <service name="executeTask" provider="java:RPC">
  5. <parameter name="className" value="com.unis.p2p.server.HelloImpl" />
  6. <parameter name="allowedMethods" value="executeTaskList" />
  7. </service>
  8. <transport name="http">
  9. <requestFlow>
  10. <handler type="URLMapper" />
  11. </requestFlow>
  12. </transport>
  13. </deployment>

第五步:启动应用服务器,发布Web Service服务。

地址栏输入:http://127.0.0.1:8080/Webservice_Begin/services回车,如果没有错误提示,则恭喜你,你的Web Service已经发布成功。

点击wsdl,还可以看到具体的wsdl的配置信息如下:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. - <wsdl:definitions targetNamespace="http://127.0.0.1:8080/Webservice_Begin/services/executeTask" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://127.0.0.1:8080/Webservice_Begin/services/executeTask" xmlns:intf="http://127.0.0.1:8080/Webservice_Begin/services/executeTask" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  3. - <!--
  4. WSDL created by Apache Axis version: 1.4
  5. Built on Apr 22, 2006 (06:55:48 PDT)
  6. -->
  7. - <wsdl:message name="executeTaskListResponse">
  8. <wsdl:part name="executeTaskListReturn" type="soapenc:string" />
  9. </wsdl:message>
  10. - <wsdl:message name="executeTaskListRequest">
  11. <wsdl:part name="message" type="soapenc:string" />
  12. </wsdl:message>
  13. - <wsdl:portType name="HelloImpl">
  14. - <wsdl:operation name="executeTaskList" parameterOrder="message">
  15. <wsdl:input message="impl:executeTaskListRequest" name="executeTaskListRequest" />
  16. <wsdl:output message="impl:executeTaskListResponse" name="executeTaskListResponse" />
  17. </wsdl:operation>
  18. </wsdl:portType>
  19. - <wsdl:binding name="executeTaskSoapBinding" type="impl:HelloImpl">
  20. <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
  21. - <wsdl:operation name="executeTaskList">
  22. <wsdlsoap:operation soapAction="" />
  23. - <wsdl:input name="executeTaskListRequest">
  24. <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://server.p2p.unis.com" use="encoded" />
  25. </wsdl:input>
  26. - <wsdl:output name="executeTaskListResponse">
  27. <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://127.0.0.1:8080/Webservice_Begin/services/executeTask" use="encoded" />
  28. </wsdl:output>
  29. </wsdl:operation>
  30. </wsdl:binding>
  31. - <wsdl:service name="HelloImplService">
  32. - <wsdl:port binding="impl:executeTaskSoapBinding" name="executeTask">
  33. <wsdlsoap:address location="http://127.0.0.1:8080/Webservice_Begin/services/executeTask" />
  34. </wsdl:port>
  35. </wsdl:service>
  36. </wsdl:definitions>

第六步:创建WebService客户端测试类

/Webservice_Begin/test/com/unis/p2p/client/WebServiceTest.java

  1. package com.unis.p2p.client;
  2. import java.net.URL;
  3. import org.apache.axis.client.Call;
  4. import org.apache.axis.client.Service;
  5. import org.junit.Test;
  6. /**
  7. * 类说明:Client端测试类,发出请求
  8. * @author Posey 2010-12-17
  9. *
  10. */
  11. public class WebServiceTest {
  12. @Test
  13. public void testExecuteTask() throws Exception {
  14. String res = "";
  15. String ss = "ERR|2352|5683|2235|8428";
  16. String[] str = ss.split("\\|");
  17. String tempMsg = "";
  18. for (int i = 1; i < str.length; i++) {
  19. tempMsg += str[i] + ",";
  20. }
  21. tempMsg = tempMsg.substring(0, tempMsg.length()-1);
  22. //调接口
  23. res = this.executeTask("http://127.0.0.1:8080/Webservice_Begin/services/executeTask",
  24. "executeTaskList",
  25. new Object[]{ tempMsg });
  26. System.out.println(res);    //输出结果
  27. }
  28. //核心处理方法
  29. private synchronized String executeTask(String url, String method, Object[] args) throws Exception {
  30. // 创建Service实例
  31. Service service = new Service();
  32. // 通过Service实例创建Call实例
  33. Call call = (Call) service.createCall();
  34. // 将WebService的服务路径加入到Call实例中,并为Call设置服务的位置
  35. URL webServiceUrl = new URL(url);
  36. call.setTargetEndpointAddress(webServiceUrl);
  37. // 调用WebService方法
  38. call.setOperationName(method);
  39. // 调用WebService传入参数
  40. String result = (String) call.invoke(args);
  41. return result;
  42. }
  43. }

第七步:Run As - JUnit Test,执行成功

6. 使用Axis开发WebService程序的更多相关文章

  1. WebService应用--使用java开发WebService程序

    使用Eclipse开发第一个WebService程序,本示例采用的工具为Spring-Tool-Suite,和Eclipse没有本质的区别,开发环境jdk1.7 一.开发步骤: 1.新建名为WebSe ...

  2. axis2开发webservice程序

    一.环境 eclipse + jdk 6.0 + win7 64位 +tomcat7.0 二.创建服务端程序 1.新建web项目,webserviceTest 2.下载axis2,将lib目录下的ja ...

  3. JAX-WS开发WebService程序

    近来公司里要用的到WebService做开发,所以就自己学习了一下,刚开始感觉挺难的,但是真正学会以后,原来这么简单. 今天把这些东西哦记下来,以便日后的复习. 我来介绍一下我的开发环境:Eclips ...

  4. struts2结合axis开发webservice

    第一步:引入axis的依赖jar包  第二步:修改web.xml文件  <listener> <listener-class>org.apache.axis.transport ...

  5. 使用CXF开发WebService程序的总结(七):Spring+CXF+Mybatis+Mysql共同打造的服务端示例

    通过该demo,可以 熟悉下 spring+cxf+maven+mybatis+mysql等常见后端技术整合 1. 在前面的 父工程 ws_parent 中 添加依赖 由于原来的项目是使用的cxf依赖 ...

  6. 使用CXF开发WebService程序的总结(六):结合拦截器使用

    1. 使用CXF提供的拦截器 拦截器在我看来分为两端两向,两端分为:客户端和服务端,两向(方向)分为:进(in)和出(out),即大致四类拦截器. 在这里利用cxf提供的 日志拦截器举例 1.1 在服 ...

  7. 使用CXF开发WebService程序的总结(五):基于Map数据类型处理的的客户端和服务端代码的编写

    1. 首先我们按照List或数组等处理方式来处理Map,看看效果 1.1 在服务端的接口中添加以下方法 /** * 查询所有班级信息加上对应的学生列表 * * @return */ public Ma ...

  8. 使用CXF开发WebService程序的总结(四):基于bean的客户端和服务端代码的编写

    1. 在原服务端项目 ws_server中添加两个bean 1.1 添加两个类  User 和 Clazz   package com.lonely.pojo; public class User { ...

  9. 使用CXF开发WebService程序的总结(三):创建webservice客户端

    1.创建一个maven子工程 ws_client,继承父工程 1.1 修改父工程pom配置 <modules> <module>ws_server</module> ...

随机推荐

  1. django 之数据库模块

    前提ajango的 数据库主要是为了存取网站的一些内容,数据库的设置一般放在model.py 下   目录下 我们设置如下的数据库:具体的代码如下面所示: # -*- coding: utf-8 -* ...

  2. 紫书 例题8-9 UVa 1451 (数形结合)

    这道题用了数形结合, 真的牛逼, 完全想到不到还可以这么做 因为题目求的是平均值, 是总数除以个数, 这个时候就可以联系 到斜率, 也就是说转化为给你一堆点, 让你求两点之间的最大斜率 要做两个处理 ...

  3. javascript取前n天的日期两种方法

    方法一: var d = new Date(); d = new Date(d.getFullYear(),d.getMonth(),d.getDate()-n); 方法二: var now = ne ...

  4. codevs1281 矩阵乘法 快速幂 !!!手写乘法取模!!! 练习struct的构造函数和成员函数

    对于这道题目以及我的快速幂以及我的一节半晚自习我表示无力吐槽,, 首先矩阵乘法和快速幂没必要太多说吧,,嗯没必要,,我相信没必要,,实在做不出来写两个矩阵手推一下也就能理解矩阵的顺序了,要格外注意一些 ...

  5. HDU——T 1507 Uncle Tom's Inherited Land*

    http://acm.hdu.edu.cn/showproblem.php?pid=1507 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  6. GridView中使用CheckBox

    asp.net中checkbox是向server提交时才干触发事件 把该控件的autopostback设置为true.才干响应事件  protected void Checkbox_CheckedCh ...

  7. 动态语言切换(续)-designer中的retranslateUi(带源码)

    本站所有文章由本站和原作者保留一切权力,仅在保留本版权信息.原文链接.原文作者的情况下允许转载,转载请勿删改原文内容, 并不得用于商业用途. 谢谢合作.原文链接:动态语言切换(续)-designer中 ...

  8. thinkphp5项目--个人博客(三)

    thinkphp5项目--个人博客(三) 项目地址 fry404006308/personalBlog: personalBloghttps://github.com/fry404006308/per ...

  9. 安卓开发--HttpDemo02

    package com.cnn.httpdemo02; import android.app.Activity; import android.os.Bundle; import android.vi ...

  10. BZOJ 2342 Manacher

    思路: 首先用manacher可以求出以i和i+1中间为对称轴,最长回文串能扩增的长度p[i]. 然后4*(y-x)能更新答案,当且仅当y≤x+p[x]/2且y-p[y]≤x. 按i-p[i]将所有点 ...