Soap purification wired protocol (III): Services written in scripting language

Source: Internet
Author: User
Tags soap client
Are you a Java developer eager to stay at the forefront of technology forever? The software industry is changing, and you are eager to grasp the future of Web. More importantly, how can you leverage your years of Java experience to the extreme. You don't have to go too far to find answers to these questions. The answer lies in soap.

Soap (Simple Object Access Protocol) is a wired protocol that uses XML to encode data. It provides a higher level of collaborative operation capability for Java platform independence and portability. In this
In the second article of the soap series, I introduced Apache SOAP. As one of the implementations of the SOAP specification, Apache
Soap simplifies the construction of a soap application. We used Apache
Soap constructs two simple helloworld services and client programs that call the sayhelloto () method of these services. We can see that although the creation of the soap service is quite simple
But the client program must complete a lot of extra work. For example, it must set the call object, call the call object's invoke () method, and then analyze the results returned as the call result.
Response object.

In fact, Apache
The process of creating a soap service in soap can also become simpler. The only premise is that you must understand one of the specified scripting languages. For me -- and for most Java developers, lucky
These scripting languages contain JavaScript. Really good, in Apache
In soap, you can use JavaScript to create a soap service. This document describes how to create a soap service using JavaScript.

I. Reconstruct Apache SOAP
Apache SOAP scripts can be created in bean scripting.
Framework (BSF, bean script framework. BSF was originally developed by IBM and is now released as an open source code project. It enables Java programs to run and use
Scripts written in other languages also enable other scripting languages to use existing Java classes. Apache
Soap utilizes the former capabilities of BSF. The standard binary version downloaded from the Apache website does not support scripts. Specifically, soap. jar does not contain
Org. Apache. Soap. server. invokebsf class, which is a combination of Apache SOAP and BSF and an interface. Apache
Soap developers know that not everyone who uses soap needs BSF, and not everyone has installed the script engine. Therefore, script support is omitted in soap. jar. To use a script
To compile a soap service, you must re-construct the service from the source code to introduce the invokebsf class.

First download the source code (soap-src-2.0.zip) from http://xml.apache.org/dist/soap ). Then, extract the downloaded file.
Compress to Apache
The directory where soap is installed. Here, it is E: the root directory of the drive. After that, you will get a SRC subdirectory under the soap_2-0 directory that contains Apache
All source code of soap. Before re-constructing Apache SOAP from the source code, you must download the required BSF
JAR file. You can find one at ftp://ftp.mozilla.org/pub/js. Use the Mozilla JavaScript Engine rhino
With it, rhino can download a zip file from http://www.mozilla.org/rhino/download.html. I decode this file
Compress to E: the root directory of the disk and finally obtain an E:/rhino directory containing Rhino. What we are interested in is its Js. jar.

Next, you need a tool that actually performs the re-construction operation, namely ant. Ant is also an Apache Software Engineering. It is a Java-based tool. Ant actually and creates
The Web Server Tomcat project belongs to the same project, that is, Jakarta. In ant, all the construction information, such as the construction target and dependency, are specified through the xml configuration file. This is
Ant has unique features. In addition, ant is scalable. See the articles in the "reference resources" section at the end of this article to learn how to make full use of ant's potential. You can refer to the links provided by reference resources
Download ant and uncompress it (I put it in the root directory of the C: disk ).

Now, run the following command from the Apache SOAP installation directory:

Set
Classpath = E:/Jakarta-Tomcat/lib/servlet. jar; E:/xerces-1_2_0/xerces. jar;
E:/soap-2_0/lib/BSF. Jar
C:/build/bin/ant

Since the preceding command does not specify an xml configuration file, the ant batch command file looks for a file named build. XML in the current directory (here, E:/soap_2-0)
. Apache
Soap provides this file. Open this file and you can see that the invokebsf class is only available when com. IBM. BSF. bsfmanager is in classpath.
Will be compiled. This is why I put BSF. Jar (which includes the bsfmanager class) into the class path. Extract the newly constructed soap. jar file from the build/lib subdirectory
Copy to the Lib subdirectory (I suggest modifying the original soap. jar file for backup ). Finally, add BSF. jar and JS. jar to the Web server class path.

Success! Now you can start writing the soap service using scripts.

2. Compile the helloworld application in Javascript
Now, we use JavaScript to re-compile the helloworld service of the second article. The complete code of the service program is as follows:

Function sayhelloto (name)
{
Return "hello" + name + ", how are you? ";
}

Is there anything easier? However, do not make it easy to cheat you. In fact, you can perform complicated processing in the service program. For example, you can access any standard Java class from the script code. See the modified script code below, which outputs the server time:

Function sayhelloto (name)
{
VaR today = new java. util. Date ();
Java. Lang. system. Out. println ("today is" + today. tostring ());
Return "hello" + name + ", how are you? ";
}

In addition, you can import and use any of your own Java classes. For example, you can modify the script code to use name JavaBean:

Importclass (packages. Hello. Name );
Function sayhelloto (name)
{
VaR today = new java. util. Date ();
Java. Lang. system. Out. println ("today is" + today. tostring ());
VaR beanname = new name ();
Beanname. setname ("John ");

Java. Lang. system. Out. println (beanname. getname ());
Return "hello" + name + ", how are you? ";
}

3. Deploy services
Before using a javascript service, you must deploy it. As described in the second article in this series, there are two ways to deploy services in Apache SOAP: to use a web interface management tool or to deploy services from the command line. Let's take a look at the specific operation process of the two methods.

3.1 Use Web interface management tools
To use the web interface management tool, open http: // localhost: 8080/Apache-soap/admin in a browser. Click
Deploy button. Remember, the ID input box is used to set the Object ID. The soap infrastructure uses the Object ID to associate RPC (Remote Procedure Call) requests to the soap service. Every
Apache
The soap service must have an object ID, which must be unique among all services deployed on the server. Set the ID to urn: Hello.
The object ID set for the service in article 2.

Set the scope input box to application. The scope input box is used to specify the survival time of the service instance that responds to the call request (refer to more instructions in the second article ).

In the methods input box, enter the name of the method that can be called by the deployed service. Multiple method names are separated by blank characters. Our service only supports one method, namely sayhelloto ().

Since the service is implemented in Javascript rather than Java as in the second article, the provider
Enter the script in the type input box. Correspondingly, the Java provider input box (including the provider
Class and static input boxes) are not required. But you must enter the script
In the provider input box, select JavaScript (Rhino) as the script language. Because the script body is provided in the script text input box, you do not need to enter
Script filename input box. Copy the following script code to the script input box:

Importclass (packages. Hello. Name );
Function sayhelloto (name)
{
VaR today = new java. util. Date ();
Java. Lang. system. Out. println ("today is" + today. tostring ());
VaR beanname = new name ();
Beanname. setname ("John ");

Java. Lang. system. Out. println (beanname. getname ());
Return "hello" + name + ", how are you? ";
}

Scroll to the bottom of the screen and click the deploy button under the form (not the deploy button on the left of the window ). To verify that the service has been successfully deployed, click the list button on the left of the window. At this time, the urn: Hello service should appear in the service list. Click this service to confirm that all information is consistent with the information just entered.

3.2 deploy services from the command line
To deploy a service from a command line, all deployment information must be placed in an XML deployment descriptor file. The following is the XML deployment descriptor file used to deploy the service:

<ISD: Service xmlns: ISD = "http://xml.apache.org/xml-soap/deployment"
Id = "urn: Hello">
<ISD: provider type = "script" Scope = "application" methods = "sayhelloto">
<ISD: script language = "JavaScript">
Importclass (packages. Hello. Name );
Function sayhelloto (name)
{
VaR today = new java. util. Date ();
Java. Lang. system. Out. println ("today is" + today. tostring ());
VaR beanname = new name ();
Beanname. setname ("John ");

Java. Lang. system. Out. println (beanname. getname ());
Return "hello" + name + ", how are you? ";
}
</ISD: SCRIPT>
</ISD: provider>
</ISD: Service>

Compared with the deployment descriptor file used in the second article, the main difference here is that the provider type is set to script rather than Java. For this reason, the Describer file does not specify a Java class, but provides the script code of the service.

Before deploying the service, make sure that the Web server is started. The following code shows how to deploy a service:

Java org. Apache. Soap. server. servicemanagerclient
Http: // localhost: 8080/Apache-soap/servlet/rpcrouter deploy
Deploymentdescriptor. xml

Deploymentdescriptor. XML is the XML file that contains the deployment description described earlier. To verify that the service has been successfully deployed, run the following command:

Java org. Apache. Soap. server. servicemanagerclient
Http: // localhost: 8080/Apache-soap/servlet/rpcrouter query urn: Hello

At this time, we should see the same content as the deploymentdescriptor. xml file.

Iv. Test
We use the client. Java provided in the second article to test the helloworld service. Why can I use the same client program to access services written in JavaScript?
What about it? Because the customer program does not care about the language in which the service is written. As long as the service can understand the SOAP request and return the soap response, the client program will not focus on the implementation of the service. Review 1
Below is the batch command file I used to run Hello. Client:

Set
Classpath = E:/soap-2_0/samples/; E:/soap-2_0/lib/soap. jar;
E:/xerces-1_2_0/xerces. Jar
Java hello. Client Tarak

Observe the console window of the Web server. Every time we run the client program, we can see the current date and output "John ".

■ Conclusion
In this article, I introduced Apache
The script language supported by the SOAP implementation. Why is it important? You just need to analyze why Web development is so popular. In my opinion, a key reason is that web development is mature and almost
Anyone can use simple scripting languages such as HTML and JavaScript to construct complex web pages. Similarly, on the web development server, people can use JSP to learn
Powerful scripting language. I think this reasoning is also applicable to soap development. Soap should be simplified as much as possible if it wants to enter the mainstream for most people's support. Apache
Soap is added to support scripts for this purpose. It significantly extends the scope of developers who create soap services.

However, do not forget that there is another factor to consider: the client developer, that is, the developer who calls the soap service. As mentioned above, Apache
The soap client developers suffer losses, but increase unnecessary work. Therefore, in the next and last articles in this series, I will introduce a framework that uses Java
2. Based on the new dynamic proxy class introduced in platform 1.3, it is as simple and intuitive as creating a client program as creating a soap service.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.