In heterogeneous systems, WebService is often used. In terms of its meaning, WebService is more presented as a standard. Since it is a standard, like other frameworks in the Java World, its products are not unique. The implementation frameworks of WebService include axis, axis2, xxfie, and cxf. This article does not discuss WebService in depth. It just provides a simple example to give you a simple understanding of WebService and a simple access to WebService on the flex and Java clients, this may be a common task in projects.
For axis, it supports two ways to publish the WebService: instant publishing and custom publishing. In this example, the former can be used only because it is simple and can fully achieve the purpose of this Article. However, it should be emphasized that it is not applicable to formal projects. The reason is that real-time release is too simple. Simply put the source code of the service and put it on Tomcat to use it, which leads to its flexibility, once we only have a jar package, but no source code, it will be unavailable. To put it bluntly, first release a WebService Service.
Find webapps \ axis (in the downloaded axis-bin-1_4.zip file, copy the entire axis folder to Tomcat's webapps, and then put the following WebService class helloflextowebservice. JWS under Tomcat \ webapps \ axis.
Public class helloflextowebservice {Public String getreturn (string username) {return "you are accessing WebService," + username ;}}
Run Tomcat and accessHttp: // localhost: 8080/axis/helloflextowebservice. JWS? WSDL, We will find that tomcat has automatically Parsed the above service as a WSDL file for us, simple cut partCodeAs shown in the following figure, we are confident that the corresponding relationship will be established first after comparing the above service classes.
<WSDL: Message name = "getreturnresponse"> <WSDL: part name = "getreturnreturn" type = "XSD: string"/> </WSDL: Message>-<WSDL: message name = "getreturnrequest"> <WSDL: part name = "username" type = "XSD: string"/> </WSDL: Message>-<WSDL: porttype name = "helloflextowebservice">-<WSDL: Operation name = "getreturn" parameterorder = "username"> <WSDL: input message = "impl: getreturnrequest "name =" getreturnrequest "/> <WSDL: Output Message =" impl: getreturnresponse "name =" getreturnresponse "/> </WSDL: Operation> </WSDL: porttype>
This document does not provide much explanation for this WSDL file. In short, it is a service released externally. The client accesses the service through the axis engine to obtain the relevant data. The following is a sample code for a Java client to access this service.
Import Java. RMI. remoteException; import javax. XML. RPC. serviceexception; import javax. XML. namespace. QNAME; import Org. apache. axis. client. call; import Org. apache. axis. client. service; public class wsclient {public static void main (string [] ARGs) {string wsdlurl = "http: // localhost: 8080/axis/helloflextowebservice. JWS? WSDL "; Service = new service (); try {call = (CALL) service. createcall (); call. settargetendpointaddress (wsdlurl); call. setoperationname (New QNAME (wsdlurl, "getreturn"); try {string result = (string) Call. invoke (new object [] {"chenweidong"}); system. out. println (result);} catch (RemoteException e) {e. printstacktrace ();} system. out. println ();} catch (serviceexception e) {e. printstacktrace ();}}}
Compare the following code for using Flex to access the WebService:
<? XML version = "1.0" encoding = "UTF-8"?> <S: Application xmlns: FX = "http://ns.adobe.com/mxml/2009" xmlns: S = "Library: // ns.adobe.com/flex/spark" xmlns: MX = "Library: // ns.adobe.com/flex/mx "minwidth =" 955 "minheight =" 600 "> <FX: declarations> <s: WebService id =" WebService "WSDL =" http: // localhost: 8080/axis/helloflextowebservice. JWS? WSDL "showbusycursor =" true "result =" onLoad (event) "fault =" faulthandler (event) "/> </FX: declarations> <mx: vbox> <mx: textinput id = "cityname" text = "chenweidong"/> <mx: button label = "callwebservice" Click = "callwebservice (event)"/> <mx: textinput id = "myweatherreport" Height = "30" width = "260" editable = "false"/> </MX: vbox> <FX: SCRIPT> <! [CDATA [import MX. controls. alert; import MX. RPC. events. faultevent; import MX. RPC. events. resultevent;/** access WebService Service */protected function callwebservice (Event: mouseevent): void {WebService. getreturn (cityname. text);}/** WebService access succeeded */private function onload (Event: resultevent): void {myweatherreport. TEXT = string (event. result);}/** failed to access WebService */private function faulthandler (Event: faultevent): void {alert. show (event. fault. tostring (), 'webservice error');}]> </FX: SCRIPT> </S: Application>
From the flex and Java client code, we can find that accessing the WebService first locates the service and then accesses the interface provided by the Service to obtain the corresponding data.
This article briefly describes how to publish the WebService and use Java and flex to access the published service. Through this article, we will mainly understand the principles of this WebService and Its diversified implementation frameworks, provide some basic ideas for using it in our work, and understand that different languages are similar in accessing WebService, the reason is that WebService is a standard and there will naturally be no deviation in use. You still need to read the relevant documents for a deep understanding.