Delphi and Java Integration us…
  • RSS Feed

Last modified on 7/7/2009 5:51 PM by User.

Tags:

Delphi and Java Integration using Web Services

Introduction

This tutorial provides a very simple and quick introduction to the NetBeans IDE workflow by walking you through the creation of a simple Java web service application.

  1. Create a simple Web Service in Java
  2. Create a Delphi client for the Web Service

1. Create a simple Web Service in Java:

  • Start NetBeans IDE.
  • In the IDE, choose File > New Project (Ctrl-Shift-N).
  • In the New Project wizard, expand the Java category and select Java Application. Then click Next.
  • In the Name and Location page of the wizard, do the following:
    • In the Project Name field, type MyWebServiceApp.
    • Leave the Use Dedicated Folder for Storing Libraries checkbox unselected.
    • In the Create Main Class field, type messageserver.MyWebServiceApp.
    • Leave the Set as Main Project checkbox selected.
  • Click Finish.

The project is created and opened in the IDE. You should see the following components:

  • The Projects window, which contains a tree view of the components of the project, including source files, libraries that your code depends on, and so on.
  • The Source Editor window with a file called MyWebServiceApp open.

Adding Code to the Generated Source File

Because you have left the Create Main Class checkbox selected in the New Project wizard, the IDE has created a skeleton class for you. You can add the web service code to the skeleton code by replacing the line:

            // TODO code application logic here
with the lines:
        Endpoint endpoint = Endpoint.publish("http://localhost:8090/MyWebService?wsdl",
new MyWebService());
JOptionPane.showMessageDialog(null, "Shutdown Server");
endpoint.stop();

and add missing import statements after the package line. The complete class source code should now look like this:

     
package messageserver;

import javax.swing.JOptionPane;
import javax.xml.ws.Endpoint;

/**
* MyWebServiceApp
*
*/

public class MyWebServiceApp {
public static void main(String[] args) {
Endpoint endpoint = Endpoint.publish("http://localhost:8090/MyWebService?wsdl",
new MyWebService());
JOptionPane.showMessageDialog(null, "Shutdown Server");
endpoint.stop();
}
}

Create the Web Service class:

Now create a new class for the service implementation. The service will be very simple and contains only one method, getEcho.

  • In the IDE, choose File > New File.
  • In the New File wizard, expand the Java category and select Java Class. Then click Next.
  • In the Name and Location page of the wizard, do the following:
    • In the Project Name field, type MyWebService.
    • In the Package field, type messageserver.
  • Click Finish.

Copy and paste the following class definition. The complete class source code should now look like this:

package messageserver;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@WebService(name = "MyWebService", serviceName = "MyService")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class MyWebService {

public MyWebService() { }

@WebMethod
public String getEcho(String data) {
// code
return data;
}
}

Start the Web Service

  • In the IDE, choose Run > Run Main Project (F6).

2. Create a Delphi client for the Web Service

  • Start Delphi IDE.
  • In the IDE, choose File > New > VCL Forms Application - Delphi.
  • In the IDE, choose File > New > Other ...
  • In the New Items wizard, expand the Delphi Projects > WebServices category and choose WSDL Importer. Then click Ok.
  • In the WSDL Import Wizard, do the following:
    • In the Location field, type http://localhost:8090/MyWebService?wsdl.
  • Click Next.
  • Click Finish.
  • Open the main form unit.
  • Place two TEdit, one TButton and one THTTPRio component on the form.
  • Save all files.
  • Choose File > Use Unit ... and select MyWebService1. Then click Ok.
  • Click on the HTTPRIO component.
  • In the IDE, choose View > Object Inspector (F11).
  • In the object inspector, do the following:
    • In the WSDLLocation field, type http://localhost:8090/MyWebService?wsdl.
  • Double click on the button component.
  • In the source code editor, replace the event handler by the following code:
procedure TForm1.Button1Click(Sender: TObject);
var
Port: MyWebService;
begin
Port := HTTPRIO1 as MyWebService;
Edit2.Text := Port.getEcho(Edit1.Text);
end;