当前位置导航:炫浪网>>网络学院>>编程开发>>JAVA教程>>JAVA网络编程

axis 的客户端使用

1. http://ws.apache.org/Axis/下载 axis-1_2_1 解压放在 D:\softSetup\下

2.下载 apache-ant-1.6.1  解压放在D:\softSetup\下

3.环境变量设置:

JAVA_HOME=C:\jdk1.5.0_06
CATALINA_HOME=tomcat-5.5.15
ANT_HOME=D:\softSetup\apache-ant-1.6.1
AXISCLASSPATH=%AXIS_LIB%\activation.jar;%AXIS_LIB%\mail.jar;%AXIS_LIB%\axis.jar;%AXIS_LIB% \wsdl4j.jar;%AXIS_LIB%\axis-ant.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.0.4.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\saaj.jar;%AXIS_LIB%\log4j-1.2.8.jar;%AXIS_LIB%\xerces.jar;%AXIS_LIB%\junit.jar;%AXIS_LIB%\wsdl4j-1.5.1.jar
AXIS_HOME=D:\softSetup\axis-1_2_1
AXIS_LIB=%AXIS_HOME%\lib
CATALINA_HOME=D:\softSetup\jakarta-tomcat-5.0.24
classpath=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;%AXISCLASSPATH%
path=%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;%CATALINA_HOME%\bin;%ANT_HOME%\bin

4.在命令行 运行set 确保新加的环境变量生效

5.根据合作伙伴给的wsdl地址 http://www.****.com/services/OrderService?wsdl保存下来到D:\project\test\wsdl\OrderService.wsdl

6.在命令行 切换到 D:\project\test\wsdl\下运行axis的WSDL2Java 命令:  java org.apache.axis.wsdl.WSDL2Java OrderService.wsdl    生成 bean 文件,***Service.java,***ServiceLocator.java,***ServiceSoapBindingStub.java 等文件,可以用Axis的wsdl2java生成web service的客户端,wsdl2java生成的客户端是stub方式的。它包括endpoint借口、实现该接口的stub、 serviceLocator、可选的单元测试代码。其中,serviceLocator中hard code了服务端地址.

7. 应用.写一个表单jsp,提交到下面jsp文件进行处理(可以写在类里,此处简单处理)

<%@ page contentType="text/html; charset=GBK"%>
<%@page import="org.apache.axis.client.Call"%>
<%@page import="javax.xml.namespace.QName"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.rmi.RemoteException"%>
<%@page import="java.util.*"%>
<%@page import="java.net.URL"%>
<%@page import="org.apache.axis.client.Service"%>
<%@page import="com.newSky.datainterface.beans.*"%>
<%!

  private final static String success = "测试完成";
  private final static String fail = "测试失败:";
  final static String endPoint = "http://www.***.com/services/OrderService";
  final static String portName = "addNewOrder";
  final static String nameSpace = "http://beans.datainterface.newSky.com";
%>
<html>
<head></head>
<body>
<%
  try {
    Call call = null;
    URL url = null;
    try {
      //create new url
      url = new URL(endPoint);
    }
    catch (MalformedURLException e) {
      System.out.println(fail + e.getMessage());
      e.printStackTrace();
    }
    //create new service
    Service service = new Service();
    try {
      //create new call
      call = (Call) service.createCall();
    }
    catch (Exception se) {
      System.out.println(fail + se.getMessage());
      se.printStackTrace();
    }
    //设置服务器
    call.setTargetEndpointAddress(url);
    //设置端口
    call.setOperationName(new QName(endPoint, portName));
    //***********************
     //注册类,开始
     try {
       QName qn1 = new QName(nameSpace, "OrderUserData");
       call.registerTypeMapping(OrderUserData.class, qn1,
                                new org.apache.axis.encoding.ser.BeanSerializerFactory(OrderUserData.class, qn1),
                                new org.apache.axis.encoding.ser.BeanDeserializerFactory(OrderUserData.class, qn1));
     
     }
     catch (Exception EQ) {
       EQ.printStackTrace();
     }
    //注册类,结束
    //设置返回类型
    // set bean 此处省略。。
    try {
      //调用服务
      System.out.println("*************  测试开始 ******************");
      call.setUsername(request.getParameter("id"));
      String result = (String) call.invoke(new Object[] {new Integer(request.getParameter("id)),
                                             new String(request.getParameter("pwd")), orderData});
      System.out.println(result);

      if(result.equals("-100")){
        out.write("用户名密码错误");
      }
       else{
        out.write("成功");
      }
      System.out.println("*************  测试完成 ******************");
    }
    catch (RemoteException re) {
      System.out.println(fail + re.getMessage());
      //re.printStackTrace();
    }
  }
  catch (Exception e) {
    // e.printStackTrace();
  }
%>
</body>
</html>

8. ok ^_^

相关内容
赞助商链接