以下逐一講述web service在axis上的部署和開發(fā),設置classpath set CLASSPATH=.;%AXIS_HOME%\lib\axis.jar;%AXIS_HOME%\lib\axis-ant.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\saaj.jar;%AXIS_HOME%\lib\wsdl4j-1.5.1.jar;%AXIS_HOME%\lib\log4j-1.2.8.jar;E:/thirdparty/activation/activation.jar;E:/thirdparty/activation/mail.jar
二、編寫DII(Dynamic Invocation Interface )方式web服務 1.編寫服務端程序HelloClient public class HelloClient { public String getName(String name) { return "hello "+name; } }
public class TestHelloClient { public static void main(String[] args) { try { String wsdlUrl = "http://localhost:8080/axis/HelloClient.jws?wsdl"; String nameSpaceUri = "http://localhost:8080/axis/HelloClient.jws"; String serviceName = "HelloClientService"; String portName = "HelloClient";
ServiceFactory serviceFactory = ServiceFactory.newInstance(); Service afService = serviceFactory.createService(new URL(wsdlUrl), new QName(nameSpaceUri, serviceName)); HelloClientInterface proxy = (HelloClientInterface) afService.getPort(new QName( nameSpaceUri, portName), HelloClientInterface.class); System.out.println("return value is "+proxy.getName("john") ) ; }catch(Exception ex) { ex.printStackTrace() ; } } }
四、編寫wsdd發(fā)布web服務,編寫stub client訪問web服務
1.編寫服務端程序server,SayHello.java,編譯server.SayHello.java package server; public class SayHello { public String getName(String name) { return "hello "+name; } } 2.編寫LogHandler.java import org.apache.axis.AxisFault; import org.apache.axis.Handler; import org.apache.axis.MessageContext; import org.apache.axis.handlers.BasicHandler;
import java.util.Date;
public class LogHandler extends BasicHandler { public void invoke(MessageContext msgContext) throws AxisFault { /** Log an access each time we get invoked. */ try { Handler serviceHandler = msgContext.getService();
Integer numAccesses = (Integer)serviceHandler.getOption("accesses"); if (numAccesses == null) numAccesses = new Integer(0);
numAccesses = new Integer(numAccesses.intValue() + 1);
Date date = new Date(); String result = date + ": service " + msgContext.getTargetService() + " accessed " + numAccesses + " time