天天看點

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>