Delphi and C # Java implement the WebService architecture 1, 2

Source: Internet
Author: User

Http://www.360doc.com/content/070519/12/10584_508473.html

Http://www.cnblogs.com/jin20000/archive/2009/01/08/1371720.html

Use
Creating a WebService in Java is quick, but it is always inferior to creating a client in Java.
Delphi (in my opinion, Java experts should not laugh at me), then we use Java to implement the WebService server program and Delphi to create the client.

1. Environment Construction


I will not introduce the installation and configuration of JDK and tomcat here. Note that my tomcat is installed on D:/tomcat and the port is set to 8099.

 

Install axis
1. Can be from http://ws.apache.org/axis/
Download the latest version of Axis. I am using axis1.2.
2.decompress axis-bin-1_2beta.zip and copy all the axis directories in the webapps directory in the axis-1_2beta directory to webapps In the tomcat installation directory.
3. Add all jar files in axis/lib to the classpath system variables.

Note ):

.;
D:/tomcat/webapps/axis/webinf/lib/axis. jar;
D:/tomcat/webapps/axis/WEB-INF/lib/SAAJ. jar;
D:/tomcat/webapps/axis/WEB-INF/lib/commons-logging.jar;
D:/tomcat/webapps/axis/WEB-INF/lib/commons-discovery.jar;
D:/tomcat/webapps/axis/WEB-INF/lib/wsdl4j. jar;
D:/tomcat/webapps/axis/webinf/lib/jaxrpc. jar;

4. Start tomcat to access http: // cbf107: 8099/axis/index.html
(The host name and port number used here are the same as the tomcat configuration used ).
You can see the following page:


 

2. Web Service server development


Write a Java Applet:


Public class test {
Public int add (INT P1, int P2 ){
Return P1 + P2;
}
Public int subtract (INT P1, int P2 ){
Return P1-P2;
}
}

Create a test folder in the Directory D:/tomcat/webapps/axis and save it. Note: Change the suffix to JWS, that is, test. JWS.

Access: http: // cbf107: 8099/axis/test. JWS in a browser
We can see that


 

Click "click to see the WSDL ".


 
Save this page as an XML document (we will use it when using Delphi to create a client)


 

Iii. Delphi web service client development

Open the new project in Delphi, place two edit and two buttons on the form, and name the button name
Btnadd and btnsubtract. caption is named "add" and "Subtract.

Choose File> New> Other and select the WSDL importer in WebService.

In search UDDI .., select the XML file we saved earlier and click Next to finish.



 

Save all. We found that test1.pas is added to the Delphi project.


 

The following interface statement is displayed:
Test = interface (iinvokable)
['{52882ff0-46e2-6644-a77b-ce714d4cbfd5}']
Function add (const P1: integer; const P2: integer): integer; stdcall;
Function subtract (const P1: integer; const P2: integer): integer; stdcall;
End;

Then we can directly call this interface. At this time, we will return to the interface and place a soap on the form from the WebServices panel of Delphi.


 

Set attributes

 


 

Finally, we can complete our code:
1. Add in usesTest1
Because we need to reference this unit.
2. Add global variablesT1: test;
(T1 is a test interface)
3. Add a click event for two buttons

Procedure tform1.btnaddclick (Sender: tobject );
VaR
I, j, Z: integer;
Begin
I: = strtoint (edit1.text );
J: = strtoint (edit2.text );
T1: = (httprio1 as test );
Z: = t1.add (I, j );
T1. _ release;
Showmessage (inttostr (z ));
End;



Procedure tform1.btnsubstractclick (Sender: tobject );
VaR
I, j, Z: integer;
Begin
I: = strtoint (edit1.text );
J: = strtoint (edit2.text );
T1: = (httprio1 as test );
Z: = t1.subtract (I, j );
T1. _ release;
Showmessage (inttostr (z ));
End;

4. Run the F9 program for testing.


 


 

We can see that we use Delphi to implement the Java program, right.

5. Maybe you think it should have been done here. That's right! However, I do not know enough about WebService.
You can modify the Java program again, that is, the method in test. JWS.

Public int add (INT P1, int P2 ){
Return P1 + P2;
}


Change

