Java and. NET are two of the most competitive development media in the world today, and there are many similarities between the languages. In many large-scale development projects, it is often necessary to use two languages for integrated development. Many developers tend to be biased towards one language and feel intimidated by the other when using integrated development. Here's an example of how Java and. NET are called each other. The following introduction mainly includes three aspects: one is to call each other through common web services, and the other is to use TCP/IP sockets to call each other, and the other is to use remote to realize the mutual invocation of distant objects.
In this chapter first to introduce you to the simplest, most commonly used Web services to call each other way. First of all, the source of Web services, Web Services is a new branch of the Web application, can perform any function from simple request to complex business processing. Once deployed, other Web service applications can discover and invoke the services it deploys. Web Service is an application that uses standard Internet protocols, such as Hyper-File Transfer Protocol (HTTP), Simple Object Access Protocol (SOAP), XML, and so on, to programmatically embody the functionality of the Internet and Intranet, and Web services are considered to be component programming on the Web. Web services must provide a standard set of type systems for communicating different types of systems in different platforms, programming languages, and component models.
XML and XSD
Extensible Markup Language XML is the basic format for representing data in a Web service platform. In addition to being easy to establish and easy to analyze, the main advantage of XML is that it is platform-independent and vendor-independent. XML is created by the World Wide Web Association (SCHEMAXSD), which defines a standard set of data types and provides a language to extend this set of data types. The WEB service platform uses XSD as the data type system. When you use a language such as Java, C # to construct a Web service, all of the data types you use must be converted to an XSD type in order to conform to the Web service standard. If you want it to be passed between different organizations on different platforms and different software, you also need to wrap it up through the SOAP protocol.
SOAP
Soap is a Simple Object Access Protocol (PROTOCOL), which is a lightweight protocol for exchanging XML encoded information. It has three main aspects: Xml-envelope defines a framework for describing information content and how to handle content, encodes program objects as rules for XML objects, and executes remote procedure call (RPC) conventions. Soap can run on any other transport protocol. For example, you can use SMTP, the Internet e-mail protocol, to pass SOAP messages, which is tempting. The headers between the transport layers are different, but the XML payload remains the same. Web Service wants to implement a "software-software dialogue" between different systems can be called each other, breaking the software application, Web site and various devices in the state of incompatibility between the realization of "web-based seamless integration" goal.
WSDL
The Web Service Description Language WSDL is a formal description document provided in a machine-readable manner and XML-based language used to describe Web service and its functions, parameters, and return values. Because it is XML-based, WSDL is both machine-readable and human-readable.
Here's a two-part explanation. If you implement Java and. NET calls through a Web service.
First, use. NET as the server side, Java as a client implementation calls each other.
In the. NET system, using WCF as a new generation of service development tools is one of Microsoft's newest selling points, we use WCF as an example to implement server-side, first create a new Web site project, add a WCF service Personservice on the site. You will see PERSONSERVICE.SVC, Ipersonservice, PersonService.cs three files, wherein Ipersonservice is to expose an interface, the function of the interface is implemented by Personservice, and the client finds the service through PERSONALSERVICE.SVC and adds references to it.
In Personservice.svc, include only one row, which lists the implementation class for the service
<%@ ServiceHost language= "C #" debug= "true" service= "Service.personservice" codebehind= "~/app_code/ PersonService.cs "%>
Implementation of the service
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Runtime.Serialization;
Using System.ServiceModel;
Using System.Text;
Note: Using the rename command on the Refactor menu, you can change the interface name "Ipersonservice" in code and configuration files at the same time.
Namespace Service
{
[ServiceContract]
Publicinterface Ipersonservice
{
[OperationContract]
Ilist<person> GetList ();
}
Publicclass Personservice:ipersonservice
{
Public ilist<person> GetList ()
{
Ilist<person> personlist =new list<person> ();
Person Person1 =new person ();
person1.id = 0;
Person1. Age = 27;
Person1. Name = "Leslie";
Personlist.add (Person1);
Person Person2 =new person ();
Person2.id = 1;
Person2. Age = 23;
Person2. Name = "Rose";
Personlist.add (Person2);
Person Person3 =new person ();
Person3.id = 2;
Person3. Age = 29;
Person3. Name = "Jack";
Personlist.add (Person3);
return personlist;
}
}
}
In order to use person to achieve remote transfer, we must serialize the person, in WCF includes the service contract, data contract, message contract three parts, and the data contract is used to serialize the data, if you want to have a further understanding of WCF, Can be linked using WCF to implement SOA service-oriented programming
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Runtime.Serialization;
<summary>
Summary description of person
</summary>
Namespace Service
{
[DataContract]
Publicclass person
{
[DataMember]
Publicint ID
{
Get
Set
}
[DataMember]
Publicstring Name
{
Get
Set
}
[DataMember]
Publicint Age
{
Get
Set
}
}
}
There are several serialization methods in the data contract, including Datacontractserializer,netdatacontractserializer,xmlservializer,datacontractjsonserializer. In this case, the use of the most common DataContractSerializer, and DataContractJsonSerializer is now a more popular way, especially in the development of network projects, more use of JSON for data communication.
Finally, if you configure Web. config, you can successfully publish the WCF service
<?xml version= "1.0"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name= "ServiceBehavior" >
<servicemetadata httpgetenabled= "True"/>//note setting Httpgetenabled to true enables the client to successfully capture the service
<servicedebug includeexceptiondetailinfaults= "false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name= "Service.personservice" behaviorconfiguration= "ServiceBehavior" >//name property must correspond to the class name of the service implementation class
<endpoint address= "" binding= "BasicHttpBinding" contract= "Service.ipersonservice"/>//contract must correspond to contract name
<endpoint address= "Mex" binding= "mexHttpBinding" contract= "IMetadataExchange"/>//note open metadata to enable customers to download
</service>
</services>
</system.serviceModel>
</configuration>
The following uses MyEclipse8.6 for client development, first add a reference to the service, press CTRL + N to create a new project, select the Web service->web service client, click Next, Select Jax-WS on the framework and click Next
Enter the path to the service on the WSDL URL and add a Java Pagckage package myservices to the service, click Done so that the WCF service can successfully join the client.
Add a test class for this project, run to test
Package myassembly;
Import java.util.List;
Publicclass Test {
Publicstaticvoid Main (string[] args) {
Myservices.personservice service=new Myservices.personservice (); Get Service object
Myservices.ipersonservice Personservice=service.getbasichttpbindingipersonservice (); Binding a remote object through the BasicHttpBinding protocol
List<myservices.person> personlist=personservice.getlist (). Getperson ();
for (int n=0;n<personlist.size (); n++) {
System.out.println ("ID:" +personlist.get (N). GetID () + "Name:" +personlist.get (N). GetName (). toString () + "Age:" + Personlist.get (n). Getage ());
}
}
}
Second, use Java as the server side. NET calls each other as a client implementation.
There are many tools for developing Web service in Java, the most commonly used are axis, XFire, Netbean, etc., support jax-ws2.0 at Java-se above 6.0, and JAX-WS 2.0 is the update product of Jax-RPC 1.0. In Jax-WS, a remote call can be converted to an XML-based protocol such as soap. In the process of using JAX-WS, developers do not need to write any code that generates and processes SOAP messages. The JAX-WS runtime implementation translates calls to these APIs into a SOAP message. On the server side, the user simply needs to define the interface Sei (Service endpoint interface) required by the remote call through the Java language and provide the relevant implementation that can be published as a WebService interface by invoking the Jax-WS service Publishing interface. Below we will build a Web Service with Xfire.
First create a right-click on a project, select Myeclipse->add XFire Web Service capabilities, and reference the XFire Toolkit later. A webservices folder is automatically created in the project, and the Service.xml in the folder is configured for the publishing Web service.
Now build the good one service layer first
Create a model package that contains a value object person
Package Model;
Import java.io.Serializable;
Publicclass Person implements Serializable {
Privateint ID;
private String name;
Privateint age;
Publicint GetId () {
return ID;
}
Publicvoid setId (int id) {
This.id=id;
}
Public String GetName () {
return name;
}
Publicvoid setName (String name) {
This.name=name;
}
Publicint Getage () {
return age;
}
Publicvoid setage (int age) {
This.age=age;
}
}
Create a service package that contains the Services interface
Package Service;
Import java.util.List;
Import model.*;
Publicinterface Personservice {
List<person> GetList ();
}
Build a Serviceimpl package that implements the service
Package Serviceimpl;
Import model.*;
Import service.*;
Import java.util.*;
Publicclass Personserviceimpl implements personservice{
Public list<person> GetList () {
List<person> personlist=new linkedlist<person> ();
Person Person1=new person ();
Person1.setid (0);
Person1.setage (23);
Person1.setname ("Leslie");
Personlist.add (Person1);
Person Person2=new person ();
Person2.setid (1);
Person2.setage (30);
Person2.setname ("Mike");
Personlist.add (Person2);
return personlist;
}
}
Configure the service above the Service.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "http://xfire.codehaus.org/config/1.0" >
<service>
<name>PersonService</name>
<namespace>http://leslie-pc:8080/PersonService</namespace>
<serviceClass>
Service.personservice
</serviceClass>
<implementationClass>
Serviceimpl.personserviceimpl
</implementationClass>
</service>
</beans>
Its configuration features are as follows:
- Service
The service tag and the XML content it contains provide a complete description of the POJO published as a Web service.
- Name
The unique name that is used when the Web service is published.
- Namespace
The namespace used by the Web service when it is published.
- ServiceClass
The full name of the Web service interface class, including the package name and class name.
- Implemetationclass
The full name of the Web service implementation class, including the package name and class name.
You can now run the program, test the service, enter the service address HTTP://LESLIE-PC:8080/WEBSITE1/SERVICES/PERSONSERVICE?WSDL when testing, and the system will display the WSDL code
<?xml version= "1.0" encoding= "UTF-8"?>
-<wsdl:definitions targetnamespace= "Http://leslie-pc:8080/PersonService" xmlns:ns1= "Http://Model" xmlns: Soapenc12= "http://www.w3.org/2003/05/soap-encoding" xmlns:tns= "Http://leslie-pc:8080/PersonService" xmlns:wsdl= " http://schemas.xmlsoap.org/wsdl/"xmlns:xsd=" Http://www.w3.org/2001/XMLSchema "xmlns:soap11="/HTTP/ schemas.xmlsoap.org/soap/envelope/"xmlns:wsdlsoap=" http://schemas.xmlsoap.org/wsdl/soap/"xmlns:soapenc11=" http ://schemas.xmlsoap.org/soap/encoding/"xmlns:soap12=" Http://www.w3.org/2003/05/soap-envelope ">
-<wsdl:types>
-<xsd:schema xmlns:xsd= "Http://www.w3.org/2001/XMLSchema" attributeformdefault= "qualified" elementformdefault= "Qualified" targetnamespace= "Http://leslie-pc:8080/PersonService" >
-<xsd:element name= "GetList" >
<xsd:complextype/>
</xsd:element>
-<xsd:element name= "Getlistresponse" >
-<xsd:complexType>
-<xsd:sequence>
<xsd:element maxoccurs= "1" minoccurs= "1" name= "Out" nillable= "true" type= "Ns1:arrayofperson"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
-<xsd:schema xmlns:xsd= "Http://www.w3.org/2001/XMLSchema" attributeformdefault= "qualified" elementformdefault= "Qualified" targetnamespace= "Http://Model" >
-<xsd:complextype name= "Arrayofperson" >
-<xsd:sequence>
<xsd:element maxoccurs= "unbounded" minoccurs= "0" name= "person" nillable= "true" type= "Ns1:person"/>
</xsd:sequence>
</xsd:complexType>
-<xsd:complextype name= "Person" >
-<xsd:sequence>
<xsd:element minoccurs= "0" name= "Age" type= "Xsd:int"/>
<xsd:element minoccurs= "0" name= "id" type= "xsd:int"/>
<xsd:element minoccurs= "0" name= "name" nillable= "true" type= "xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
-<wsdl:message name= "Getlistrequest" >
<wsdl:part name= "Parameters" element= "Tns:getlist"/>
</wsdl:message>
-<wsdl:message name= "Getlistresponse" >
<wsdl:part name= "Parameters" element= "Tns:getlistresponse"/>
</wsdl:message>
-<wsdl:porttype name= "Personserviceporttype" >
-<wsdl:operation name= "GetList" >
<wsdl:input name= "Getlistrequest" message= "Tns:getlistrequest"/>
<wsdl:output name= "Getlistresponse" message= "Tns:getlistresponse"/>
</wsdl:operation>
</wsdl:portType>
-<wsdl:binding name= "personservicehttpbinding" type= "Tns:personserviceporttype" >
<wsdlsoap:binding style= "Document" transport= "Http://schemas.xmlsoap.org/soap/http"/>
-<wsdl:operation name= "GetList" >
<wsdlsoap:operation soapaction= ""/>
-<wsdl:input name= "Getlistrequest" >
<wsdlsoap:body use= "literal"/>
</wsdl:input>
-<wsdl:output name= "Getlistresponse" >
<wsdlsoap:body use= "literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
-<wsdl:service name= "Personservice" >
-<wsdl:port name= "Personservicehttpport" binding= "tns:personservicehttpbinding" >
<wsdlsoap:address location= "Http://leslie-pc:8080/WebSite1/services/PersonService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
The server-side test has been successful and is now in use. NET to invoke the service, right-click on the project, select Add Web Service, enter the address of the service on the URL address http://leslie-pc:8080/WebSite1/services/PersonService?wsdl , enter the code on one page to test.
protectedvoid Page_Load (object sender, EventArgs e)
{
Service.personservice personservice =new service.personservice ();
ilist<service.person> personlist = Personservice.getlist ();
foreach (Service.person person in personlist)
{
Response.Write ("ID:" + person.id.ToString () + "name:" + Person.name + "Age:" + person.age.ToString () + "<br/>");
}
}
If the test is successful, congratulations, you've learned how Java and. NET are called each other through Web services. But because Web services are inherently non-limited by the language of development, it is not a challenge to make calls to each other through Web services, as long as you have some knowledge of Java and. Net. But often in some erp,oa development process, will use TCP/IP sockets in many cases to achieve the functionality of the software, TCP/IP This "old guy" why the use of such a long time will often see its figure, because the use of TCP/IP is more efficient, and easy to pass through the firewall barrier, The HTTP protocol is also built on a TCP/IP basis. In the next chapter, we'll show you how Java and. NET are called each other through TCP/IP sockets.
Original code: (due to limited upload space, the Java project can not upload the. metadata, please set up a Java projects project, then add the original code to run successfully)
Java server,. NET Client
. NET Server,. Java Client
Mutual invocation of Java and. Net--using JNBridge bridging mode for remote communication
Java and. NET Mutual invocation--TCP/IP sockets to call each other's basic schema (with the original code)
Mutual invocation of Java and. NET-calls through Web services (with original code)
Mutual invocation of Java and. NET--invoking each other through Web services