建立web服务并发布

1.3 建立web服务并发布


1.3.1 web.xml中配置XFire的Servlet及其配置
当创建web service时自动会在web.xml配置文件中增加如下XFire的Servlet配置代码:
<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>
1.3.2 配置XFire的配置文件services.xml
当创建一个名为MyFirstService的web service时自动创建两个java文件:IMyFirstService接口和MyFirstServiceImpl实现接口的类,并在services.xml配置文件中增加了如下代码:
<service>
 <name>MyFirstService</name>
 <serviceClass>com.service.IMyFirstService</serviceClass>
 <implementationClass>
  com.service.MyFirstServiceImpl
 </implementationClass>
 <style>wrapped</style>
 <use>literal</use>
 <scope>application</scope>
</service>


1.4 建立客户程序,调用web服务
1.4.1 客户程序的关键代码
需要导入的包如下:
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.XFire;
import org.codehaus.xfire.client.XFireProxyFactory;
import com.service.IMyFirstService;   //web service 接口
关键代码如下:
try
{
 ObjectServiceFactory objFactory=new ObjectServiceFactory();
 Service smodel=objFactory.create(IMyFirstService.class);
 XFireFactory fireFactory=XFireFactory.newInstance();
 XFire fire=fireFactory.getXFire();
 XFireProxyFactory fireProxyFactory=new XFireProxyFactory(fire);
String wsdl="http://localhost:8080/webservicedemo/services/MyFirstService";
IMyFirstService service=(IMyFirstService)fireProxyFactory.create(smodel, wsdl);
 System.out.println(service.sum(20, 30));
 System.out.println("complete");
}
catch(Exception ex)
{
 ex.printStackTrace();
}

你可能感兴趣的:(Web,xml,servlet)