Before getting started, I would like to make some necessary descriptions of the topics to be discussed in this article and the starting knowledge that readers need. The topic of this article is to discuss soap technology. We will try to make it easier to understand, we have introduced a common "Hello World" example to show readers how they work and how they work. In my opinion, it is difficult to start learning this new technology only by reading official documents. The official documents contain a large number of examples and explain all the features and advantages, but they do not explain how they work. In this article, I am not going to explain the definition of soap technology in a superficial way. We assume that readers are familiar with Java and web technologies and have the basics of XML language, XML Namespaces, and XML schema. In this case, there will be no difficulty in understanding this article. However, even if you do not have this knowledge, you do not have to worry about it. We will try to explain it as easily as possible, but you will encounter some problems and troubles when you start to build your own soap application. Therefore, it is necessary to spend some time learning the above knowledge.
From a technical point of view, it is necessary to mention our debugging environment. We use Windows XP and install Java 2 SDK 1.4.1, Apache SOAP 2.3.1, JAF 1.0.2, javamail 1.3.1, and xerces 2.6.0. All mentioned above can be normally run in the tcomcat 4.1.29 JSP/servlet container. All software is free and free, 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 at Sun's official website (http://java.sun.com) and other software is down on the official site of the Jakarta Project Team (http://jakarta.apache.org. You may be a little worried and upset when you see that we need so many different packages, but you should not worry about these. Once you understand the basics, it will be quite easy for you. Installation is not difficult. You only need to carefully set the environment variables, such as catalina_home, classpath, and java_home. All these steps are manual. Since I just want to focus your attention on them again, you can quickly complete the above configuration process. I just added the following strings to the bin/setclasspath. BAT file of Tomcat:
... 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. Jar Set 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 one used above, you need to correct them, and then close and restart Tomcat for them to take effect. In this way, you are ready to run soap. But now, I want to forget the relevant technical aspects to learn some theoretical knowledge.
Soap indicates the Simple Object Access Protocol ). Soap is very simple, just like its name. It is an XML-based protocol that allows program components and applications to communicate with each other over HTTP, a standard Internet protocol. Soap is an independent platform that does not rely on programming languages. It is simple, elastic, and easy to expand. Currently, applications can communicate with each other through a Remote Procedure Call (RPC) based on DCOM and CORBA, but HTTP is not designed for this purpose. RPC is very difficult to apply on the Internet, and there are many compatibility and security problems, because firewalls and proxy servers usually block these types of traffic. The best communication method between applications is through HTTP, because HTTP supports all Internet browsers and servers. For this purpose, the SOAP protocol is created.
How do they work? For example, an application (a) needs to communicate with another application (B) with the help of soap. They will use the following frame chart to complete this process:
This SOAP envelope (SOAP envelope) is an XML document containing the following content:
As you can see, it is very simple. It looks like a normal envelope or your email. Do you want to see how they work? Come with us. In fact, we have many ways to create and run our own "Hello World" application without using soap, but because we need to maintain its simplicity, I will give you a frame chart (Scheme) on how it works ).
Our "Hello World" example contains a soap service. Our soap client will send their names to the soap service and try to get some replies or responses. This soap service needs to be deployed to a soap admin tool, so that the SOAP (proxy) RPC router relocated to all requests can know which service they should use to operate. All in all, this operation is performed in the following ways:
Now let's take a step-by-step look at what happened. In step 1, helloworldclient connects to a soap RPC router, requests our soap service, and passes a string containing our name to it. The soap RPC router checks whether the soap service has been deployed. If it is found to be deployed, it will pass data to the soap service and call a specific method. This is step 2. Then the soap service method is executed and a string value (this value is the response or response of the soap client) is returned (Step 3 ). In step 4, the soap RPC router only redirects the data to the soap client. All data transmitted in step 1 and Step 4 is completed through SOAP envelope. As you can see, the algorithm is quite simple, so we only want to focus on the actual code.
First, we need to create a soap service. The following is its code. Do not forget to put it into the helloworld/directory (it must be included in your classpath ):
1: // soapservice. Java 2: Package helloworld; 3: Public class soapservice { 4: Public String sayhi (string X ){ 5: Return ("Hello my friend," + x + "! Gglad to see you! "); 6 :} 7 :} |
It is easy to add any comments. To compile it, you only need to use the following command:
Javac soapservice. Java
Step 2: Once we have prepared the soap service, we need to deploy it using the soap Service Manager. This can be implemented in many ways, but in order to make it easier for beginners to understand soap, I provide the easiest way. Assume that your web server (tomcat or other) is running properly and you have installed soap correctly. When the browser accesses http: // localhost: 8080/soap/, you will see the welcome page of Apache SOAP. Click Run the Admin Client and deploy. You will get a screen display where you need to fill in the ID, scope, method, provider type and Java provider Information to the form field. You can ignore all other form fields unless you really need their information. We do not need them in our "helloworld" example. Therefore, we fill in the following values:
ID: urn: helloworld_soapservice Scope: Application Methods: sayhi Provider type: Java Java provider-provider class: helloworld. soapservice Java provider-static? No |
Note: ID is the unique name of the soap service from the soap client. The method contains a series of methods provided by the soap service. Java provider-provider class is the name of the soap service Java class.
Now, click deploy to deploy your service. Again, pay attention to setting the classpath environment variables correctly. Then, your helloworld. soapservice class can be found, and all necessary jar packages can also be found. This is a common mistake that almost everyone will make. Now, you can click list to see that your service has been deployed. Congratulations!
Finally, let's create a soap client. The code looks a little complicated, but in reality it won't be so long.
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 ourname = "Superman "; 14: String oururn = "urn: helloworld_soapservice "; 15: String ourmethod = "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. addelement (new parameter ("ourname", String. Class, Ourname, null )); 24: C. setparams (Params ); 25: system. Out. Print ("and its answer is :"); 26: rep = C. Invoke (URL ,""); 27: If (REP. generatedfault ()){ 28: fault = rep. getfault (); 29: system. Out. println ("/ncall failed! "); 30: system. Out. println ("code =" + fault. getfaultcode ()); 31: system. Out. println ("string =" + fault. getfaultstring ()); 32:} else { 33: parameter result = rep. getreturnvalue (); 34: system. Out. Print (result. getvalue ()); 35: system. Out. println (); 36 :} 37 :} 38 :} |
Here are some explanations. In row 3, we set our name, which will be passed to the soap service. In row 14th, we set the service ID we want to call, and the service method set in row 15th ). With this ID, the service can be deployed in the soap Service Manager. We didn't set any other values. We only needed the basic values just now to work properly. You can get the relevant information from the official soap documentation, which comes from the soap package and their explanations are beyond the scope of this article.
Compile the soap client as follows:
Javac helloworldclient. Java
To complete it successfully, let's check whether everything is ready for our tests. Tomcat is running. All environment variables are correct. The soap service is compiled and deployed, and the soap client is compiled successfully. OK, let's run it and you will see this screen:
As you can see, our soap client successfully sends its name and receives a response using the SOAP protocol. As mentioned above, the soap Service sends and receives SOAP envelope. This is the source code of SOAP envelope.
SOAP envelope sent to the soap Service
<? XML version = '1. 0' encoding = 'utf-8'?> <SOAP-ENV: envelope xmlns: SOAP-ENV = "http://schemas.xmlsoap.org/ Soap/envelope /" Xmlns: xsi = "http://www.w3.org/2001/ XMLSCHEMA-instance" Xmlns: XSD = "http://www.w3.org/2001/XMLSchema"> SOAP-ENV: Body> <NS1: sayhi xmlns: NS1 = "urn: helloworld_soapservice" SOAP-ENV: encodingstyle = "http://schemas.xmlsoap.org/ Soap/encoding/"> <Ourname xsi: TYPE = "XSD: string"> Superman </ourname> </NS1: sayhi> SOAP-ENV: Body> </SOAP-ENV: envelope>: |
SOAP envelope received from the soap Service
<? XML version = '1. 0' encoding = 'utf-8'?> <SOAP-ENV: envelope xmlns: SOAP-ENV = "http://schemas.xmlsoap.org/ Soap/envelope /" Xmlns: xsi = "http://www.w3.org/2001/ XMLSCHEMA-instance" Xmlns: XSD = "http://www.w3.org/2001/XMLSchema"> SOAP-ENV: Body> <NS1: sayhiresponse xmlns: NS1 = "urn: helloworld_soapservice" SOAP-ENV: encodingstyle = "http://schemas.xmlsoap. Org/soap/encoding/"> <Return xsi: TYPE = "XSD: string"> hello my friend, Superman! Gglad to see you! </Return> </NS1: sayhiresponse> SOAP-ENV: Body> SOAP-ENV: envelope> |
To understand the meaning of all labels in SOAP envelope, I suggest you spend some time reading the http://www.w3.org/2001/06/soap-envelope namespace specification.
I hope this article will help you understand the soap technology. This technology is simple, interesting, powerful, and elastic. It is used in many web applications, and the number of such applications is also increasing. Learning soap is worthwhile. At least you need to know what it is and how it operates.