当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
Java远程调用之Hessian简例(2)
发布时间:2010/7/14 10:35:13 来源:城市学习网 编辑:ziteng
  代码如下:
  RomoteTest.java
  package client;
  import java.net.MalformedURLException;
  import server.Hello;
  import com.caucho.hessian.client.HessianProxyFactory;
  public class RomoteTest {
  public static void main(String[] args) {
  String url = "http://localhost:8080/Hessian/hessianService";
  HessianProxyFactory factory = new HessianProxyFactory();
  try {
  Hello hello = (Hello) factory.create(Hello.class, url);
  System.out.println(hello.hello("kingtoon"));
  } catch (MalformedURLException e) {
  e.printStackTrace();
  }
  }
  }
  HelloImpl.java
  package server.impl;
  import server.Hello;
  public class HelloImpl implements Hello {
  public String hello(String name) {
  return "hello, " + name + "! Welcome to use Hessian.";
  }
  }
  Hello.java
  package server;
  public interface Hello {
  String hello(String name);
  }
  web.xml
  <?xml version="1.0" encoding="UTF-8"?>
  <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
  <servlet-name>hessianService</servlet-name>
  <servlet-class>
  com.caucho.hessian.server.HessianServlet
  </servlet-class>
  <init-param>
  <param-name>service-class</param-name>
  <param-value>server.impl.HelloImpl</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>hessianService</servlet-name>
  <url-pattern>/hessianService</url-pattern>
  </servlet-mapping>
  </web-app>
  所需jar包:本文使用hessian-3.2.1.jar,仅此而已。
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved