Web service learning [SOAP]

xiaoxiao2021-03-06  23

1.1 preface

Information exchange between various systems within the enterprise system has always been a problem. In the past, there are DCOM, CORBA and other solutions, but it is not very perfect, not too complicated, it is defective. It is now more popular SOAP (full name: Simple Object Access Protocol, Simple Object Access Protocol).

1.2 SOAP introduction

SOAP and Web Service and Apache SOAP These new concepts (should not be new) often engage in people. I understand so understanding, Web Service (also known as Web Service) is a big concept of concept, which shows a design idea. SOAP is an important component of Web Service. If the web service is metaphor, SOAP can be comparable to TCP / IP. SOAP is an agreement rather than specific products. Microsoft also has its own SOAP implementation product, while the popular SOAP implements product is Apache SOAP, but its next version has been named Axis.

SOAP is a carrier for data transmissions through an XML file. Walking HTTP lines. The general enterprise's firewall opens HTTP's 80-port, so SOAP will not be blocked by firewall, this is an advantage of SOAP.

Both sides of the information transfer require support for SOAP services because the XML files are sent, the other party needs to have SOAP services to receive, and then the other party will have feedback is also an XML file. At this time, you also need to install the SOAP service to receive, as shown below :

XML file

In the XML file transfer to SOAP, SOAP services will have some internal processing. Its specific processing procedure is temporarily regardless of so much, let's write a HelloWorld instance to feel the first.

1.3 download

A total of four packages have to be downloaded, they are all open source. Among them, the first two is apache, the next two is the Sun website, as shown below:

l soap: http://apache.freeelamp.com/ws/soap/Version-2.3.1/

L Xerces: http://xml.apache.org/dist/xerces-j/

l javamail: http://java.sun.com/products/javamail/downloads/index.html

l jaf: http://java.sun.com/products/javabeans/glasgow/jaf.html

If you don't say it, you don't say it. Talk about what you want to pay attention to: Try to download the "target saving as" of IE, some use flashgets cannot be downloaded. Do not turn off the web before downloading.

The downloaded version is: JAF1.0.2 JavaMail 1.3.2 soap2.3.1 Xerces 1.4.4, as shown below.

After downloading them separately. Among them, the SOAP package has some weird, the first decompression is a file SOAP-BIN-2.3.1 without an extension, add this file to a zip or jar suffix name, and then extract once.

1.4 Installation and Write HelloWorld Instance (CVS: V0001)

Native installation environment: WindowsXP JDK1.4.2_06 Tomcat5.0.28 SOAP2.3.1

1.4.1 Copy the JAR file

1. Install JDK and Tomcat. This article is all over the Internet, this article is no longer detailed. Their installation is also very simple: installing JDK is basically clicked "Next", I have not set any environment variables after installation; Tomcat is basically click "Next" to be installed. 2, found in the unzip catalog of these four packages: Xerces.jar, soap.jar, mail.jar, Activation.jar (JAF), copy them to Tomcat's "Tomcat 5.0 / Common / Lib" directory This directory is Tomcat's default package directory, and all packets in this directory are automatically loaded when Tomcat is started.

3, copy the Tools.jar under the C: / JDK / LIB / path to Tomcat's "Tomcat 5.0 / Common / Lib" directory.

Note: This package needs to be used in the management page that displays SOAP. Sets ClassPath points to c: /jdk/lib/tools.jar is useless, I have never added Tools.jar package to classpath, and did not set JDK_HOME Nor did you add C: / JDK / BIN to the Path path, and I didn't do anything when I installed JDK.

4. Put the SOAP.War file under the WebApps directory of the directory, copy it to Tomcat's Tomcat 5.0 / WebApps directory, this directory is the directory where Tomcat's web application, soap.war is SOAP website, as shown below Show:

5, restart Tomcat service. At this time, Tomcat will load newly added package in the "Tomcat 5.0 / Common / Lib" directory to memory.

1.4.2 Write a SOAP program

Write a SOAP program three steps:

l Writing a server-side program, this program is nothing distinguish between

l Configure SOAP to point to the server to the server side.

l Writing the client's program, the client's program has a deep SOAP brand, which will use a lot of SOAP packages and methods.

Since I am used to using Eclipse to write programs, the project is also developed with Eclipse, so the SOAP program here is also written in eclipse. Of course, you can also write SOAP programs with Notepad JDK.

1. Configure the library bonus of the MySoAP project.

Add the four JAR packets shown below to the library bonus of the project. With regard to the settings of the library, here is the way "user library", the specific operation can be referred to this article: http://blog.9cbs.net/glchengang/archive/2005/02/17/291522.aspx. After completing the library reference, the SOAP program written in Eclipse can use SOAP-related classes.

2. Create a new Java project Mysoap, create a package "cn.com.chengang.soap.hello in the project, then create two Java files in the package, as shown below:

(1) HelloWorldService.java is a server-side program that is as follows. There is only one method in this program, and there is no difference in other Java programs, which is also very simple to return a HelloWorld string.

Package cn.com.chengang.soap.hello;

Public class helloworldservice {

Public string getMessage () {

Return "Hello World!";

}

}

(2) HelloWorldClient.java is the client's access program whose code is as follows: package cn.com.chengang.soap.hello;

Import java.net.URL;

Import org.apache.soap.constants;

Import org.apache.soap.fault;

Import org.apache.soap.rpc.call;

Import org.apache.soap.rpc.parameter;

Import org.apache.soap.rpc.response;

Public class helloworldclient {

Public static void main (string args []) throws exception {

String endpoint = "http: // localhost: 8080 / soap / servlet / rpcrouter";

Call call = new call (); // Create an RPC Call

Call.SetTargetObjecturi ("URN: HelloWorldService); // Remote service name

Call.setMethodName ("getMessage"); // Access method

Call.setencodingstyleuri (constants.ns_uri_soap_enc); // Setting the coding style

URL URL = New URL (Endpoint); // SOAP Service URL

/ / Start sending an RPC request and returning a server-side response

Response Resp = CALL.INVOKE (URL, ");

/ / Check if there is a fault in the answer packet

// If you have something wrong, you will print an error message. If you have it, you will print it to the correct return value HelloWorld

IF (resp.generatedfault ()) {

Fault fault = resp.getfault ();

System.out.println ("The Following Error Occured);

System.out.println ("Fault Code =" Fault.getFault ());

System.out.println ("Fault String =" Fault.getFaultString ());

} else {

Parameter Result = Resp.getReturnValue ();

System.out.println (Result.getValue ());

}

}

}

This program has used a lot of SOAP classes. Note: If you are two computers, then HelloWorldService.java and HelloWorldClient.java are separately installed on both computers. The program code in the HelloWorldClient is the GetMessage method in HelloWorldService through the SOAP service.

4. Copy the compilation file of HelloWorldService.java HelloWorldService.class to Tomcat, followed by the following:

(1) Find the HelloWorldService.class file in the bin directory of the Navigator view.

(2) Creating a "CN / COM / CHENGANG / SOAP / HELLO" directory structure under the "Tomcat 5.0 / Common / Classes /" path, this directory structure is the same as the HelloWorldService.class. Then copy the HelloWorldService.class file to this directory, as shown below. Note: There is another way to compare generally, that is, put all server-end Class files into a JAR package, then put this JAR package in the Tomcat 5.0 / Common / Lib catalog.

5, restart Tomcat.

This step is not to forget, only to restart Tomcat can load the new JAR package or Class file under Common to memory.

1.4.3 Release the SOAP server-side program: HelloWorldService.java

There are a variety of ways to let the HelloWorldService register in the SOAP service. This article describes the method of writing an XML file to register the SOAP service.

(1) HelleWorld.xml file. This file can be placed anywhere, and it is not necessarily the relationship with the location of HelloWorldService.java.

Code description:

l URN: HelloWorldService is a service name, which requires the system unique. Here is the same as the same name, you can also take other names.

l GetMessage is the service method provided, that is, the name of the HelloWorldService.

l

(2) Set two environment variables. The reason why these two variables to be set is because the next release command.

Tomcat_home = E: / Program Files / Apache Software Foundation / Tomcat 5.0

ClassPath =% Tomcat_Home% / Common / lib / soap.jar;% Tomcat_Home% / Common / lib / mail.jar;% Tomcat_Home% / Common / lib / activation.jar;% Tomcat_Home% / CommON / lib / xerces.jar

(3) Enter the DOS window and locate the directory where helloWorld.xml is located, and then run the following command (one line). If it is correct, there should be no display; if the command error will output an error message.

Java org.apache.soap.server.serviceManagerClient http://127.0.0.1:8080/soap/servlet/rpcrouter Deploy HelloWorld.xml In addition, introduce other two common commands:

Show the registered SOAP service:

Java org.apache.soap.server.serviceManagerClient http://127.0.0.1:8080/soap/servlet/rpcrouter List

Cancel release:

Java org.apache.soap.server.serviceManagerClient http://127.0.0.1:8080/soap/servlet/rpcrouter undelpoy "URN: HelloWorldService"

The execution process of the command is as follows: (I put the XML file in the E: / SOAPTEST directory, this directory is this file)

You can also go to the SOAP website to see if registration is successful.

1.4.4 Run the client

In Eclipse, HelloWorldClient.java is as follows as a normal Java application, got the following results:

It can be seen that the client program HelloWorldClient calls the GetMessage method for HelloWorldService through the SOAP service and gets a return result.

Here we don't write the transmitted XML file (the previous XML is the registration service, not one thing), because the SOAP package has automatically completed the XML and transferred to the server for us.

1.5 Method for Parameters (CVS: V0002)

In the example of HelloWorld above, the GetMessage method is no parameters, this section we come to add a parameter.

(1) Modify HelloWorldService.java as follows:

Package cn.com.chengang.soap.hello;

Public class helloworldservice {

Public string getMessage () {

Return "Hello World!";

}

Public String getMessage (string str) {

Return "Hello World!" STR;

}

Public String getMessage (String str1, string str2) {

Return "Hello World!" STR1 "&" STR2;

}

}

(2) Copy helloWorldService.class to Tomcat's "Tomcat 5.0 / COMMON / CLASSES / CN / COM / CHENGANG / SOAP / HELLO" directory overwrites the original HelloWorldService.class.

(3) Restart Tomcat service.

(4) Modify the HelloWorldClient program as follows (red word part is new):

Package cn.com.chengang.soap.hello;

Import java.net.URL;

Import java.util.vector;

Import org.apache.soap.constants;

Import org.apache.soap.fault;

Import org.apache.soap.rpc.call; import org.apache.soap.rpc.parameter;

Import org.apache.soap.rpc.response;

Public class helloworldclient {

Public static void main (string args []) throws exception {

String endpoint = "http: // localhost: 8080 / soap / servlet / rpcrouter";

Call call = new call (); // Create an RPC Call

Call.SetTargetObjecturi ("URN: HelloWorldService); // Remote service name

Call.setMethodName ("getMessage"); // Access method

Call.setencodingstyleuri (constants.ns_uri_soap_enc); // Setting the coding style

Vector params = new vector ();

Parameter P1 = New Parameter ("Name", String.class, "Chen Gang", NULL;

Parameter P2 = New Parameter ("Name2", String.class, "Chen Yong", NULL

Params.addelement (P1);

Params.AddeElement (P2);

Call.SetParams (params);

URL URL = New URL (Endpoint); // SOAP Service URL

/ / Start sending an RPC request and returning a server-side response

Response Resp = CALL.INVOKE (URL, ");

/ / Check if there is a fault in the answer packet

// If you have something wrong, you will print an error message. If you have it, you will print it to the correct return value HelloWorld

IF (resp.generatedfault ()) {

Fault fault = resp.getfault ();

System.out.println ("The Following Error Occured);

System.out.println ("Fault Code =" Fault.getFault ());

System.out.println ("Fault String =" Fault.getFaultString ());

} else {

Parameter Result = Resp.getReturnValue ();

System.out.println (Result.getValue ());

}

}

}

(6) Run HelloWorldClient in Eclipse to get the following effect

Reference

http://blog.9cbs.net/caiyi0903/Archive/2004/01/20/18036.aspx

http://blog.9cbs.net/caiyi0903/Archive/2004/01/20/18036.aspx

http://blog.9cbs.net/caiyi0903/Archive/2004/01/20/18037.aspx

http://blog.9cbs.net/madfool/archive/2002/08/17/11309.aspx

转载请注明原文地址:https://www.9cbs.com/read-44617.html

New Post(0)