程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 只需3步把您的Java程序轉換為Web服務

只需3步把您的Java程序轉換為Web服務

編輯:關於JAVA

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)

http://www.mscto.com

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自 http://www.mscto.com

動生成的wsdl

http://www.mscto.com

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、編寫代理接口









http://www.mscto.com

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> 

http://www.mscto.com

3、將編譯後的文件拷貝到Axis_HOME/WEB-INF/classes下,如:D:\tomcat\webaPPS\Axis\WEB-INF\classes http://www.mscto.com

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文件列表為: http://www.mscto.com

1.SayHello.Java

2.SayHelloService.Java。

http://www.mscto.com

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);

            }

            }

            } http://www.mscto.com 

您也可以寫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>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved