当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
Java远程调用之Hessian简例(1)
发布时间:2010/7/14 10:34:44 来源:城市学习网 编辑:ziteng
  (一)远程服务端
  1.服务接口
  package server;
  public interface Hello {
  String hello(String name);
  }
  2.接口实现类
  package server;
  public class HelloImpl implements Hello{
  public String hello(String name) {
  return "hello,"+name+"! 欢迎学习Hessian";
  }
  }
  在服务器端配置web.xml
  xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  hessianService
  com.caucho.hessian.server.HessianServlet
  service-class
  server.HelloImpl
  1
  hessianService
  /hessianService
  还要导入jar包,从Hessian官网上下载,放入lib文件夹下.
  为了方便,将Hello接口及实现类打包成jar包,以供客户端使用,在这里打包成hello.jar.
  至此 服务器端 完成.
  注:服务器端IP是http://192.168.1.104:8081 [NextPage]   (二)客户端
  通过服务器端暴露的服务接口,来进行远程调用服务.
  程序如下:
  package client;
  import java.net.MalformedURLException;
  import server.Hello;
  import com.caucho.hessian.client.HessianProxyFactory;
  public class RomoteTest {
  /**
  * @param args
  */
  public static void main(String[] args) {
  // TODO Auto-generated method stub
  //hessian服务的url 其中hessian-v1是项目名
  String url = "http://192.168.1.104:8081/hessian-v1/hessianService";
  //创建HessianProxyFactory实例
  HessianProxyFactory factory = new HessianProxyFactory();
  //获得Hessian服务的远程引用
  try {
  Hello hello = (Hello)factory.create(Hello.class,url);
  System.out.println(hello.hello("kingtoon"));
  } catch (MalformedURLException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
  }
  }
  因为要用到接口Hello,所以需要将Hello.jar导入到lib下.
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved