(一)远程服务端
服务接口
package server;
public interface Hello {
String hello(String name);
}
接口实现类
package server;
public class HelloImpl implements Hello{
public String hello(String name) {
return hello+name+! 欢迎学习Hessian;
}
}
在服务器端配置webxml
<?xml version= encoding=UTF?>
<webapp version=
xmlns=
xmlns:xsi=instance
xsi:schemaLocation=
app__xsd>
<servlet>
<servletname>hessianService</servletname>
<servletclass>comcauchohessianserverHessianServlet</servletclass>
<initparam>
<paramname>serviceclass</paramname>
<paramvalue>serverHelloImpl</paramvalue>
</initparam>
<loadonstartup></loadonstartup>
</servlet>
<servletmapping>
<servletname>hessianService</servletname>
<urlpattern>/hessianService</urlpattern>
</servletmapping>
</webapp>
还要导入jar包从Hessian官网上下载放入lib文件夹下
为了方便将Hello接口及实现类打包成jar包以供客户端使用在这里打包成hellojar
至此 服务器端 完成
注:服务器端IP是
(二)客户端
通过服务器端暴露的服务接口来进行远程调用服务
程序如下:
package client;
import MalformedURLException;
import serverHello;
import comcauchohessianclientHessianProxyFactory;
public class RomoteTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Autogenerated method stub
//hessian服务的url 其中hessianv是项目名
String url = v/hessianService;
//创建HessianProxyFactory实例
HessianProxyFactory factory = new HessianProxyFactory();
//获得Hessian服务的远程引用
try {
Hello hello = (Hello)factorycreate(Helloclassurl);
Systemoutprintln(hellohello(kingtoon));
} catch (MalformedURLException e) {
// TODO Autogenerated catch block
eprintStackTrace();
}
}
}
因为要用到接口Hello所以需要将Hellojar导入到lib下
测试结果如下:
hellokingtoon! 欢迎学习Hessian