手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>Java技术>列表

简单的webservice开发例子

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
Axis支持三种web service的部署和开发,分别为:

1、Dynamic Invocation Interface ( DII)

2、Stubs方式

3、Dynamic Proxy方式

二、编写DII(Dynamic Invocation Interface )方式web服务

1.编写服务端程序HelloClient


public class HelloClient
{
public String getName(String name)
{
return "hello " name;
}
}




2、将源码拷贝到Axis_HOME下,重命名为 HelloClient.jws

3、访问连接http://localhost:8080/Axis/HelloClient.jws?wsdl,页面显示Axis自动生成的wsdl

4、编写访问服务的客户端 TestHelloClient.java


import org.apache.Axis.client.Call;
import org.apache.Axis.client.Service;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;

public class SayHelloClient2
{
public static void main(String[] args)
{
try
{
String endpoint =
"http://localhost:8080/Axis/HelloClient.jws";

Service service = new Service();
Call call = null;

call = (Call) service.createCall();

call.setOperationName(new QName(
"http://localhost:8080/Axis/HelloClient.jws",
"getName"));
call.setTargetEndpointAddress
(new java.net.URL(endpoint));

String ret = (String) call.invoke(new Object[]
{"zhangsan"});
System.out.println("return value is " ret);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}




三、编写Dynamic Proxy方式访问服务

1、编写部署服务端程序,同上边DII方式,本次仍使用上边部署的HelloClient

2、编写代理接口


public interface HelloClientInterface
extends java.rmi.Remote
{
public String getName(String name)
throws java.rmi.RemoteException;
}




3、编写并执行客户端程序TestHelloClient.java


import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import java.net.URL;
import javax.xml.namespace.QName;

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发布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.
*/

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!