Terry.Li-彬

虚其心,可解天下之问;专其心,可治天下之学;静其心,可悟天下之理;恒其心,可成天下之业。

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  143 随笔 :: 344 文章 :: 130 评论 :: 0 Trackbacks

一、对象参数的传递

首先是我们要传输的变量对象Order,代码如下:

package samples.userguide.example5;
public class Order
{
    private String customerName;

    private String shippingAddress;
    
    public String getCustomerName()
    { return customerName; }
    public void setCustomerName(String name)
    { customerName = name; }
}

要发布的services的代码如下:

package samples.userguide.example5;

public class BeanService
{
    public Order getNewOrder(Order order)
    {
        order.setCustomerName("liuyujun");
        return order;
    }

}

然后我们只要在配置文件server-config.wsdd里加入下面这段就可以发布了

<service name="OrderProcessor" provider="java:RPC">
        <parameter name="className" value="samples.userguide.example5.BeanService"/>
        <parameter name="allowedMethods" value="*"/>
        <beanMapping qname="myNS:Order" xmlns:myNS="urn:BeanService" languageSpecificType="java:samples.userguide.example5.Order"/>
    </service>

特别要注意多了beanMapping这部分,qname指明我们用自己命名空间里的哪个对象,xmlns:myNS则指明了我们的命名空间,后面还一个指明了用到的对象类。

客户端调用程序:

package samples.userguide.example5;

public class Client
{

    private final static String targetEndpointAddress = "http://localhost:8080/axis/services/OrderProcessor";
    private final static QName    qn      = new QName( "urn:BeanService", "Order" );


    public static void main(String [] args) throws Exception
    {

//创建一个参数对象
        Order order = new Order();
        order.setCustomerName("Glen Daniels");


        Service service = new Service();
        Call call = (Call) service.createCall();

//注册对象类型
        call.registerTypeMapping(Order.class, qn,
                                 new org.apache.axis.encoding.ser.
                                 BeanSerializerFactory(Order.class, qn),
                                 new org.apache.axis.encoding.ser.
                                 BeanDeserializerFactory(Order.class, qn));
        call.setTargetEndpointAddress(new java.net.URL(targetEndpointAddress));


        call.setOperationName(new QName("OrderProcessor", "getNewOrder"));
            call.addParameter("order", qn, ParameterMode.IN);
            call.setReturnType(qn, Order.class);
         Order   newOrder = (Order) call.invoke(new Object[] {order});
        System.out.println("new custname:"+newOrder.getCustomerName());
    }
}
这样就能看到返回的效果了;

 二、获得访问者信息

MessageContext context = MessageContext.getCurrentContext();
        HttpServletRequest req = (HttpServletRequest) context.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);

这样就可以得到ruquest对象获得相关信息了,当然request的有些功能是用不了的,比如,getSession就是无效的


posted on 2007-11-24 14:46 礼物 阅读(594) 评论(0)  编辑  收藏 所属分类: webservice