天天看点

hessian学习

  hessian是个轻量级的玩意,介绍为:http://hessian.caucho.com/doc/hessian-overview.xtp

一个典型的例子如下,先建立web工程

接口如下:

package hessian;

public interface IBasic {   

    /**  

     * 测试字符串  

     * @return  

     */  

    public String hello();   

     * 取一辆汽车   测试对象传递  

    public Car getCar();   

}  

实现:

public class BasicService implements IBasic {   

   private String hello= "Hello, world";   

   public String hello()   

   {   

     return hello;   

   }   

     public Car getCar() {   

         Car car = new Car();   

         car.setColor("RED红色");   

         car.setLength("2400");   

         car.setName("HAHACHE");   

         return car;   

     }   

 }  

注意car中要实现序列化,否则出错

import java.io.Serializable; 

public class Car implements Serializable {

  private String color;

  private String length;

  private String name;

public String getColor() {

 return color;

}

public void setColor(String color) {

 this.color = color;

public String getLength() {

 return length;

public void setLength(String length) {

 this.length = length;

public String getName() {

 return name;

public void setName(String name) {

 this.name = name;

客户端:

import com.caucho.hessian.client.HessianProxyFactory;

public class BasicClient {   

    public static void main(String []args)   

        throws Exception   

      {   

        String url = "http://localhost:8085/HessianDemo1/hello";   

        HessianProxyFactory factory = new HessianProxyFactory();   

        IBasic basic = (IBasic) factory.create(IBasic.class, url);   

          Car car   = basic.getCar();   

          System.out.println("Hello: " + basic.hello());   

        System.out.println("Hello: " + car.getName());   

      }   

web.xml:

<web-app version="2.4"

 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">

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  <servlet>  

    <servlet-name>hello</servlet-name>  

    <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>

    <init-param>  

        <param-name>home-class</param-name>  

        <param-value>hessian.BasicService</param-value>  

    </init-param>  

     <init-param>  

        <param-name>home-api</param-name>  

        <param-value>hessian.IBasic</param-value>  

</servlet>