Use Apache axis to implement Web Service (3)

Source: Internet
Author: User

 

3. Use the Web servie Client

After a Web Service is deployed on the server, it can be called by other applications. Calling a Web Service is actually very simple and requires no special settings. For example, we used http: // localhost: 8080/axis/helloworld. JWS? WSDL access is a kind of Web servicede call. Of course, this statement is not strict enough. The method provided by the Web Service is called. We use this method to call: http: // localhost: 8080/axis/helloworld? Method = sayhello & username = Brookes. The following page is displayed:

<? XML version = "1.0" encoding = "UTF-8"?>
<Soapenv: envelope xmlns: soapenv = "http://schemas.xmlsoap.org/soap/envelope/" xmlns: XSD = "http://www.w3.org/2001/XMLSchema" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance">
<Soapenv: Body>
<Sayhelloresponse soapenv: encodingstyle = "http://schemas.xmlsoap.org/soap/encoding/">
<Sayhelloreturn xsi: TYPE = "XSD: string"> Hello Brookes, welcome to my web service. </sayhelloreturn>
</Sayhelloresponse>
</Soapenv: Body>
</Soapenv: envelope>

This is a standard XML response to the SOAP protocol. The key is the <sayhelloreturn> element, which is the response of Web servcie to the sayhello method! We only need to parse this XML file to get the desired response. (For more information, see XMLHttpRequest)

Apache axis supports three ways for clients to access web services:

1. dynamic invocation interface (DiI) dynamic call interface
2. Dynamic proxy
3. stubs Pile

[Note] I have seen that the above three methods are listed as the three "development and deployment methods" of Web servcie. I think this statement is inappropriate. If development involves client development, the deployment seems to be more specific to the server. My understanding is also a family of words. please correct me.

Let's first look at the DiI method. The core code is as follows (we used the previous helloworld Web Service ):

 

Import org. Apache. axis. Client. call;
Import org. Apache. axis. Client. Service;
Import javax. xml. namespace. QNAME;
Public class wstest ...{
Public static void main (string [] ARGs )...{
Try ...{
String endpoint = "http: // localhost: 8080/axis/helloworld. JWS ";
Service = new service ();
Call call = NULL;
Call = (CALL) service. createcall ();
Call. setoperationname (New QNAME ("http: // localhost: 8080/axis/helloworld. JWS", "sayhello "));
Call. settargetendpointaddress (New java.net. URL (endpoint ));
String response = (string) Call. Invoke (new object []... {"Brookes "});
System. Out. println ("return value is:" + response );
} Catch (exception ex)... {ex. printstacktrace ();}
}
}

Run the program and obtain the following output information:

Return Value is: Hello Brookes, welcome to my web service.

It's so easy to access the Web service using DiI!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.