中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > J2EE
只需3步把您的Java程序转换为Web服务
作者:Jackie 时间:2006-09-28 11:34 出处:ccidnet.com 责编:月夜寒箫
              摘要:只需3步把您的Java程序转换为Web服务

1、选择要转换的Java文件,生成class

2、写wsdd

3、发布

剩下的就只有调用了wsdl2Java

我原来的系统是CICS的,对后台封装了一层,现在用webservice再封装一层,前台页面,控制,数据传输,数据处理统统都可以分开了。

一、Axis安装 1、环境 J2SE SDK 1.3 or 1.4: 我使用 1.4.2 Servlet Container: 我使用的Tomcat 5.0

二、到 http://ws.apache.org/Axis/网站下载Axis安装包

三、解压缩安装包,将Axis_UNZIP_PATH\Axis-version\webapps下的Axis包拷贝到TOM

CAT_HOME\webapps\下,以下约定Axis_HOME为该TOMCAT_HOME\webapps\Axis目录

四、启动tomcat,访问http://localhost:8080/Axis 检查安装是否成功

五、以上步骤执行成功,可以开发webservice例子了

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.
            */
            try {
            Handler serviceHandler
            = msgContext.getService();
            Integer numAccesses =
            (Integer)serviceHandler.getOption("accesses");
            if (numAccesses == null)
            numAccesses = new Integer(0);
            numAccesses = new Integer
            (numAccesses.intValue() + 1);
            Date date = new Date();
            String result =
            date + ": service " +
            msgContext.getTargetService() +
            " accessed " + numAccesses + " time(s).";
            serviceHandler.setOption
            ("accesses", numAccesses);
            System.out.println(result);
            } catch (Exception e)
            {
            throw AxisFault.makeFault(e);
            }
            }
            }

3、编写wsdd文件

 

deploy.wsdd
            <deployment xmlns=
            "http://xml.apache.org/Axis/wsdd/"
            xmlns:Java=
            "http://xml.apache.org
            /Axis/wsdd/providers/Java">
            <handler name="print"
            type="Java:LogHandler"/>
            <service name="sayhello"
            provider="Java:RPC">
            <requestFlow>
            <handler type="print"/>
            </requestFlow>
            <parameter name="className"
            value="server.SayHello"/>
            <parameter name="allowedMethods"
            value="*"/>
            </service>
            </deployment>

3、将编译后的文件拷贝到Axis_HOME/WEB-INF/classes下,如:D:\tomcat\webapps\Axis\WEB-INF\classes

4、发布服务:

Java org.apache.Axis.client.AdminClient deploy.wsdd

5、生成client stub文件

a:方式1

将SayHello.Java拷贝到Axis_HOME/下,重命名为SayHello.jws,

执行下面的命令生存client stub

 

Java org.apache.Axis.wsdl.WSDL2Java
            -p client  http://localhost:8080
            /Axis/services/SayHello.jws wsdl

b:方式2

执行如下命令生成SayHello.wsdl

 

Java org.apache.Axis.wsdl.Java2WSDL
            -oSayHello.wsdl -lhttp://localhost:8080
            /Axis/services/SayHello -nsayhello server.SayHello

执行如下命令生成client stub

 

Java org.apache.Axis.wsdl.WSDL2Java
            SayHello.wsdl  -p client

生成的stub client文件列表为:

1.SayHello.Java

2.SayHelloService.Java。

3.SayHelloServiceLocator.Java

4.SayHelloSoapBindingStub.Java

6、编写客户端程序,编译并执行

 

public class SayHelloClient
            {
            public static void main(String[] args)
            {
            try
            {
            SayHelloService service = new client.
            SayHelloServiceLocator();
            client.SayHello_PortType
            client = service.getSayHello();
            String retValue=client.getName("zhangsan");
            System.out.println(retValue);
            }
            catch (Exception e)
            {
            System.err.println
            ("Execution failed. Exception: " + e);
            }
            }
            }

您也可以写server-config.wsdd

 

<?xml version="1.0" encoding="UTF-8"?>
            <deployment xmlns=
            "http://xml.apache.org/axis/wsdd/"
            xmlns:Java=
            "http://xml.apache.org/axis
            /wsdd/providers/Java">
            <globalConfiguration>
            <parameter name=
            "adminPassword" value="admin"/>
            <parameter name=
            "attachments.implementation"
            value=
            "org.apache.axis.attachments.AttachmentsImpl"/>
            <parameter name=
            "sendXsiTypes" value="true"/>
            <parameter name=
            "sendMultiRefs" value="true"/>
            <parameter name=
            "sendXMLDeclaration" value="true"/>
            <parameter name=
            "axis.sendMinimizedElements" value="true"/>
            <requestFlow>
            <handler type=
            "Java:org.apache.axis.handlers.JWSHandler">
            <parameter name=
            "scope" value="session"/>
            </handler>
            <handler type=
            "Java:org.apache.axis.handlers.JWSHandler">
            <parameter name="scope"
            value="request"/>
            <parameter name="extension"
            value=".jwr"/>
            </handler>
            </requestFlow>
            </globalConfiguration>
            <handler name="LocalResponder"
            type="Java:org.apache.axis.transport.
            local.LocalResponder"/>
            <handler name="URLMapper"
            type="Java:org.apache.axis.handlers.
            http.URLMapper"/>
            <handler name="Authenticate"
            type="Java:org.apache.axis.handlers.
            SimpleAuthenticationHandler"/>
            <handler name="print"
            type="Java:stub.LogHandler"/>
            <service name="sayhello"
            provider="Java:RPC">
            <requestFlow>
            <handler type="print"/>
            </requestFlow>
            <parameter name="className"
            value="stub.server.SayHello"/>
            <parameter name="allowedMethods"
            value="*"/>
            </service>
            <transport name="http">
            <requestFlow>
            <handler type="URLMapper"/>
            <handler type="Java:org.apache.axis.
            handlers.http.HTTPAuthHandler"/>
            </requestFlow>
            </transport>
            <transport name="local">
            <responseFlow>
            <handler type="LocalResponder"/>
            </responseFlow>
            </transport>
            </deployment>
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有