Public int add (INT P1, int P2 ){
Return P1 + p2 + 10;
}


Run your client program again.

Iv. Note: all codes of unit1

Unit unit1;

Interface

Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls, Rio, soaphttpclient, test1;

Type
Tform1 = Class (tform)
Httprio1: thttprio;
Edit1: tedit;
Edit2: tedit;
Btnadd: tbutton;
Btnsubstract: tbutton;
Procedure edit2keypress (Sender: tobject; var key: Char );
Procedure btnaddclick (Sender: tobject );
Procedure btnsubstractclick (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;

VaR
Form1: tform1;
T1: test;

Implementation

{$ R *. DFM}

// This is to allow two edits to only enter numbers. You can directly call onkeypress of edit1.
Procedure tform1.edit2keypress (Sender: tobject; var key: Char );
Begin
If key in ['0'... '9'] = false then
Key: = #0;
End;

Procedure tform1.btnaddclick (Sender: tobject );
VaR
I, j, Z: integer;
Begin
I: = strtoint (edit1.text );
J: = strtoint (edit2.text );
T1: = (httprio1 as test );
Z: = t1.add (I, j );
T1. _ release;
Showmessage (inttostr (z ));
End;

Procedure tform1.btnsubstractclick (Sender: tobject );
VaR
I, j, Z: integer;
Begin
I: = strtoint (edit1.text );
J: = strtoint (edit2.text );
T1: = (httprio1 as test );
Z: = t1.subtract (I, j );
T1. _ release;
Showmessage (inttostr (z ));
End;

End.

 

Record the use and debugging of WebService. Memo.

Because jb2005 + axis is used to publish WebService, there are always some strange problems, so jb9 is used.

(1) Use jb9 + axis to publish the WebService


1. Create a project -- Project, and create a web -- web application;

2. Create a Web Service -- web services configuration;

3. Select the name of the newly created web application for the webapp;

4. Create a WebService server class (test. Java ).

-----------------------------

Public class test {
Private Static int;
Private Static int B;

Public test (){
}

Public void Seta (int x ){
This. A = X;
}

Public void SETB (INT y ){
This. B = y;
}

Public int getsum (){
Int c = A + B;
// System. Out. println ("A:" + );
// System. Out. println ("B:" + B );
// System. Out. println ("C:" + C );
Return C;
}
}

-----------------------------

5. Publish the WebService

(1) Right-click the class to be published and select "export as a Web service ";

(2) If "generate client stub" is selected, the client class and Interface Test class are generated at the same time after the release. You can skip this option;

(3) In "deploy scope", you can select this option as "session". Other Default values are as follows;

(4) Select "allow selected methods" in "selection mode" and select the interface method to be released in "methods in. You can select multiple.

(5) by default, click "finish" to generate the Web Service Description Language file.

6. Note

Class attributes use the Private Static modifier. For example, "Private Static int ".

Class methods cannot use static modifiers, such as "Public int getsum ()", because static class methods cannot be published as WebService interfaces.

 

(2) Use Delphi7 to compile the WebService Client

1. Create a project & Application;

2. New-other-WebService-WSDL importer, select the WSDL description file of the WebService Service, or select the URL of the WSDL file;

3. Click "finish" to generate the *. Pas unit file corresponding to the WSDL file;

The content is roughly as follows:

------------------------------------------------------
Test = interface (iinvokable)
['{787ce852-ef00-40ba-5802-9bf9dfbf79f0}']
Procedure Seta (const A: integer); stdcall;
Procedure SETB (const B: integer); stdcall;
Function getsum: integer; stdcall;
End;

Function gettest (usewsdl: Boolean = system. False; ADDR: String = ''; httprio: thttprio = nil): test;

Implementation

Function gettest (usewsdl: Boolean; ADDR: string; httprio: thttprio): test;
Const
Defwsdl = 'HTTP: // 127.0.0.1: 8088/test/services/test? WSDL ';
Defurl = 'HTTP: // 127.0.0.1: 8088/test/services/test ';
Defsvc = 'testservice ';
Defprt = 'test ';
VaR
Rio: thttprio;
Begin
Result: = nil;

If (ADDR = '') then
Begin
If usewsdl then
ADDR: = defwsdl
Else
ADDR: = defurl;
End;

If httprio = nil then
Rio: = thttprio. Create (nil)
Else
Rio: = httprio;

Try
Result: = (Rio as test );
If usewsdl then
Begin
Rio. wsdllocation: = ADDR;
Rio. Service: = defsvc;
Rio. Port: = defprt;
End else
Rio. url: = ADDR;
Finally
If (result = nil) and (httprio = nil) then
Rio. Free;
End;
End;

Initialization
Invregistry. registerinterface (typeinfo (test), 'HTTP: // 127.0.0.1: 8088/test/services/test', 'utf-8 ');
Invregistry. registerdefaultsoapaction (typeinfo (test ),'');

End.

------------------------------------------------------

4. Suggestion: Rename the *. Pas unit file generated by the WSDL file (webtest. Pas). It is best not to use the default name;

5. the WebService client Call Code Compiled by Delphi7:

-----------------------------
Uses webtest;

VaR atest: Test; // test class
Sum: integer;
Begin
Atest: = gettest (); // The *. Pas unit file corresponding to the uses WSDL.
Atest. Seta (1 );
Atest. SETB (2 );
Sum: = atest. getsum ();
Showmessage (inttostr (SUM ));
End;

-----------------------------

6. Note

If the static description is not added to the properties of the server class, the client written in Delphi7 cannot get the correct result when it uses Seta () and SETB () and then getsum.

Because WebService is connectionless, a new object application is obtained every time class methods are used. The getsum () operation after Seta () and SETB () operation is not for the same object.

However, after static is added, all objects share the same value of the class. In this case.

In addition, if "session" is selected in "deploy scope" When jb9 + axis releases WebService, instead of the default "request ",

If the attribute of the server class does not use the static constraint, you can use Seta () and SETB () on the Delphi client and then getsum () to obtain the correct return value.

Magic. Pai_^

7. Suggestions

This example only tests and solves the problem. If you want to transmit data to the WebService server, it is best to use parameters in interface methods.

For example, if you change it to getsum (int x, int y) and then release the interface, you can use an interface method to complete the operation, instead of using the set method to pass the parameter data.

 

(3) Use jb9 to compile the WebService Client

1. Create a project -- project;

2. Create a Web Service -- import a web service;

3. Select the URL of the WSDL file in the wsdl url or the WSDL description file of the WebService Service;

4. Because it is a WebService client, you can select "generate server-side classes;

5. In "package options", you can change "name" to a proper name;

6. Click "finish" to generate the jajva class file of the client. Generally, there are 6 files with their own naming rules. See JB.

7. WebService client test class written in jb9

-----------------------------

Public class client {

Public client (){
}

Public static void main (string [] ARGs ){
Try {
Testservice service = new testservicelocator ();
Test aclient = service. gettest ();
Aclient. Seta (1 );
Aclient. SETB (2 );
Int sum = aclient. getsum ();
System. Out. println ("sum is:" + sum );
}
Catch (exception e ){
System. Err. println ("execution failed. Exception:" + E );
}
}

-----------------------------

8. Note

If the attribute of the server class is not added with the static description, the client compiled by JB cannot get the correct result when it uses Seta (), SETB (), and then getsum.

We recommend that you use an interface with parameters to transmit parameter data. Avoid using the set method.

(4) use Delphi7 to call the WebService written in C #

When a client compiled by Delphi7 calls the WebService compiled by C #, garbled characters may occur if the parameters passed in the function are Chinese characters.

Search for some materials online and find a solution: Change the useutf8inheader attribute of httprio to true.

Edit the *. Pas unit file generated by the WSDL file. Add the following sentence to the get function: Rio. httpwebnode. useutf8inheader: = true.

The code snippets are roughly as follows:

------------------

Try
Rio. httpwebnode. useutf8inheader: = true; // Add a sentence and modify the encoding scheme.
Result: = (Rio as test );
If usewsdl then
Begin
Rio. wsdllocation: = ADDR;
Rio. Service: = defsvc;
Rio. Port: = defprt;
End else
Rio. url: = ADDR;
Finally
If (result = nil) and (httprio = nil) then
Rio. Free;
End;

----------------

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.