基于Tomcat5.0和Axis2开发WebService应用实例

1.工作环境

Eclipse 3.1.2+Lomboz+jdk1.5+ apache-tomcat-5.0.18+AXIS2:1.0(war版本和bin版本)

在http://ws.apache.org/axis2/download/1_0/download.cgi页面下,下载AXIS2的Binary Distribution url: http://apache.justdn.org/ws/axis2/1_0/axis2-std-1.0-bin.zip和war Distribution url: http://apache.justdn.org/ws/axis2/1_0/axis2-1.0-docs.zip。把这两个文件解压,比如解压缩的后得目录为C:/axis2-std-1.0-bin和C:/axis2.war。

在Eclipse下通过菜单window—preferences…–Java—Build Path—User Libraries 新建一个user library,比如名字就叫axis2把C:/axis2-std-1.0-bin/lib下的所有jar文件包含进来。把axis2.war拷贝到%TOMCAT-HOME%/webapps下面。

2.检验安装

在Eclipse下启动Tomcat,在地址栏内输入http://localhost:8080/axis2/。

点击Validate,将到达 Axis2 Happiness Page。

3.WebService中的HelloWorld

1)新建一个动态web工程,取名ZZaxis,右键点击项目名,选择Properties-Java Build Path-Add Library-User Library-axis2。

2)新建package sample,建立HelloWorld.java,代码如下。

HelloWorld.java

import org.apache.axiom.om.OMAbstractFacTory;import org.apache.axiom.om.OMElement;import org.apache.axiom.om.OMFacTory;import org.apache.axiom.om.OMNamespace;public class HelloWorld {    public OMElement sayHello(OMElement in){        String name=in.getText();        String info=name+"HelloWorld!";        OMFacTory fac=OMAbstractFacTory.getOMFacTory();        OMNamespace mNs=fac.createOMNamespace("http://helloworld.com/","hw");        OMElement resp=fac.createOMElement("sayHelloResponse",omNs);        resp.setText(info);        return resp;    }}

3)在WebContent/META-INF/建立services.xml,代码如下。

services.xml

  This is a sample Web Service.sample.HelloWorld 

4)将目录sample和目录META-INF组织如下(新建目录example)。

+-example

|——– +-sample

|——- HelloWorld.class

|———+-META-INF

|——- services.xml

5)打包生成aar文件。

在命令符环境下,将当前目录转到example。

jar cvf HelloWorld.aar . //注意最后一个点,在当前目录下生成HelloWorld.aar。

6)在Eclipse中启动Tomcat,在地址栏下键入http://localhost:8080/axis2/。选择Administration,输入用户名admin,密码axis2。选择左侧工具栏Tools- Upload Service,上传之前打包的HelloWorld.aar。该文件将在/webapps/axis2/WEB-INF/services目录下。

7)编写客户端检验代码。新建Java Project,取名为ZZaxisClient。右键点击项目名,选择Properties-Java Build Path-Add Library-User Library-axis2。

8)新建package example.client。建立TestClient.java,代码如下。

TestClient.java

package example.client;import org.apache.axiom.om.OMAbstractFacTory;import org.apache.axiom.om.OMElement;import org.apache.axiom.om.OMFacTory;import org.apache.axiom.om.OMNamespace;import org.apache.axis2.addressing.EndpointReference;import org.apache.axis2.client.Options;import org.apache.axis2.client.ServiceClient;public class TestClient {    private static EndpointReference targetEPR=new EndpointReference     ("http://localhost:8080/axis2/services/HelloWorld");    public static OMElement getSayHelloOMElement(){        OMFacTory fac=OMAbstractFacTory.getOMFacTory();        OMNamespace mNs=fac.createOMNamespace("http://helloworld. 少一点预设的期待,那份对人的关怀会更自在

基于Tomcat5.0和Axis2开发WebService应用实例

相关文章:

你感兴趣的文章:

标签云: