Very Simple Object Access Protocol with Javaby Olexiy & Alexander Prokhorenko
Now, let's see what happened in the end. In STEP 1, HelloWorldClient will connect to a SOAP RPC Router, requested our SOAP Service and pass a string that contains our name to it. The SOAP RPC Router checks if it has deployed this SOAP Service. If it is found to be deployed, it will pass data to this SOAP Service and call a specific method, this is STEP 2. The SOAP Service method then will be executed, and a string value will be returned (this value is the answer or response of the SOAP Client) (STEP 3). In STEP4, SOAP RPC Router will only be redirected to this data to the SOAP Client. All data transmitted in STEP1 and STEP4 is done through SOAP Envelope. As you can see, the algorithm is quite simple, so we are only ready to care about the actual code.
First, we have to create a SOAP Service. Here is its code, please don't forget to put it in the HelloWorld / directory (must be included in your classpath):
1: // SOAPSERVICE.JAVA
2: package helloworld;
3: Public class soapservice {
4: Public string sybi (string x) {
5: Return ("Hello My Friend," X "! Glad to see you!");
6:}
7:}
It is also easy to add any comments. To compile it, just need to use the following command:
Javac SOAPSERVICE.JAVA
In the second step, once we are ready to SOAP Service, we need to deploy it with SOAP Service Manager. This can be implemented in many ways, but in order to make the readers of the beginner SOAP easier to understand SOAP, I have provided a easier way. We assume that your web server (Tomcat or other) is running normally and you have installed SOAP correctly. Then when the browser accesses http: // localhost: 8080 / soap /, you will see the welcome page of Apache SOAP. Click Run The Admin Client and then deploy. You will get a screen display, where you need to fill in ID, Scope, Method, Provider Type, and Java Provider information to the form field. You can ignore all other form fields unless you really need them. Our "HelloWorld" example does not need them, so we fill the following value:
ID: URN: HelloWorld_SoapServicescope: ApplicationMethods: SayhiProvider Type: Javajava Provider - Provider Class: HelloWorld.soapServiceJava Provider - Static? NO
Some comments: ID is the unique name we have to identify our SOAP Service from the SOAP Client. Method contains a range of methods provided by SOAP Service. Java Provider-Provider Class is the name of the SOAP Service Java class. Now, click the deploy button, then your service will be deployed. Also stressed, please pay attention to the correct setting of the ClassPath environment variable. Then, your HelloWorld.soapService class can be found, and all required JAR packages can also be found. This is an ordinary error that almost everyone will commit. Now, you can click List, you will see your service has been deployed. Congratulations!
Finally, let us create a SOAP Client. The code seems to be a bit complicated, but it will not be only so long in reality.
1: // HelloWorldClient.java
2: Import java.io. *;
3: Import java.net. *;
4: Import java.util. *;
5: Import org.apache.soap. *;
6: Import org.apache.soap.rpc. *;
7: public class helloworldclient {
8: public static void main (string [] arg) throws exception {
9: Call C = NULL;
10: URL URL = NULL;
11: Vector params = NULL;
12: Response rep = NULL;
13: String urename = "superman";
14: String Oururn = "URN: HelloWorld_soapService";
15: String old = "Sayhi";
16: URL = New URL ("http: // localhost: 8080 / soap / servlet /
Rpcrouter ");
17: System.out.Println ("Passing to our Deployed" Oururn "
Our Name (" Ourname "): ");
18: C = new call ();
19: C.SetTargetObjecturi (OURURN);
20: C.SETMETHODNAME (OURMETHOD);
21: C.setencodingStyleuri (constants.ns_uri_soap_enc);
22: params = new vector ();
23: Params.AddeElement (New Parameter ("Ourname", String.class,
ORNAME, NULL);
24: C.SetParams (params);
25: System.out.Print ("And ITS Answer IS:");
26: Rep = C.INVoke (URL, ");
27: if (rep.GeneratedFault ()) {
28: fault fault = rep.getfault ();
29: System.out.println ("/ NCALL FAILED!");
30: System.out.println ("CODE =" Fault (); 31: System.out.println ("String =" Fault.getFaultString ());
32:} else {
33: Parameter results = rep.getReturnValue ();
34: System.out.print (Result.getValue ());
35: System.out.println ();
36:}
37:}
38:}
Let me do some explanations. On line 13, we set our name, this name will be passed to SOAP Service. On the 14th line, we set the ID (Service ID) of the service we want to call, and the service method set up to 15 rows (Service Method). With this ID, the service can be deployed in the SOAP Service Manager. We did not set any other values, just only used the basic values just those basic values. You can get relevant information from the official document of SOAP, which comes from the SOAP package, which interprets the scope of this article.
Compile this SOAP Client in the following way:
Javac HelloWorldClient.java
In order to complete it, let's check it out for our test, all things are ready. Tomcat is running, all environment variables are correct, SOAP Service is compiled and deployed, and the SOAP Client is successfully compiled. OK, let's run it, you will see this screen:
As you can see, our SOAP Client uses the SOAP protocol to successfully send its name and receive a reply. As mentioned earlier, the SOAP Service is sent and received is SOAP Envelope. This is the source code of SOAP Envelope.
Soap envelope sent to SOAP Service
XML Version = '1.0' encoding = 'UTF-8'?>
XMLns: xsi = "http://www.w3.org/2001/ Xmlschema-instance " XMLns: xsd = "http://www.w3.org/2001/xmlschema"> SOAP-ENV: EncodingStyle = "http://schemas.xmlsoap.org/ SOAP / Encoding / "> ns1: guyhi>> Soap-env: body> Soap-env: envelope>: SOAP envelope received from SOAP Service XML Version = '1.0' encoding = 'UTF-8'?> XMLns: xsi = "http://www.w3.org/2001/ Xmlschema-instance " XMLns: xsd = "http://www.w3.org/2001/xmlschema"> SOAP-ENV: EncodingStyle = "http://schemas.xmlsoap. ORG / SOAP / Encoding / "> Glad to see you! Return> ns1: Sayhiresponse> Soap-env: body> Soap-env: envelope> To understand all the tags in SOAP envelope, I suggest you spend a little time to read http://www.w3.org/2001/06/soap-envelope Namespace Specification. I hope this article can help you understand SOAP technology. This technology is simple, fun, powerful, and resilient. It is used in many web applications, and the number of these applications is increasing. Learning SOAP is worth it, at least you have to know what it is and how it works. Translated by caiyi0903 (Willpower), 2004.1.17