关于二进制Web服务框架Hessian最简单代码实例
在resin3.1 下测试通过
hessian版本: hessian-3.1.6.jar
官方地址:
http://hessian.caucho.com/#Java
步骤如下:
1. 接口定义
package com.yanek.study.hessian;
public interface Hello {
public String hello(String name);
}
2.接口实现
package com.yanek.study.hessian;
public class HelloImpl implements Hello {
public String hello(String name)
{
return "hello " + name + " welcome study Hessian";
}
}
3. web.xml配置
配置HessianServlet ,配置提供服务的类
<servlet>
<servlet-name>hessianService</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>service-class</param-name>
<param-value>com.yanek.study.hessian.HelloImpl</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>hessianService</servlet-name>
<url-pattern>/hessianService</url-pattern>
</servlet-mapping>
4. 客户端测试类:
package com.yanek.study.hessian;
import java.net.MalformedURLException;
import com.caucho.hessian.client.HessianProxyFactory;
public class HessianClient {
/**
* @param args
*/
public static void main(String[] args) {
// Hessian服务的url
String url = "http://study.myweb.com/hessianService";
// 创建HessianProxyFactory实例
HessianProxyFactory factory = new HessianProxyFactory();
// 获得Hessian服务的远程引用
Hello d;
try {
d = (Hello) factory.create(Hello.class, url);
// 调用远程服务。
System.out.println("下面调用Hessian服务: " + d.hello("yanek"));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}