zhou363667565 发表于 2013-2-3 14:04:39

web service中Handler的高级应用

1.编写服务器端代码:
package pack.java.web.service.handler;import org.apache.axis.AxisFault;import org.apache.axis.MessageContext;import org.apache.axis.handlers.BasicHandler;/*** * 服务器端代码; * 继承basicHandler 抽象类,并且实现invoke方法; * @author Administrator * */public class MyHandler extends BasicHandler{//版本号;private static final long serialVersionUID = 1453729851511344780L;private static longCOUNT =0L;private int requestCount = 0;//请求,调用;public void invoke(MessageContext arg0) throws AxisFault {// TODO Auto-generated method stubrequestCount++;COUNT++;String status = (String)this.getOption("status");System.out.println("My Handler's status is:"+status+",count="+COUNT+",requestCount="+this.requestCount);}} 
2.编写myHandler.dess文件.
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"><handler name="MyHandler" type="java:pack.java.web.service.handler.MyHandler"><parameter name="status" value="success" /></handler><service name="MyHandlerWSDD" provider="java:RPC"><!-- Handler配置之前处理 --><requestFlow><handler type="MyHandler" /></requestFlow><!-- 服务器的包和类,方法,范围范围配置. --><parameter name="className" value="pack.java.web.service.WebServiceWSDD" /> <parameter name="allowedMethods" value="*"/> <parameter name="scope" value="request"/><!-- 通过 <requestFlow> 这个标签,引用到 WebServiceWSDD中,来处理.--><requestFlow><handler type="MyHandler"/></requestFlow></service></deployment> 
3.拷贝myHandler.wsdd文件到Tomcat目录下的axis的Web-INF中,
 
 
4.发布web service.
F:\apache-tomcat-6.0.20\webapps\axis\WEB-INF>java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -l http://localhost:9999/axis/services/MyHandlerWSDD myHandler.wsdd
 
发布结果:
Processing file myHandler.wsdd
<Admin>Done processing</Admin>
 
5.在浏览器中输入:获得url.
http://localhost:9999/axis/services/MyHandlerWSDD
浏览器中显示结果:
MyHandlerWSDD

Hi there, this is an AXIS service!
Perhaps there will be a form for invoking the service here...
 
5.编写客户端代码,
package pack.java.web.service.client.handler;import java.rmi.RemoteException;import javax.xml.namespace.QName;import javax.xml.rpc.ServiceException;import org.apache.axis.client.Call;import org.apache.axis.client.Service;/*** * 客户端;调用服务器端的方法; * @author Administrator * */public class HandlerClientTest {/** * 测试主方法; * @param args */public static void main(String[]args){String url="http://localhost:9999/axis/services/MyHandlerWSDD";Service service=new Service();try {Call call=(Call) service.createCall();call.setTargetEndpointAddress(url);call.setOperationName(new QName(url,"hello"));try {String result=(String) call.invoke(new Object[]{"Peng Xiao Ting ",519});System.out.println(result);} catch (RemoteException e) {// TODO Auto-generated catch blocke.printStackTrace();}} catch (ServiceException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} 
 
调用hello方法时,在控制台得出的结果:
Peng Xiao Ting 520。
 
服务器端返回的结果:
 
- Unable to find config file.  Creating new servlet engine config file: /WEB-INF/server-config.wsdd
My Handler's status is:success,count=1,requestCount=1
this is my hello method.
a=Peng Xiao Ting
b=520
result=520
My Handler's status is:success,count=2,requestCount=2
this is my hello method.
a=Peng Xiao Ting
b=520
result=520
My Handler's status is:success,count=3,requestCount=3
this is my hello method.
a=Peng Xiao Ting
b=519
result=519 
页: [1]
查看完整版本: web service中Handler的高级应用