Implement Java and .NET Internet through Web Service
(Package the .NET program into a web service, call via Java program)
Web Service Overview
What is Web Service
Web Service is a software model capable of processing discrete tasks or continuous tasks based on network, especially the World Wide Web. At present, more popular applications are packaged by a company, providing Web Service, and then other companies can dynamically use these online services through the Internet. This provides new standards and architectures for future global e-commerce development.
Web service architecture
Web service is independent, modular, can describe, publish, position, and calls through the Internet. Three roles are included in the Architecture of Web Service: Service Provider, Service Requestor, service registry. There are three operations between roles: Publish, Find, Binding (BIND).
The following figure clearly describes three roles and the role relationship.
Web Service Protocol
Simple Object Access Protocol (SOAP)
SOAP is an abbreviation for Simple Object Access Protocol. It is a representation layer protocol based on XML, which is used to easily exchange data in the form of objects between dispersed or distributed applications. In the lower layer of SOAP, it can be http / http, or SMTP / POP3, which can also be specially designed for some applications.
SOAP includes three main parts:
2 SOAP package structure: Define a whole framework to indicate what content in the message, who will handle these content, and these contents are optional or required.
2 SOAP encoding rules: A series of mechanisms that define instances of data types that exchange applications defined.
2 SOAP RPC said: Define an agreement used to represent remote procedure calls and answers.
Web Service Description Language (WSDL)
WSDL is an abbreviation for Web Service Description Language. The language defines network services into a set of communication endpoints that can exchange messages, providing a distributed system to help documents, and can also be used as a solution for automatic application communication.
Unified description, discovery and integration protocol (UDDI)
UDDI is a web-based, distributed, implemented standard specification for the information registry provided by Web Service, and also contains a group of web services that can provide itself to make other companies can discover. Implementation standard for access protocols.
Call Web Service via Java
Here we have implemented a Web Service through .NET, it provides such a function, adding the two numbers of the input, return the result.
Example Web Service provided by Web Service
This example web service provides two integers, using C # to implement Web Service, can access the Web Service description file via http://10.16.92.29/java/math.asmx?wsdl, the corresponding WSDL description is as follows Down:
Calling this web service through jbuilder9
Create a new project TestWebService
New project, specify the project name "TestWebService", and the directory, all of which are set by default.
Import Web Service from the URL
By shortcut "Ctrl N", open the new window, select the "Web Services" item, then select "Import A Web Service", click OK. Enter the WSDL file address: http://10.16.92.29/java/math.asmx? WSDL, and then click "Next". In this step, all options can be set by default. Then click "Next".
In this step, you can specify the package name at will, if you select the checkbox in "Package Options". All classes generated will use the same package name. You can then select the "SOAP" protocol type, where we select the SOAP1.2 protocol, and other settings can be used by default, click the "Next" button.
In the confirmation window, click Finish to complete the import work.
After the introduction is successful, JBuilder9 will automatically generate the following files as shown below.
Automatically generate file role
Ø Service1.java
This class defines the required interface
Ø service1locator.java
This class implements the service1 interface, mainly used to locate the WSDL file.
Ø Service1soap.java
This class defines the interface method provided by Web Service
Ø Service1soapImpl.java
This class implements the Service1SoAP interface
Ø Service1soapstub.java
This class implements the Service1SoAP interface for implementing Web Service calls.
Ø service1testcase.java
JBuilder automatically generated unit test class
Write test code
Package Org.Tempuri;
Import java.util. *;
/ **
* test program
*
Title: Test Web Service P> through the client
*
Description: Test Web Service P> through the client
*
Copyright: Copyright (C) 2004 P>
*
Company: Tiger p>
* @Author Sucretiger
* @version 1.0
* /
Public Class Test
{
Public static void main (String [] AGRS)
{
LONG STARTW = Calendar.getInstance (). gettime (). gettime ();
For (int i = 0; i <3; i ) {
Try {
// Locate the location of the WSDL file
Service1locator SL = New Service1Locator ();
// Get a reference to Web Service
Service1soap ss = (service1soap) sl.getService1SoAP ();
// Test the time spent by the web service call by recording the start and end of the time
LONG START = Calendar.getInstance (). gettime (). gettime ();
System.out.println ("Start Time: Start);
// Call the function method provided by Web Service
Int results = ss.add (100, 99);
System.out.println ("Return Result:" Result);
LONG END = Calendar.getInstance (). gettime (). gettime (); system.out.println ("End Time: End);
System.out.println ("Time Consumption:" (end - start));
}
Catch (Exception EX) {
EX.PrintStackTrace ();
}
}
}
}
View test results
D: /JBUILDER9/JDK1.4/bin/javaw -classpath "D: / Projects / TestWebService / Classes; D: /JBuilder9/thirdparty/junit3.8/junit.jar; d: /jbuilder9/lib/Unittest .jar; D: /jbuilder9/lib/saaj.jar; d: /jbuilder9/lib/jaxrpc.jar; d: /jbuilder9/lib/axis.jar; d: /jbuilder9/lib/wsdl4j.jar; d: / jbuilder9 / lib / log4j-
1.2.8
.jar; d: /jbuilder9/lib/commons-logging.jar; d: /jbuilder9/lib/commons-discovery.jar; d: /jbuilder9/lib/xercesImpl.jar; d: /jbuilder9/lib/xmlparserapis.jar D: /jbuilder9/lib/activation.jar; d: /jbuilder9/lib/mail.jar; d: /jbuilder9/jdk1.4/demo/jfc/java2d/java2demo.jar; d: /jbuilder9/jdk1.4 /Demo/plugin/jfc/java2d/java2demo.jar;d:/JBUILDER9/JDK1.4/JRE/LIB/Charsets.jar;d:/JBUILDER9/JDK1.4/JRE/LIB/EXT/dnsns.jar;d : /Jbuilder9/jdk1.4/jre/lib/ext/ldapsec.jar; D: /JBuilder9/jdk1.4/jre/lib/ext/localedata.jar; D: /JBuilder9/jdk1.4/jre/lib/ Ext / sunjce_provider.jar; d: /jbuilder9/jdk1.4/jre/lib/im/indicim.jar; d: /jbuilder9/jdk1.4/jre/lib/jaws.jar; d: /jbuilder9/jdk1.4 /jre/lib/jce.jar;d:/JBUILDER9/JDK1.4/jre/lib/jsse.jar;d:/JBUILDER9/JDK1.4/JRE/LIB/rt.jar;d:/JBUILDER9/JDK1. 4 / JRE / LIB / SUNRSASIGN.JAR; D: /JBUILDER9/JDK1.4/lib/dt.jar; d: /jbuilder9/jdk1.4/lib/htmlconverter.jar; d: /jbuilder9/jdk1.4/lib /Tools.jar "Org.tempuri.test
Start time: 1084968122004
Return Result: 199 (Web Service Returns)
End time: 1084968122833
Time consumption: 829 (// call the time spending a web service, unit milliseconds)
Start time: 1084968122848
Return Result: 199
During the end: 1084968123036
Time consumption: 188
Start time: 1084968123036
Return Result: 199
During the end: 108496812323
Time consumption: 187
Call Web Service through JSP
Ø Pack the JBuilder9 by importing the class generated by the WSDL file, packed into client.jar. The figure above is the directory structure of the class file to be packaged, and use the following command to complete the package operation:
Jar -cvf client.jar ORG
Ø Find the following packages in the lib subdirectory of JBuilder9 installation directory (Axis.jar, Commons-Discovery.jar, Jaxrpc.jar, Log4j-
1.2.8
.jar, commons-logging.jar, saaj.jar, wsdl4j.jar).
Ø Write a JSP test code and package it to generate a test web application.
The figure above is the directory structure of the web application to be packaged, and use the following command to complete the package operation:
Jar -cvf defaultwebapp.war DefaultWebApp
Ø The Web Service can be tested by http: // localhost: 7001 / index.jsp.
Ø JSP test source code
<% @ page contenttype = "text / html; charset = GBK"%>
<% @ Page Import = "java.util. *"%>
<% @ page import = "org.tempuri. *"%>
<%
LONG STARTW = Calendar.getInstance (). gettime (). gettime ();
For (int i = 0; i <3; i ) {
Try {
// Locate the location of the WSDL file
Service1locator SL = New Service1Locator ();
// Get a reference to Web Service
Service1soap ss = (service1soap) sl.getService1SoAP ();
// Test the time spent by the web service call by recording the start and end of the time
LONG START = Calendar.getInstance (). gettime (). gettime ();
System.out.println ("Start Time: Start);
// Call the function method provided by Web Service
Int results = ss.add (100, 99);
System.out.println ("Return Result:" Result);
LONG END = Calendar.getInstance (). gettime (). gettime ();
System.out.println ("End Time: End);
System.out.println ("Time Consumption:" (end - start));
}
Catch (Exception EX) {
EX.PrintStackTrace ();
}
}
%>
Note: The required JAR package must be loaded when the application server is started, otherwise the JSP file is compiled.
Reference
2 JBuilder9 help file
2 http://www.w3.org/2002/ws/
2 http: // www-900.ibm.com/developerWorks/cn/webservices/ WS-WSAR / Part2 / INDEX.SHTML