web service(axis)例子HelloService步骤说明

1.将axis1.1目录下webapps下的axis包拷贝到tomcat5.0/webapps下
2.打开浏览器http://localhost:8080/axis,点击Validate,缺少activation.jar,mail.jar,xmlsec.jar三个包,要下载并放到tomcat5.0\webapps\axis\WEB-INF\lib目录下。
3.设置CLASSPATH(windows2000下),如下:
AXIS_HOME=D:\programtool\Tomcat5.0\webapps\axis
AXIS_CLASSPATH=%AXIS_HOME%\WEB-INF\classes;%AXIS_HOME%\WEB-INF\lib\axis.jar;%AXIS_HOME%\WEB-INF\lib\axis-ant.jar;%AXIS_HOME%\WEB-INF\lib\commons-discovery.jar;%AXIS_HOME%\WEB-INF\lib\commons-logging.jar;%AXIS_HOME%\WEB-INF\lib\jaxrpc.jar;%AXIS_HOME%\WEB-INF\lib\log4j-1.2.8.jar;%AXIS_HOME%\WEB-INF\lib\saaj.jar;%AXIS_HOME%\WEB-INF\lib\wsdl4j.jar;%AXIS_HOME%\WEB-INF\lib\activation.jar;%AXIS_HOME%\WEB-INF\lib\xmlsec.jar;%AXIS_HOME%\WEB-INF\lib\mail.jar       
CLASSPATH=.;%AXIS_CLASSPATH%         
注:windows2000下设置完需重启。
4.测试java org.apache.axis.client.AdminClient list,会列出Axis服务器的部署信息。
5.编译要发布为服务的类(HelloService.java),将所得的class文件(HelloService.class)复制到tomcat5.0\webapps\axis\WEB-INF\classes 目录下。
6.切换到deploy.wsdd所在目录,运行java org.apache.axis.client.AdminClient deploy.wsdd,如果部署成功,会输出<Admin>Done processing</Admin>信息。可以点击http://localhost:8080/axis浏览器中View来查看所发布的服务。
7.接下来客户端测试该服务。首先,Axis提供了实现WSDL2Java的命令行工具org.apache.axis.wsdl.WSDL2Java,利用这个工具可以从WSDL地址处生成Java代码。命令如下:
java org.apache.axis.wsdl.WSDL2Java -Nhttp://localhost:8080/axis/services/HelloService=com http://localhost:8080/axis/services/HelloService?wsdl
会在com(由上述命令行中参数设定)包下生成HelloService.java、HelloServiceService.java、HelloServiceServiceLocator.java和HelloServiceSoapBindingStub.java四个文件。
8.编译客户端测试类(javac HelloServiceClient.java)并运行(java HelloServiceClient),cmd里会得到应输出语句。

附:
/*
*HelloService.java
*/
public class HelloService
{
  public String sayHello(String name)
  {
    System.out.println("HelloService!!!");
    return "HelloService say hello to "+name;
  }
}
/*
*HelloServiceClient.java
*/
//import java.rmi.RemoteException;
//import javax.xml.rpc.ServiceExpection;
//import com.*;
public class HelloServiceClient
{
  public static void main(String[] args)
  {
   try
   {
    com.HelloServiceServiceLocator locator=new com.HelloServiceServiceLocator();
    com.HelloService service=locator.getHelloService();
    System.out.println(service.sayHello("bill!!!"));
   }
   catch(Exception e)
   {
     System.err.println("Execution failed.Exception:"+e);
   }
  }
}
/*
*deploy.wsdd
*/
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
    xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="HelloService" provider="java:RPC">
  <parameter name="className"
      value="HelloService"/>
  <parameter name="allowedMethods" value="*"/>
</service>
</deployment>

你可能感兴趣的:(web Service)