什么是AXIS2就不介绍了,主要介绍的是如何利用AXIS2快速的实现一个Web Service。本文以实例说明了一个Web Service的创建、部署褐调用的过程。
本文使用的AXIS2的版本是1.1.1,Tomcat版本是6.0.10,JDK版本是1.5,操作系统WinXP SP2,IE浏览器IE6 SP2。
一、服务Java Bean的编写,以下是java代码
1 package cn.net.ynst;
2
3 import cn.net.ynst.nativecode.JavaDLL;
4 import cn.net.ynst.nativecode.Service;
5 import cn.net.ynst.nativecode.Services;
6
7 import org.apache.axiom.om.OMAbstractFactory;
8 import org.apache.axiom.om.OMElement;
9 import org.apache.axiom.om.OMNamespace;
10 import org.apache.axiom.om.OMFactory;
11
12 public class NativeService {
13
14 static {
15 System.loadLibrary("JavaDLL");
16 }
17
18 public String getWindowsDirectory() {
19 return JavaDLL.getWindowsDirectory();
20 }
21
22 public String getSystemDirectory() {
23 return JavaDLL.getSystemDirectory();
24 }
25
26 public String getMoudlePath() {
27 return JavaDLL.getMoudlePath();
28 }
29
30 public String getCurrentDirectory() {
31 return JavaDLL.getCurrentDirectory();
32 }
33
34 public int getWindowsMajorVersion() {
35 return JavaDLL.getWindowsMajorVersion();
36 }
37
38 public int getWindowsMinorVersion() {
39 return JavaDLL.getWindowsMinorVersion();
40 }
41
42 public int getWindowsBuilderNo() {
43 return JavaDLL.getWindowsBuilderNo();
44 }
45
46 public int getWindowsPlatformId() {
47 return JavaDLL.getWindowsPlatformId();
48 }
49
50 public String getWindowsVersion() {
51 StringBuffer sb = new StringBuffer();
52 JavaDLL.getWindowsVersion(sb);
53
54 return sb.toString();
55 }
56
57 public String getWindowsVersionA() {
58 return JavaDLL.getWindowsVersionA();
59 }
60
61 public String getWindowsVersionB() {
62 return JavaDLL.getWindowsVersionB();
63 }
64
65 public String getEnvironmentVar(String envName) {
66 return JavaDLL.getEnvironmentVar(envName);
67 }
68
69 public String getWindowsSystemInfo() {
70 StringBuffer sb = new StringBuffer();
71 JavaDLL.getWindowsSystemInfo(sb);
72
73 return sb.toString();
74 }
75
76 public String getEnvironmentStrings() {
77 StringBuffer sb = new StringBuffer();
78 JavaDLL.getEnvironmentStrings(sb);
79
80 return sb.toString();
81 }
82
83 public String getVersion() {
84 return JavaDLL.getVersion();
85 }
86
87 public String getComputerName() {
88 return JavaDLL.getComputerName();
89 }
90
91 public String getUserName() {
92 return JavaDLL.getUserName();
93 }
94
95 public String getProcessList() {
96 StringBuffer sb = new StringBuffer();
97 JavaDLL.getProcessList(sb);
98
99 return sb.toString();
100 }
101
102 public String getServiceList() {
103 Services services = new Services();
104 JavaDLL.getServiceList(services);
105
106 Service[] array = services.getServices();
107 StringBuffer sb = new StringBuffer();
108 for(int i=0; i<array.length; i++) {
109 Service service = array[i];
110 sb.append(service);
111 sb.append("<BR>");
112 }
113
114 return sb.toString();
115 }
116
117 public OMElement getUser(OMElement in_name, OMElement in_pass) {
118 OMFactory fac = OMAbstractFactory.getOMFactory();
119 OMNamespace omNs = fac.createOMNamespace("http://ynst.net.cn/xsd", "ns1");
120
121 OMElement user = fac.createOMElement("user", omNs);
122 OMElement name = fac.createOMElement("name", omNs);
123 name.setText("jacky_zz");
124 if(in_name != null) name.setText(in_name.getText());
125
126 OMElement pass = fac.createOMElement("pass", omNs);
127 pass.setText("123456");
128 if(in_pass != null) pass.setText(in_pass.getText());
129
130 user.addChild(name);
131 user.addChild(pass);
132
133 return user;
134 }
135 }二、Web服务描述文件
1 <service name="NativeService">
2 <description>
3 This service is to invoke native code
4 </description>
5 <parameter name="ServiceClass" locked="false">cn.net.ynst.NativeService</parameter>
6 <operation name="getWindowsDirectory">
7 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
8 </operation>
9 <operation name="getSystemDirectory">
10 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
11 </operation>
12 <operation name="getMoudlePath">
13 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
14 </operation>
15 <operation name="getCurrentDirectory">
16 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
17 </operation>
18 <operation name="getWindowsMajorVersion">
19 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
20 </operation>
21 <operation name="getWindowsMinorVersion">
22 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
23 </operation>
24 <operation name="getWindowsBuilderNo">
25 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
26 </operation>
27 <operation name="getWindowsPlatformId">
28 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
29 </operation>
30 <operation name="getWindowsVersion">
31 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
32 </operation>
33 <operation name="getWindowsVersionA">
34 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
35 </operation>
36 <operation name="getWindowsVersionB">
37 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
38 </operation>
39 <operation name="getWindowsSystemInfo">
40 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
41 </operation>
42 <operation name="getEnvironmentStrings">
43 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
44 </operation>
45 <operation name="getVersion">
46 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
47 </operation>
48 <operation name="getComputerName">
49 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
50 </operation>
51 <operation name="getUserName">
52 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
53 </operation>
54 <operation name="getProcessList">
55 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
56 </operation>
57 <operation name="getServiceList">
58 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
59 </operation>
60 <operation name="getEnvironmentVar">
61 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
62 </operation>
63 <operation name="getUser">
64 <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
65 </operation>
66 </service>三、文件打包
将类文件和Web服务描述文件打包为aar文件
四、部署
将aar文件复制到<TOMCAT>/webapps/axis2/WEB-INF/services目录下,并修改文件services.list,将aar文件名复制到该文件并保存。
五、浏览器调用
1 <html>
2 <head>
3 <style>
4 hr {height:1px; color:silver}
5 body {font-size:12px; font-family:Tahoma}
6 </style>
7 <script language="javascript">
8 var url="http://localhost:8080/axis2/services/NativeService";
9 var url_wsdl="http://localhost:8080/axis2/services/NativeService?wsdl";
10
11 function User(username, password) {
12 this.username = username;
13 this.password = password;
14
15 this.parse = this.Parse = function(userNode) {
16 if(userNode == null) return;
17 if(!userNode.childNodes) return;
18 if(!userNode.childNodes.length) return;
19 if(userNode.childNodes.length==0) return;
20
21 for(var i=0;i<userNode.childNodes.length;i++) {
22 var node=userNode.childNodes[i];
23 if(!node.nodeName) continue;
24
25 var nodeName = node.nodeName.toLowerCase();
26 var nodeValue = (node.firstChild!=null?node.firstChild.nodeValue:"");
27
28 if(nodeName.indexOf("name") > 0) {
29 this.username = nodeValue;
30 }
31 else if(nodeName.indexOf("pass") > 0) {
32 this.password = nodeValue;
33 }
34 }
35 };
36
37 this.getUsername = this.GetUsername = function() {
38 return this.username;
39 };
40
41 this.setUsername = this.SetUsername = function(username) {
42 this.username = username;
43 };
44
45 this.getPassword = this.GetPassword = function() {
46 return this.password;
47 };
48
49 this.setPassword = this.SetPassword = function(password) {
50 this.password = password;
51 };
52
53 this.toString = this.ToString = function() {
54 return "Username:"+this.username+", Password:"+this.password;
55 };
56
57 this.getXML = this.GetXML = function(prefix) {
58 if(prefix==null) prefix="ns1";
59 var xml = "<"+prefix+":name>"+this.username+"</"+prefix+":name>";
60 xml+= "<"+prefix+":pass>"+this.password+"</"+prefix+":pass>";
61
62 return xml;
63 };
64
65 return this;
66 }
67
68 function invoke() {
69 var sMethodList="getUser;getEnvironmentVar;getWindowsDirectory;getSystemDirectory;getMoudlePath;getCurrentDirectory;getWindowsMajorVersion;";
70 sMethodList+="getWindowsMinorVersion;getWindowsBuilderNo;getWindowsPlatformId;getWindowsVersion;";
71 sMethodList+="getWindowsVersionA;getWindowsVersionB;getWindowsSystemInfo;getEnvironmentStrings;";
72 sMethodList+="getVersion;getComputerName;getUserName;getProcessList;getServiceList";
73
74 var aMethodPara = new Array();
75 aMethodPara[aMethodPara.length] = (new User("李鑫", "jacky@163.com")).getXML("ns1");
76 aMethodPara[aMethodPara.length] = "<ns1:xx>PATH</ns1:xx>";
77 aMethodPara[aMethodPara.length] = "";
78 aMethodPara[aMethodPara.length] = "";
79 aMethodPara[aMethodPara.length] = "";
80 aMethodPara[aMethodPara.length] = "";
81 aMethodPara[aMethodPara.length] = "";
82 aMethodPara[aMethodPara.length] = "";
83 aMethodPara[aMethodPara.length] = "";
84 aMethodPara[aMethodPara.length] = "";
85 aMethodPara[aMethodPara.length] = "";
86 aMethodPara[aMethodPara.length] = "";
87 aMethodPara[aMethodPara.length] = "";
88 aMethodPara[aMethodPara.length] = "";
89 aMethodPara[aMethodPara.length] = "";
90 aMethodPara[aMethodPara.length] = "";
91 aMethodPara[aMethodPara.length] = "";
92 aMethodPara[aMethodPara.length] = "";
93 aMethodPara[aMethodPara.length] = "";
94 aMethodPara[aMethodPara.length] = "";
95
96 var aMethodList=sMethodList.split(";");
97 var request="<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header/><soapenv:Body><ns1:{MethodName} xmlns:ns1=\"http://ynst.net.cn/xsd\">{MethodPara}</ns1:{MethodName}></soapenv:Body></soapenv:Envelope>";
98
99 var str="";
100 var methodRegExp=new RegExp("{MethodName}", "gi");
101 var paramRegExp=new RegExp("{MethodPara}", "gi");
102 var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
103
104 for(var i=0;i<aMethodList.length;i++) {
105 var req=request.replace(methodRegExp, aMethodList[i]);
106 var req=req.replace(paramRegExp,aMethodPara[i]);
107
108 var time1=(new Date()).getTime();
109 var ret=fnRemoteCall(url, req);
110 var time2=(new Date()).getTime();
111 if(ret==null) continue;
112
113 xmlDoc.loadXML(ret);
114 //可以使用xmlDoc.text获取文本信息
115 var returnNode=getNode(xmlDoc, "return");
116 if(returnNode) {
117 var valueNode=returnNode.firstChild;
118 if(valueNode) {
119 var nodeValue=valueNode.nodeValue;
120 if(nodeValue==null) {
121 var userNode=getNode(returnNode, "user");
122 var user = new User();
123 user.parse(userNode);
124 nodeValue = user.toString();
125 }
126
127 var xxx="<li><font color='blue'>"+aMethodList[i]+"</font>:(<font color='red'>"+(time2-time1)+"</font>ms)<BR>"+nodeValue+"</li>";
128 str+=(str=="")?xxx:"<BR><hR>"+xxx;
129 }
130 }
131 }
132
133 xx.innerHTML=str;
134 }
135
136 function wsdl() {
137 var time1=(new Date()).getTime();
138 var requestHttp=false;
139 if(window.XMLHttpRequest) {
140 requestHttp=new XMLHttpRequest();
141
142 //检测是否支持跨域访问
143 try {
144 requestHttp.open("GET", url_wsdl,false);
145 } catch(e1) {
146 try {
147 requestHttp=new ActiveXObject("MSXML2.XMLHTTP");
148 } catch(e2) {
149 try {
150 requestHttp=new ActiveXObject("Microsoft.XMLHTTP");
151 } catch(e3) {
152 requestHttp=false;
153 }
154 }
155 }
156 }
157 else {
158 try {
159 requestHttp=new ActiveXObject("MSXML2.XMLHTTP");
160 } catch(e1) {
161 try {
162 requestHttp=new ActiveXObject("Microsoft.XMLHTTP");
163 } catch(e2) {
164 requestHttp=false;
165 }
166 }
167 }
168
169 if(!requestHttp) return;
170
171 var sRet = "";
172 try {
173 requestHttp.open("GET",url_wsdl,false);
174 requestHttp.setRequestheader("content-type","text/html; charset=UTF-8");
175 requestHttp.setRequestheader("content-length",0);
176 requestHttp.send("");
177
178 var sRet = requestHttp.responseText;
179 if(sRet==null) return;
180 }
181 catch (e) {
182 if(e.description) alert(e.description);
183 else alert(e);
184
185 return;
186 }
187
188 var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
189 xmlDoc.loadXML(sRet);
190
191 var length=xmlDoc.childNodes.length;
192 for(var i=0;i<length;i++) {
193 walkNode(xmlDoc.childNodes[i]);
194 }
195
196 var time2=(new Date()).getTime();
197 document.write("<br>invoke time:"+(time2-time1)+"ms");
198 }
199
200 function walkNode(node) {
201 if(node == null) return;
202
203 /*
204 switch(node.nodeType) {
205 case 1: //ELEMENT
206 document.write("Node:"+node.nodeName+"<br>");
207 break;
208 case 2: //ATTRIBUTE
209 case 3: //TEXT
210 document.write("NodeValue:"+node.nodeValue+"<br>");
211 break;
212 case 4: //CDATA_SECTION
213 case 5: //ENTITY_REFERENCE
214 case 6: //ENTITY
215 case 7: //PROCESSING_INSTRUCTION
216 case 8: //COMMENT
217 case 9: //DOCUMENT
218 case 10: //DOCUMENT_TYPE
219 case 11: //DOCUMENT_FRAGMENT
220 case 12: //NOTATION
221 default:
222 break
223 }
224 */
225
226 switch(node.nodeType) {
227 case 1: //ELEMENT
228 document.write("<dl><font color='blue'><</font><font color='blue' style='font-weight:bold'>"+node.nodeName+"</font>");
229 if(node.attributes) {
230 if(node.attributes.length) {
231 var count = node.attributes.length;
232 for(var i=0;i<count;i++) {
233 var attribute = node.attributes[i];
234 if(attribute) {
235 document.write(" <font color='red'>"+attribute.nodeName+"</font><font color='blue'>=\"</font><font color='fuchsia'>"+attribute.nodeValue+"</font><font color='blue'>\"</font>");
236 }
237 }
238 }
239 }
240
241 var length = node.childNodes.length;
242 if(length==0) {
243 document.write("<font color='blue'>/></font></dl>\r\n");
244 return;
245 }
246
247 document.write("<font color='blue'>></font>\r\n");
248
249 for(var j=0;j<length;j++) {
250 walkNode(node.childNodes[j]);
251 }
252
253 document.write("<font color='blue'></</font><font color='blue' style='font-weight:bold'>"+node.nodeName+"</font><font color='blue'>></font></dl>\r\n");
254 case 2: //ATTRIBUTE
255 //never happen, i guess
256 break;
257 case 3: //TEXT
258 if(node.nodeValue)
259 document.write(node.nodeValue);
260 break;
261 case 4: //CDATA_SECTION
262 break;
263 case 5: //ENTITY_REFERENCE
264 break;
265 case 6: //ENTITY
266 break;
267 case 7: //PROCESSING_INSTRUCTION
268 break;
269 case 8: //COMMENT
270 break;
271 case 9: //DOCUMENT
272 break;
273 case 10: //DOCUMENT_TYPE
274 break;
275 case 11: //DOCUMENT_FRAGMENT
276 break;
277 case 12: //NOTATION
278 break;
279 default:
280 break;
281 }
282 }
283
284 function getNode(node, nodeName) {
285 if(!node) return null;
286 if(!node.nodeName) return null;
287 if(!node.childNodes) return null;
288 if(!node.childNodes.length) return null;
289 if(node.childNodes.length==0) return null;
290
291 var findNode=node;
292 if(findNode.nodeName.toLowerCase().indexOf(nodeName.toLowerCase())>=0 && findNode.nodeName) {
293 return findNode;
294 }
295
296 for(var i=0;i<findNode.childNodes.length;i++) {
297 var node=getNode(findNode.childNodes[i], nodeName);
298 if(node!=null) return node;
299 }
300
301 return null;
302 }
303
304 function fnRemoteCall(sRPCURL,sReuqestContent) {
305 if(sRPCURL==null||sRPCURL=="") {alert("请求的URL不能为空!"); return null;}
306 if(sReuqestContent==null) sReuqestContent="";
307
308 var requestHttp=false;
309 if(window.XMLHttpRequest) {
310 requestHttp=new XMLHttpRequest();
311
312 //检测是否支持跨域访问
313 try {
314 requestHttp.open("POST", sRPCURL,false);
315 } catch(e1) {
316 try {
317 requestHttp=new ActiveXObject("MSXML2.XMLHTTP");
318 } catch(e2) {
319 try {
320 requestHttp=new ActiveXObject("Microsoft.XMLHTTP");
321 } catch(e3) {
322 requestHttp=false;
323 }
324 }
325 }
326 }
327 else {
328 try {
329 requestHttp=new ActiveXObject("MSXML2.XMLHTTP");
330 } catch(e1) {
331 try {
332 requestHttp=new ActiveXObject("Microsoft.XMLHTTP");
333 } catch(e2) {
334 requestHttp=false;
335 }
336 }
337 }
338
339 if(!requestHttp) return null;
340
341 try {
342 requestHttp.open("POST",sRPCURL,false);
343 requestHttp.setRequestheader("content-type","text/xml; charset=UTF-8");
344 requestHttp.setRequestheader("content-length",sReuqestContent.length);
345 requestHttp.send(sReuqestContent);
346
347 var sRet = requestHttp.responseText;
348 if(sRet==null) return null;
349
350 return sRet;
351 }
352 catch (e) {
353 if(e.description) alert(e.description);
354 else alert(e);
355
356 return null;
357 }
358
359 } //------------------------------------------------
360 </script>
361 </head>
362
363 <body>
364 <input type="button" value="Invoke" onclick="invoke()">
365 <input type="button" value="WSDL" onclick="wsdl()")>
366 <br>
367 <span>invoke url:http://localhost:8080/axis2/services/NativeService</span>
368 <div id="xx" name="xx"></div>
369 </body>
370 </html>六、其他
本文实现的Web服务使用了JNI来实现了部分的代码,JNI通过VC++开发实现。
由于不能上传文件,如有需要源码的请通过电子邮件与我联系:jackyxinli@163.com