Very Simple Object Access Protocol with Javaby Olexiy & Alexander Prokhorenko
The famous foreign developer website developer.com published in January 15, 2004
Before starting, I would like to make some essential descriptions on the theme and the starting knowledge of the subject and readers needed. The theme of this article is to discuss SOAP technology, we will make it easy to understand as much, so we introduced a normal "Hello World" example to show the readers how they operate, let readers know their way of operation. . I think that if the official documentation is difficult to start learning this new technology, the official document contains a lot of examples, and all features and advantages are explained, but they did not explain their way of operation. In this article, I don't plan to explain the definition of SOAP technology. We assume that readers are familiar with Java language, Web technology, with XML language, XML Namespaces, XML Schema's basis. In this case, it is understood that there will be no difficulties in this article. However, even if there is no such knowledge, you don't have to worry, we will explain as simple as possible, but you will encounter some problems and trouble when you start doing your own SOAP app. Therefore, it is necessary to spend a certain time to learn the top knowledge.
From the perspective of technology, it is necessary to mention our debugging environment. We use the Windows XP operating system with Java 2 SDK 1.4.1, Apache SOAP 2.3.1, JAF 1.0.2, JavaMail 1.3.1 and Xerces 2.6.0. The above mentioned can run normally in the Tcomcat 4.1.29 JSP / Servlet container. All software is free free software, so you can easily download them and install them on any of your favorite platforms such as Windows or UNIX. Java SDK, Jaf and JavaMail are located in Sun's official website (http://java.sun.com), other software has Down on the official website of the Jakarta project group (http://jakarta.apache.org). Maybe you have been a bit worried and uneasy when we need so many different packages (package), but in fact you should not worry about it. Once you know the foundation, then you will be quite easy to you. Install is not a difficult thing, you only need to carefully set the environment variable, such as Catalina_Home, ClassPath, Java_Home, etc., all these steps are manually. Since I just want to concentrate your attention again, you can complete the above configuration procedure. I just add some strings below to Tomcat's bin / setClassPath.bat file:
...
SET CATALINA_HOME = C: /Tomcat4.1.29
Set classpath =% java_home% / lib / Tools.jar
Set classpath =% java_home% / soap-2.3.1 / lib / soap.jar
Set classpath =% classpath%;% java_home% / javamail-1.3.1 / mail.jar
Set classpath =% classpath%;% java_home% / jaf-1.0.2 / activation.jar
Set classpath =% classpath%;% java_home% / Xerces-2_6_0 / XercesImpl.jar
Set classpath =% classpath%;% java_home% / XERCES-2_6_0 / xercessamples.jarset classpath =% classpath%;% java_home% / XERCES-2_6_0 / XML-Apis.jar
Set classpath =% classpath%;% java_home% / Xerces-2_6_0 / xmlparserapis.jar
Set classpath =% classpath%;% catalina_home% / common / lib / servlet.jar
Set classpath =% classpath%;% catalina_home% / common / lib / Tools.jar
...
If your installation path (INSTALLATION PATHS) is different from the above, you need to correct them, then close and restart Tomcat so that they take effect. In this way, you have prepared for running SOAP. But now, I have to forget the relevant technical part to learn a aoretical knowledge.
SOAP means simple object access protocol (Simple Object Access Protocol). The SOAP is very simple as its name. It is an XML-based protocol, allowing program components and applications to communicate with each other using a standard Internet protocol - http. SOAP is an independent platform that does not rely on programs, it is simple, elastic, easy to expand. Currently, applications can communicate with each other using a remote procedure call (RPC) based on DCOM and CORBA technology, but HTTP is not designed to be this purpose. RPC is very difficult in Internet applications, which will have many compatibility and security issues because firewalls and proxy servers typically block (block) of these types of traffic. The best communication method between the application is through the HTTP protocol because HTTP is supported all Internet browsers and servers. Based on this purpose, the SOAP protocol is created.
So how do they work? For example, an application (a) needs to communicate with each other with another application (b) with the help of SOAP. They will use the following frame diagram to complete this process:
This SOAP envelope is an XML document containing the following:
As you can see, it is very simple. It seems to be an ordinary envelope or your email. Do you want to see how they move? Let's come with us. In fact, there are many ways to use SOAP to create and run our own "Hello World" application, but because we have to keep it simple, I will give you a framework for how it works (Scheme).
Our "Hello World" example will contain a SOAP Service. Our SOAP Client will send their name to the SOAP Service and try to get some reply or responses. This SOAP Service requires a tool to be deployed to a SOAP Admin to relocate all requested SOAP (Proxy) RPC Router know which service they should use to operate. All in all, this is to operate in the following ways:
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. Below is its code, please don't forget to put it in the HelloWorld / directory (must be included in your classpath): 1: // SOAPSERVICE.JAVA2: 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 Step 2, 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 notes: ID is the only name we need to identify our SOAP Client SOAP Service of. 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.java2: 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. Compiled this SOAP Client: Javac HelloWorldClient.java with the following ways to complete it, let's check it 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 us 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 XML Version = '1.0' Encoding = 'UTF-8'?> Encoding = 'UTF-8'?> 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 / "> Soap-env: body> Soap-env: envelope>: SOAP ENVELOPE XML Version = '1.0' Encoding = 'UTF-8'?> 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