转自: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. webpack 操作

    依赖安装 :  全局安装webpack : sudo npm install webpack -g 本地安装webpack : npm install webpack —save-dev  需要注意的 ...

  2. 计数排序(counting-sort)

    计数排序是一种稳定的排序算法,它不是比较排序.计数排序是有条件限制的:排序的数必须是n个0到k的数,所以计数排序不适合给字母排序.计数排序时间复杂度:O(n+k),空间复杂度:O(k),当k=n时,时 ...

  3. HISTFILESIZE与HISTSIZE的区别

    在linux系统中,history命令可以输出历史命令,历史命令默认保存在文件~/.bash_history中. HISTFILESIZE 与 HISTSIZE都是history命令需要用到的两个sh ...

  4. 紫书 习题 8-1 UVa 1149(贪心)

    排序之后, 尽量最小和最大的放在一个背包, 放不下就放最大的. #include<cstdio> #include<algorithm> #define REP(i, a, b ...

  5. @SpringBootApplication cannot be resolved to a type In STS

    @SpringBootApplication cannot be resolved to a type In STS 学习了:https://stackoverflow.com/questions/4 ...

  6. GBK编码具体解析(附GBK码位分布图)

    1.GBK码位分布图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA= ...

  7. POJ--1966--Cable TV Network【无向图顶点连通度】

    链接:http://poj.org/problem?id=1966 题意:一个无向图,n个点,m条边,求此图的顶点连通度. 思路:顶点连通度,即最小割点集里的割点数目.一般求无向图顶点连通度的方法是转 ...

  8. 关于MAVEN找不到JDK的那点事

    自从SUN被Oracle收购以后.JDK就由Oracle来提供了. 在新版本号之中,假设你下载安装JDK以后,又选择了JRE安装(当然,如今JRE直接叫做Java了),那么,恭喜你,在 C:\wind ...

  9. poj_1195Mobile phones,二维树状数组

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...

  10. python判断一个单词是否为有效的英文单词?——三种方法

    For (much) more power and flexibility, use a dedicated spellchecking library like PyEnchant. There's ...