日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

基于Tomcat5.0和Axis2开发Web Service应用实例

發(fā)布時(shí)間:2024/9/20 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于Tomcat5.0和Axis2开发Web Service应用实例 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本文將介紹如何使用Tomcat5.0Apache Axis2開發(fā)、部署及測(cè)試一個(gè)簡(jiǎn)單的Web Service應(yīng)用。<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

author: ZJ <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />07-3-12

Blog: [url]http://zhangjunhd.blog.51cto.com/[/url]

?

1工作環(huán)境

Eclipse 3.1.2+Lomboz+jdk1.5+ apache-tomcat-5.0.18+AXIS2:1.0(war版本和bin版本)

[url]http://ws.apache.org/axis2/download/1_0/download.cgi[/url]頁面下載AXIS2Binary Distribution url: [url]http://apache.justdn.org/ws/axis2/1_0/axis2-std-1.0-bin.zip[/url]war Distribution url: [url]http://apache.justdn.org/ws/axis2/1_0/axis2-1.0-docs.zip[/url]。把這兩個(gè)文件解壓,比如解壓縮的后得目錄為C:\axis2-std-1.0-binC:\axis2.war。 Eclipse下通過菜單window—preferences…--Java—Build Path—User Libraries 新建一個(gè)user library,比如名字就叫axis2C:\axis2-std-1.0-bin\lib下的所有jar文件包含進(jìn)來。把axis2.war拷貝到%TOMCAT-HOME%/webapps下面。

?

2.檢驗(yàn)安裝

Eclipse下啟動(dòng)Tomcat,在地址欄內(nèi)輸入[url]http://localhost:8080/axis2/[/url]。 點(diǎn)擊Validate,將到達(dá) Axis2 Happiness Page。 3WebService中的HelloWorld

1)新建一個(gè)動(dòng)態(tài)web工程,取名ZZaxis,右鍵點(diǎn)擊項(xiàng)目名,選擇Properties-Java Build Path-Add Library-User Library-axis2。

?

2)新建package sample,建立HelloWorld.java,代碼如下。 HelloWorld.java

import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace;

?

public class HelloWorld { ?????? public OMElement sayHello(OMElement in){ ????????????? String name=in.getText(); ????????????? String info=name+"HelloWorld!"; ????????????? OMFactory fac=OMAbstractFactory.getOMFactory(); ????????????? OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw"); ????????????? OMElement resp=fac.createOMElement("sayHelloResponse",omNs); ????????????? resp.setText(info); ????????????? return resp; ?????? } }

?

3)在WebContent\META-INF\建立services.xml,代碼如下。 services.xml

<?xml version="1.0" encoding="UTF-8"?> <service name="HelloWorld"> <description> ? This is a sample Web Service. </description> <parameter name="ServiceClass" locked="false">sample.HelloWorld</parameter> <operation name="sayHello"> ? <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/> </operation> </service>

?

4)將目錄sample和目錄META-INF組織如下(新建目錄example)。 +-example |-------- +-sample ??? |------- HelloWorld.class |---------+-META-INF ?????? |------- services.xml

?

5)打包生成aar文件。 在命令符環(huán)境下,將當(dāng)前目錄轉(zhuǎn)到example jar cvf HelloWorld.aar . //注意最后一個(gè)點(diǎn),在當(dāng)前目錄下生成HelloWorld.aar。

?

6)在Eclipse中啟動(dòng)Tomcat,在地址欄下鍵入[url]http://localhost:8080/axis2/[/url]。選擇Administration,輸入用戶名admin,密碼axis2。選擇左側(cè)工具欄Tools- Upload Service,上傳之前打包的HelloWorld.aar。該文件將在<CATALINA_HOME>/webapps/axis2\WEB-INF\services目錄下。

?

7)編寫客戶端檢驗(yàn)代碼。新建Java Project,取名為ZZaxisClient。右鍵點(diǎn)擊項(xiàng)目名,選擇Properties-Java Build Path-Add Library-User Library-axis2。

?

8)新建package example.client。建立TestClient.java,代碼如下。 TestClient.java

package example.client;

?

import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace;

import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options;

import org.apache.axis2.client.ServiceClient;

?

public class TestClient { ?????? private static EndpointReference targetEPR=new EndpointReference ?????? ? ("http://localhost:8080/axis2/services/HelloWorld"); ?????? public static OMElement getSayHelloOMElement(){ ????????????? OMFactory fac=OMAbstractFactory.getOMFactory(); ????????????? OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");

????????????? OMElement method=fac.createOMElement("sayHello",omNs); ????????????? method.setText("ZJ"); ????????????? return method; ?????? } ?????? public static void main(String[] args){ ????????????? try{ ???????????????????? Options options=new Options(); ???????????????????? options.setTo(targetEPR); ???????????????????? ServiceClient sender=new ServiceClient(); ???????????????????? sender.setOptions(options); ???????????????????? OMElement sayHello=TestClient.getSayHelloOMElement(); ???????????????????? OMElement result=sender.sendReceive(sayHello); ???????????????????? System.out.println(result); ????????????? } ????????????? catch(Exception axisFault){ ???????????????????? axisFault.printStackTrace(); ????????????? } ?????? } }

?

9)測(cè)試,run TestClient.java as Java Application。結(jié)果:
<hw:sayHelloResponse xmlns:hw="http://helloworld.com/" xmlns:tns="http://ws.apache.org/axis2"> ZJHelloWorld! </hw:sayHelloResponse>

?

4.后續(xù)

詳細(xì)介紹clientserver端代碼?!?/span>基于Tomcat5.0和Axis2開發(fā)Web Service代碼詳解

轉(zhuǎn)載于:https://blog.51cto.com/zhangjunhd/23690

總結(jié)

以上是生活随笔為你收集整理的基于Tomcat5.0和Axis2开发Web Service应用实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。