Readers of this article should first understand J2EE and be familiar with basic content such as XML and tomcat. This article mainly introduces the basic content of web services, how to build a soap service in Java Web development:
I. Simple Object Access Protocol (SOAP) Simple Object Access Protocol. To understand soap, you must first understand the origins of distributed computing. With the emergence of the next generation of distributed computing system Web Services, soap becomes the actual communication standard for creating and calling applications published through the network. Soap is similar to the traditional binary protocols IIOP (CORBA) and jrmp (RMI), but it uses XML and text-based data instead of binary data representation.
Using XML Representation, soap defines a small Wired connection protocol and encoding format to represent data types, programming languages, and databases. It can also use various Internet standard protocols as its message transmission tools, it can also provide conventions for communication models such as RPC and document-driven message exchange. Please note that W3C is working on soap research, http://www.w3c.org/2000/xp/Group/, and positive responses from mainstream vendors to reach consensus on important tasks related to XML-based protocols, and define its key requirements and application scenarios.
Soap1.2 defines the following basic content:
1) syntax and semantics used to represent XML documents as structured soap messages
2) data encoding standard in soap messages
3) communication model used to exchange soap messages
4) Binding of underlying protocols such as soap Transmission
The SOAP message mainly includes the envelope header, message header, body, and attachment.
A simple SOAP message representation:
Post or studentinfo HTTP/1.1
HOST: anthropology.cun.edu
Content-Type: text/XML; charset = "UTF-8"
Content-Length: 640
Soapaction: "getstudentinfo"
<SOAP-ENV: envelop xmlns: SOAP-ENV = "http://www.w3c.org/2001/06/soap-envelope"
Xmlns: xsi = "http://www.w3c.org/2001/XMLSchema-instance"
Xmlns: XSD = "http://www.w3c.org/2001/XMLSchema"
SOAP-ENV: encodingstyle = "http://www.w3c.org/2001/06/soap-encoding">
<SOAP-ENV: Header>
<Person: Mail xmlns: person = "http://www.cun.edu/Header"> xyz@cun.edu
</SOAP-ENV: Header>
SOAP-ENV: Body>
<M: getstudentinfo xmlns: M = "http://www.cun.edu/jws.student.studentInfo">
<Student_name xsi: TYPE = 'xsd: string'>
Wang Wen Yin
</Student>
</M: getstudentinfo>
SOAP-ENV: Body>
SOAP-ENV: envelop>
The above is the namespace version 1.2, the namespace 1.1 SOAP envelope: http://schemas.xmlsoap.org/soap/envelop/, soap encoding: http://schemas.xmlsoap.org/soap/encoding/
For more information about the soap encoding specifications, see the encoded values defined at www.w3c.org/tr/xmlschema-2/. For other specifications, refer to www.w3c.org.
Ii. Learn from the actual example below. Here I use a specific example of axis in a subproject of Apache to facilitate a deep understanding of the operation of soap:
1) download the related content of axis at http://ws.apache.org/axis /:
2) create an instance Program (complying with J2EE Web program specifications), such as (webservicetest directory)
Copy the contents of the Lib folder in axis to your webservicetest/WEB-INF/lib and download the xerces package file of the interpreter from the Internet. jar, also copied to the webservicetest/WEB-INF/lib folder, (to configure log4j, put the attribute file log4j. properties copies to the webservicetest/WEB-INF/classes folder)
3) modify the Web. xml file in the application webservicetest/WEB-INF: The main servlet settings are as follows:
<Servlet>
<Servlet-Name> testservlet </servlet-Name>
<Servlet-class> org. Apache. axis. Transport. http. axisservlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> testservlet </servlet-Name>
<URL-pattern> *. JWS </url-pattern>
</Servlet-mapping>
<Servlet-mapping>
<Servlet-Name> testservlet </servlet-Name>
<URL-pattern>/servlet/testservlet </url-pattern>
</Servlet-mapping>
<Servlet-mapping>
<Servlet-Name> testservlet </servlet-Name>
<URL-pattern>/services/* </url-pattern>
</Servlet-mapping>
<Servlet>
<Servlet-Name> adminservlet </servlet-Name>
<Servlet-class>
Org. Apache. axis. Transport. http. adminservlet
</Servlet-class>
<Load-on-startup> 100 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-Name> adminservlet </servlet-Name>
<URL-pattern>/servlet/adminservlet </url-pattern>
</Servlet-mapping>
<Mime-mapping>
<Extension> WSDL </extension>
<Mime-type> text/XML </mime-type>
</Mime-mapping>
<Mime-mapping>
<Extension> XSD </extension>
<Mime-type> text/XML </mime-type>
</Mime-mapping>
Now you can enter http: // localhost/webservicetest/servlet/testservlet in the URL to see it? Axis uses org. Apache. axis. Transport. http. axisservlet in the axis. jar package to process the application. The basic configuration is described here.
3. Next let's talk about the kernel in axis.
1) Run axis without the Tomcat engine.
First, create a step file to set the environment variable classpath. You must include all the path of the package files under lib and run: Java Org. apache. axis. transport. HTTP. simpleaxisserver <port>
2) the Internal Service handler is org. Apache. axis. providers. java. rpcprovider, which indicates the method required by the service and then provides parameters for the component of the SOAP request message.
3) Application Management Function of axis:
Java org. Apache. axis. Client. adminclient lists the parameters for you to choose from. Our example is: Java org. Apache. axis. Client. adminclient-l http: // localhost/webservicetest/servlet/testservlet list. The service list is displayed, and the XML file is returned.
4) wsdl2java applications can create Java-based programs for the WSDL file, such as placeholder programs.
Java org. Apache. axis. WSDL. wsdl2java <URL>
Here is the basic content of axis.
Iv. Examples
1) Write a logic program, such as soaptest. java.
Public class soaptest {
Public String getstr (string name ){
Return "hello," + name;
}
}
2) Deploy the service and write the WSDD file soaptest_deploy.wsdd:
<Deployment name = "simapletest" xmlns = "http://xml.apache.org/axis/wsdd"
Xmlns: Java = "http://xml.apache.org/axis/wsdd/providers/java"
Xmlns: XSD = "http://www.w3.org/2000/10/XMLSchema"
Xmlns: xsi = "http://www.w3.org/2000/10/XMLSchema-instance">
<Service name = "soaptest" provider = "Java: RPC">
<Parameter name = "classname" value = "soaptest"/>
<Parameter name = "allowedmethods" value = "getstr"/>
</Service>
</Deployment>
The classname parameter is the name of the class you want to deploy (full name), allowedmethods is the method of the called service, and multiple methods can be separated by spaces (for example: <parameter name = "allowedmethods" value = "getstr getmoney"/>), which indicates all when * is used.
Now you are ready to deploy it. Make sure that the Environment path classpath is set correctly and run:
Java org. Apache. axis. Client. adminclient-l http: // localhost/webservicetest/servlet/testservlet soaptest_deploy.wsdd
(If you do not understand it here, please refer to the above instructions)
OK. Now, we have completed the deployment of a Web Service: Test http: // localhost/webservicetest/servlet/testservlet to see if there is another soaptest service in it?
If you do not want to serve the service, re-compile a WSDD file with the following content:
<Deployment name = "simapletest" xmlns = "http://xml.apache.org/axis/wsdd"
Xmlns: Java = "http://xml.apache.org/axis/wsdd/providers/java"
Xmlns: XSD = "http://www.w3.org/2000/10/XMLSchema"
Xmlns: xsi = "http://www.w3.org/2000/10/XMLSchema-instance">
<Service name = "soaptest"/>
</Deployment>
Just like above, it's okay to compare it.
V. Client test:
We can also use Java for testing on the client, and there are materials on the Internet. You can study it very easily. Now, in order to reflect the charm of Web Services, I will use the. NET platform for testing. The client uses C # for compiling (first install the. NET Framework SDK ):
1) generate a Web Service proxy using WSDL and run it on the net platform:
WSDL/L: CS/Protocol: SOAP/out: soaptestclient. CS http: // localhost/webservicetest/services/soaptest? WSDL
We obtained a CS file soaptestclient through WSDL. CS (current directory). You can open the CS file and study the code in it. The getstr (string handler tool has already completed this for us. The wsdl2java tool in axis also has the same functions, refer to the kernel content about axis mentioned above.
2) Compile the CS file into the Assembly dll:
CSC/Target: Library/R: system. Web. Services. dll/R: system. xml. dll soaptestclient. CS
Finally, we waited for a DLL file soaptestclient. dll, and the client program called it.
3) Compile the client application soaptestclientapp. CS
Using system;
Namespache JWS. Client {
Public class soaptestclientapp {
Public soaptestclientapp (){
}
Public static void main (string [] ARGs ){
If (ARGs. length! = 1 ){
Console. writeline ("Usage: soaptestclientapp <Name> ");
Environment. Exit (1 );
}
Soaptestservice st_service = new soaptestservice ();
St_service.getstr ("Wang wenyin ");
}
}
}
4) Compile the file CSC/R: soaptestclient. dll soaptestclientapp. CS.
Run soaptestclientapp
Output result:
Hello, Wang wenyin
It is consistent with the expected results.
Now, let's talk about soap Web Service Development. I hope this article will help you. Thank you :)
If you have any questions about this article, please leave a message to the author. Author address: Sidney.J.Yellow@gmail.com