软件环境

1、  Windows 2000 professional sp4

2、  axis-1_3 (http://ws.apache.org/axis/下载);

3、  tomcat-5.0.28;

4、  jdk1.4.2

Axis的测试demo

axis-bin-1_3.zip解压的webapps目录下的axis目录拷贝到%TOMCAT_HOME%\webapps目录下(最好在%TOMCAT_HOME%\webapps\axis\WEB-INF\lib增加:activation.jar、mail.jar和tools.jar),然后启动tomcat,在浏览器地址栏输入http://localhost:8080/axis/就可以看到axis的主页面了。

Webservice程序发布

1、编写简单的webservice程序如下:

package test.webservice;

 

public class Calculator {

    public int add(int i1, int i2) {

        return i1 + i2;

    }

 

    public int subtract(int i1, int i2) {

        return i1 - i2;

    }

 

    public String show(String arg) {

        return arg;

    }

}

1、 将程序包拷贝到%TOMCAT_HOME%\webapps\axis\WEB-INF\classes目录下;

2、 配置环境变量

AXIS_HOME = axis-bin-1_3.zip的解压路径

AXISCLASSPATH = %AXIS_HOME%\lib\axis.jar;%AXIS_HOME%\lib\axis-schema.jar;%AXIS_HOME%\lib\commons-discovery-0.2.jar;%AXIS_HOME%\lib\commons-logging-1.0.4.jar;%AXIS_HOME%\lib\jaxrpc.jar;%AXIS_HOME%\lib\log4j-1.2.8.jar;%AXIS_HOME%\lib\saaj.jar;%AXIS_HOME%\lib\wsdl4j-1.5.1.jar;

3、 编写发布文件deploy.wsdd(该文件的模板可以从%AXIS_HOME%\samples\stock目录下找到),将其放到%TOMCAT_HOME%\webapps\axis\WEB-INF目录下;

<deployment name="test" xmlns="http://xml.apache.org/axis/wsdd/"

    xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

 

  <service name="Calculator" provider="java:RPC">

    <!--指定作为webservice的类名-->

    <parameter name="className" value="test.webservice.Calculator" />

    <!--指定可以被调用的方法,有选择使用逗号间隔,全部使用*-->

    <parameter name="allowedMethods" value="add,subtract,show" />   

  </service>

 

</deployment>

4、 命令行方式转到%TOMCAT_HOME%\webapps\axis\WEB-INF目录下,使用命令:

java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient -lhttp://localhost/axis/services/AdminService deploy.wsdd

deploy.wsdd文件转变为server-config.wsdd(其实同时还生成attachments文件夹),这样在tomcat启动时可以被AxisServlet调用初始化。注意:使用命令时tomcat应处于启动状态。

5、 发布完毕,在浏览器地址栏输入http://localhost:8080/axis/services可以看到你发布的webservice了,可以查看具体的wsdl。

6、 java客户端测试程序

package test.webservice;

 

import javax.xml.rpc.ParameterMode;

import javax.xml.rpc.encoding.XMLType;

 

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

 

public class CalcClient {

    public static void main(String[] args) throws Exception {

        String endpoint = "http://localhost:8080/axis/services/Calculator";

 

        Service service = new Service();

        Call call = (Call) service.createCall();

 

        call.setTargetEndpointAddress(new java.net.URL(endpoint));

        // call.setOperationName("add");

        call.setOperationName("subtract");

        // call.setOperationName("show");

 

        call.addParameter("p1", XMLType.XSD_INT, ParameterMode.IN);

        call.addParameter("p2", XMLType.XSD_INT, ParameterMode.IN);

        call.setReturnType(XMLType.XSD_INT);

 

        Integer ret = (Integer) call.invoke(new Object[] { new Integer(5), new Integer(3) });

        // String ret = (String) call.invoke(new Object[] { "axis你好!" });

 

        System.out.println("The value of webservice return is :" + ret);

    }

}

 

添加用户验证

用户验证使用axis的简单用户验证handler:SimpleAuthenticationHandler。

1、 deploy.wsdd文件中增加以下红色字体部分

<deployment name="test" xmlns="http://xml.apache.org/axis/wsdd/"

    xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

 

  <service name="Calculator" provider="java:RPC">

    <!--指定作为webservice的类名-->

    <parameter name="className" value="test.webservice.Calculator" />

    <!--指定可以被调用的方法,有选择使用逗号间隔,全部使用*-->

    <parameter name="allowedMethods" value="add,subtract,show" />

    <!--指定可以调用用户-->

    <parameter name="allowedRoles" value="testname" />    

    <requestFlow>     

        <handler type="Authenticate"/>

     </requestFlow>

  </service>

 

</deployment>

2、 %TOMCAT_HOME%\webapps\axis\WEB-INF目录下建立users.lst文件,内容如下:

testname testpass

3、 重复 Webservice 程序发布的第4步,重新发布(也可以直接修改 server-config.wsdd文件 )。

4、 修改客户端调用程序

package test.webservice;

 

import javax.xml.rpc.ParameterMode;

import javax.xml.rpc.encoding.XMLType;

 

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

 

public class CalcClient {

    public static void main(String[] args) throws Exception {

        String endpoint = "http://localhost:8080/axis/services/Calculator";

 

        Service service = new Service();

        Call call = (Call) service.createCall();

       

        // 用户验证

        call.getMessageContext().setUsername("testname"); // 用户名

        call.getMessageContext().setPassword("testpass"); // 密码

 

        call.setTargetEndpointAddress(new java.net.URL(endpoint));

        // call.setOperationName("add");

        call.setOperationName("subtract");

        // call.setOperationName("show");

 

        call.addParameter("p1", XMLType.XSD_INT, ParameterMode.IN);

        call.addParameter("p2", XMLType.XSD_INT, ParameterMode.IN);

        call.setReturnType(XMLType.XSD_INT);

 

        Integer ret = (Integer) call.invoke(new Object[] { new Integer(5), new Integer(3) });

        // String ret = (String) call.invoke(new Object[] { "axis 你好! " });

 

        System.out.println("The value of webservice return is :" + ret);

    }

}

 

本例程在上述软件环境上测试通过,源码下载: axisdemo

 

posted on 2006-05-06 20:31 野草 阅读(3520) 评论(3)  编辑  收藏 所属分类: axis

评论:
# re: axis使用笔记 2007-07-09 16:17 | 凌观生
这已经是第五次用你的网页的,你的笔记很有用啊,但不知道你的QQ或Email是多少,有机会直接向你提问就好了问

QQ:303901823
Mail:ling0725@126.com  回复  更多评论
  
# re: axis使用笔记 2007-07-10 10:09 | 凌观生
老提示这个:
Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled
配置文件到底要怎么样啊mail.jar、activation.jar都已经在

CLASSPATH = .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar;%JAVA_HOME%\lib\dt.jar;%AXIS_HOME%\activation.jar;%AXIS_HOME%\mail.jar

下了  回复  更多评论
  
# re: axis使用笔记 2007-12-13 14:10 | guest
嗯,通过setUserName之后,似乎在Auth的时候一直得不到用户名/密码的说.  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: