`
javawebsoa
  • 浏览: 409408 次
社区版块
存档分类
最新评论

axis 开发 web service 实战(选自JavaResearch)

 
阅读更多

<!-- end of article title -->
axis开发webservice实战
lianghyan 原创
<!--start of article content -->

一、axis安装
1.环境
J2SESDK1.3or1.4:我使用1.4.2
ServletContainer:我使用Tomcat5.0
2.到http://ws.apache.org/axis/网站下载axis安装包
3.解压缩安装包,将AXIS_UNZIP_PATH/axis-version/webapps下的axis包拷贝到TOMCAT_HOME/webapps/下,
以下约定AXIS_HOME为该TOMCAT_HOME/webapps/axis目录
4.启动tomcat,访问http://localhost:8080/axis检查安装是否成功
5.以上步骤执行成功,可以开发webservice例子了

axis支持三种webservice的部署和开发,分别为:
1。DynamicInvocationInterface(DII)
2。Stubs方式
3。DynamicProxy方式

以下逐一讲述webservice在axis上的部署和开发,设置classpath
setCLASSPATH=.;%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(DynamicInvocationInterface)方式web服务
1.编写服务端程序HelloClient
publicclassHelloClient{
publicStringgetName(Stringname)
{
return"hello"+name;
}
}

2.将源码拷贝到AXIS_HOME下,重命名为HelloClient.jws
3.访问连接http://localhost:8080/axis/HelloClient.jws?wsdl,页面显示axis自动生成的wsdl
4.编写访问服务的客户端TestHelloClient.java

importorg.apache.axis.client.Call;
importorg.apache.axis.client.Service;
importjavax.xml.namespace.QName;
importjavax.xml.rpc.ServiceException;
importjava.net.MalformedURLException;
importjava.rmi.RemoteException;

publicclassSayHelloClient2{
publicstaticvoidmain(String[]args){
try{
Stringendpoint="http://localhost:8080/axis/HelloClient.jws";

Serviceservice=newService();
Callcall=null;

call=(Call)service.createCall();

call.setOperationName(newQName(
"http://localhost:8080/axis/HelloClient.jws","getName"));
call.setTargetEndpointAddress(newjava.net.URL(endpoint));

Stringret=(String)call.invoke(newObject[]{"zhangsan"});
System.out.println("returnvalueis"+ret);
}catch(Exceptionex){
ex.printStackTrace();
}
}
}
三、编写DynamicProxy方式访问服务
1.编写部署服务端程序,同上边DII方式,本次仍使用上边部署的HelloClient
2.编写代理接口
publicinterfaceHelloClientInterfaceextendsjava.rmi.Remote{
publicStringgetName(Stringname)throwsjava.rmi.RemoteException;
}
3.编写并执行客户端程序TestHelloClient.java

importjavax.xml.rpc.Service;
importjavax.xml.rpc.ServiceFactory;
importjava.net.URL;
importjavax.xml.namespace.QName;

publicclassTestHelloClient{
publicstaticvoidmain(String[]args){
try
{
StringwsdlUrl="http://localhost:8080/axis/HelloClient.jws?wsdl";
StringnameSpaceUri="http://localhost:8080/axis/HelloClient.jws";
StringserviceName="HelloClientService";
StringportName="HelloClient";

ServiceFactoryserviceFactory=ServiceFactory.newInstance();
ServiceafService=serviceFactory.createService(newURL(wsdlUrl),
newQName(nameSpaceUri,serviceName));
HelloClientInterfaceproxy=(HelloClientInterface)
afService.getPort(newQName(
nameSpaceUri,portName),HelloClientInterface.class);
System.out.println("returnvalueis"+proxy.getName("john"));
}catch(Exceptionex)
{
ex.printStackTrace();
}
}
}

四、编写wsdd发布web服务,编写stubclient访问web服务

1.编写服务端程序server,SayHello.java,编译server.SayHello.java
packageserver;
publicclassSayHello{
publicStringgetName(Stringname)
{
return"hello"+name;
}
}
2.编写LogHandler.java
importorg.apache.axis.AxisFault;
importorg.apache.axis.Handler;
importorg.apache.axis.MessageContext;
importorg.apache.axis.handlers.BasicHandler;

importjava.util.Date;

publicclassLogHandlerextendsBasicHandler{
publicvoidinvoke(MessageContextmsgContext)throwsAxisFault
{
/**Loganaccesseachtimewegetinvoked.
*/
try{
HandlerserviceHandler=msgContext.getService();

IntegernumAccesses=
(Integer)serviceHandler.getOption("accesses");
if(numAccesses==null)
numAccesses=newInteger(0);

numAccesses=newInteger(numAccesses.intValue()+1);

Datedate=newDate();
Stringresult=date+":service"+
msgContext.getTargetService()+
"accessed"+numAccesses+"time(s).";
serviceHandler.setOption("accesses",numAccesses);

System.out.println(result);
}catch(Exceptione){
throwAxisFault.makeFault(e);
}
}
}
3..编写wsdd文件
deploy.wsdd
<deploymentxmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

<handlername="print"type="java:LogHandler"/>
<servicename="sayhello"provider="java:RPC">
<requestFlow>
<handlertype="print"/>
</requestFlow>
<parametername="className"value="server.SayHello"/>
<parametername="allowedMethods"value="*"/>
</service>
</deployment>

3.将编译后的文件拷贝到AXIS_HOME/WEB-INF/classes下,如:D:/tomcat/webapps/axis/WEB-INF/classes
4.发布服务:
javaorg.apache.axis.client.AdminClientdeploy.wsdd
5.生成clientstub文件
a:方式1
将SayHello.java拷贝到AXIS_HOME/下,重命名为SayHello.jws,执行下面的命令生存clientstub
javaorg.apache.axis.wsdl.WSDL2Java-pclienthttp://localhost:8080/axis/services/SayHello.jws?wsdl
b:方式2
执行如下命令生成SayHello.wsdl
javaorg.apache.axis.wsdl.Java2WSDL-oSayHello.wsdl-lhttp://localhost:8080/axis/services/SayHello-nsayhelloserver.SayHello
执行如下命令生成clientstub
javaorg.apache.axis.wsdl.WSDL2JavaSayHello.wsdl-pclient
生成的stubclient文件列表为:
1。SayHello.java
2。SayHelloService.java。
3。SayHelloServiceLocator.java
4。SayHelloSoapBindingStub.java
6.编写客户端程序,编译并执行
publicclassSayHelloClient{
publicstaticvoidmain(String[]args){
try{

SayHelloServiceservice=newclient.
SayHelloServiceLocator();
client.SayHello_PortTypeclient=service.getSayHello();
StringretValue=client.getName("zhangsan");
System.out.println(retValue);


}catch(Exceptione){
System.err.println("Executionfailed.Exception:"+e);
}
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics