Chinaunix首页 | 论坛 | 博客
  • 博客访问: 498983
  • 博文数量: 704
  • 博客积分: 39800
  • 博客等级: 大将
  • 技术积分: 4950
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-15 13:32
文章分类

全部博文(704)

文章存档

2011年(1)

2008年(703)

我的朋友

分类:

2008-10-15 13:44:54

    Web Service的客户端实现有三种1. 生成的stub 2. 动态代理3. 动态调用接口其中生成stub是最常用的。stub是用JAX-RPC编译器根据WSDL文档生成的,其主要功能是将对endpoint接口的方法调用转化为SOAP 消息,并且负责将返回的SOAP响应转换为方法的返回值,把SOAP fault转化为方法的异常。JAX-RPC编译器产生的stub除了要实现endpoint接口外,还需要实现或继承 javax.xml.rpc.Stub接口或其实现的子类(Axis中是org.apache.axis.client.Stub)。 javax.xml.rpc.Stub接口主要定义了和网络通讯和认证相关的属性的设置和获取的机制。

    Web Service的客户端实现有三种1. 生成的stub 2. 动态代理3. 动态调用接口其中生成stub是最常用的。stub是用JAX-RPC编译器根据WSDL文档生成的,其主要功能是将对endpoint接口的方法调用转化为SOAP 消息,并且负责将返回的SOAP响应转换为方法的返回值,把SOAP fault转化为方法的异常。JAX-RPC编译器产生的stub除了要实现endpoint接口外,还需要实现或继承 javax.xml.rpc.Stub接口或其实现的子类(Axis中是org.apache.axis.client.Stub)。 javax.xml.rpc.Stub接口主要定义了和网络通讯和认证相关的属性的设置和获取的机制。其代码如下:

package javax.xml.rpc;
import java.util.Iterator;

public interface Stub {

    // Standard property: The Web service's Internet address.
    public static String ENDPOINT_ADDRESS_PROPERTY;
    // Standard property: Password for authentication.
    public static String PASSWORD_PROPERTY;
    // Standard property: User name for authentication.
    public static String USERNAME_PROPERTY;
    // Standard property: Boolean flag for maintaining an HTTP session.
    public static String SESSION_MAINTAIN_PROPERTY;

    // Given a property name, get its value.
    public Object _getProperty(java.lang.String name);
    // Get the names of all the properties the stub supports.
    public Iterator _getPropertyNames();
    // Configure a property on the stub.
    public void _setProperty(java.lang.String name, java.lang.Object value);
}

    JAX-RPC编译器产生还可以产生一个和WSDL中service元素对应的Service接口,该接口组合了多个port,也就是多个Stub.该接口继承了javax.xml.rpc.Service.在J2EE环境中Service接口通常通过JNDI lookup得到。

    在J2EE中使用生成的stub的典型用例如下:代码:

package com.jwsbook.jaxrpc;
import javax.servlet.http.*;
import javax.servlet.*;
import javax.naming.InitialContext;

public class BookQuoteServlet_1 extends javax.servlet.http.HttpServlet {
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException,java.io.IOException {
    try{
      String isbn = req.getParameter("isbn");

      InitialContext jndiContext = new InitialContext();

      BookQuoteService service = (BookQuoteService)
      jndiContext.lookup("java:comp/env/service/BookQuoteService");

      BookQuote bookQuote = service.getBookQuotePort();

      float price = bookQuote.getBookPrice( isbn );

      java.io.Writer outStream = resp.getWriter();
      outStream.write("The wholesale price for ISBN:"+isbn+
                    " = "+price+" ");
    }catch(javax.naming.NamingException ne){
      throw new ServletException(ne);
    }catch(javax.xml.rpc.ServiceException se){
      throw new ServletException(se);
    }
  }

    部署说明文件:


    service/BookQuoteService
    com.jwsbook.jaxrpc.BookQuoteService
    <wsdl-file>BookQuote.wsdlwsdl-file>
    mh:BookQuoteService

[1]   

【责编:Chuan】

--------------------next---------------------

阅读(304) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~