环境:Win7、Delphi7、JDK6、Tomcat6、XFire1.2
Web服务端:
Iweather.java
package net.ly.webservice.biz;
public interface Iweather {
String weather(String s);
String weather2();
}
WeatherImpl.java
package net.ly.webservice.impl;
import net.ly.webservice.biz.Iweather;
public class WeatherImpl implements Iweather {
public String weather(String s) {
return "Hello " + s ;
}
public String weather2() {
return "abcde2";
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
services.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>Weather</name>
<namespace>www.weather.com/Weather</namespace>
<serviceClass>net.ly.webservice.biz.Iweather</serviceClass>
<implementationClass>net.ly.webservice.impl.WeatherImpl</implementationClass>
</service>
</beans>
Web服务目录结构如图:
部署,启动Tomcat。
Delphi代码部分:
cmd打开DOS窗口,cd进入 D:\Program Files\Borland\Delphi7\Bin
WSDLImp http://localhost:8686/myWebService/services/Weather?wsdl
在当前目录生成Weather.pas
新建Delphi项目,保存项目到文件夹javawebservice,把Weather.pas再拷贝到这个文件夹,然后加入到项目中。
在Unit1文件引用Weather,
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Weather;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
s,s2:string;
begin
s:=GetWeatherPortType().weather('dai');
showmessage(s);
s2:=GetWeatherPortType().weather2;
showmessage(s2);
end;
end.
Delphi程序运行界面:
Web应用和Delphi桌面应用结合,对现实项目开发,解决方案更加灵活,能够打出一套漂亮的组合拳。