我的webservice Hello world-axis

准备:

不论是服务端还是客户端,都要导入相关包,报名如下:

activation.jar

axis-ant.jar

axis.jar

commons-discovery-0.2.jar

commons-logging-1.0.4.jar

jaxrpc.jar

log4j-1.2.8.jar

mail.jar

saaj.jar

wsdl4j-1.5.1.jar

服务端:

写一个类,如下:

 

package server;

public class Hello {
	public String sayHello(String name) {
		return name + "\tHello!";
	}
}

 在web.xml文件中加入以下配置:

	<servlet>
		<servlet-name>InterfaceService</servlet-name>
		<servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
		<load-on-startup>7</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>InterfaceService</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>

 为以上的类配置,建立一个文件deploy.wsdd,加入以下内容:

<deployment xmlns="http://xml.apache.org/axis/wsdd/"
	xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
	<service name="Hello" provider="java:RPC">
		<parameter name="className" value="server.Hello" />
		<parameter name="allowedMethods" value="*" />
	</service>
</deployment>

 新建文件deploy.bat,内容如下:

set Axis_Lib=E:\apache-tomcat-6.0.26\webapps\axisServer\WEB-INF\lib
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
set Axis_Servlet=http://localhost:8888/axisServer/services/InterfaceService
%Java_Cmd% org.apache.axis.client.AdminClient -l%Axis_Servlet% deploy.wsdd

 在deploy.bat文件所在的目录,用命令提示符运行该文件,运行成功后打开地址:http://localhost:8888/axisServer/services/Hello?wsdl。此时服务端已经完成,将网页内容复制下来。

客户端:

将复制下来的内容放在客户端根目录的wsdl文件夹下的axisServer.xml文件上:

  <?xml version="1.0" encoding="UTF-8" ?> 
- <wsdl:definitions targetNamespace="http://localhost:8888/axisServer/services/Hello" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:8888/axisServer/services/Hello" xmlns:intf="http://localhost:8888/axisServer/services/Hello" 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">
- <!-- 
WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)

  --> 
- <wsdl:message name="sayHelloResponse">
  <wsdl:part name="sayHelloReturn" type="xsd:string" /> 
  </wsdl:message>
- <wsdl:message name="sayHelloRequest">
  <wsdl:part name="name" type="xsd:string" /> 
  </wsdl:message>
- <wsdl:portType name="Hello">
- <wsdl:operation name="sayHello" parameterOrder="name">
  <wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest" /> 
  <wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse" /> 
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="HelloSoapBinding" type="impl:Hello">
  <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> 
- <wsdl:operation name="sayHello">
  <wsdlsoap:operation soapAction="" /> 
- <wsdl:input name="sayHelloRequest">
  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://server" use="encoded" /> 
  </wsdl:input>
- <wsdl:output name="sayHelloResponse">
  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8888/axisServer/services/Hello" use="encoded" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:service name="HelloService">
- <wsdl:port binding="impl:HelloSoapBinding" name="Hello">
  <wsdlsoap:address location="http://localhost:8888/axisServer/services/Hello" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>

 在同等目录下建立WSDL2Java.bat,内容如下:

set Axis_Lib=E:\workspace\axisServerClient\WebRoot\WEB-INF\lib
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
set Output_Path=E:\workspace\axisServerClient\src
set Package=server
%Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% axisServer.wsdl

 在该文件所在目录下用命令提示符运行,运行完毕后,在src目录下,会多出一堆文件。建立测试文件测试:

package test;

import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

import server.HelloService;
import server.HelloServiceLocator;

public class WeatherClient {
	public static void main(String[] args) {
		HelloService service = new HelloServiceLocator();
		String info = null;
		try {
			info = service.getHello().sayHello("July");
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		System.out.println(info);
	}
}	

 运行后会打印:July    Hello!

 

 

你可能感兴趣的:(apache,webservice,servlet,ext,SOAP)