Hessian 简单例子

hello.hessian.MyCar.java

package hessian.pojo;

import java.io.Serializable;


public class MyCar implements Serializable {

    private static final long serialVersionUID = 4736905401908455439L;

    private String carName;

    private String carModel;

    public String getCarName() {
        return carName;
    }

    public String getCarModel() {
        return carModel;
    }

    public void setCarName(String pCarName) {
        carName = pCarName;
    }

    public void setCarModel(String pCarModel) {
        carModel = pCarModel;
    }

    @Override
    public String toString() {
        return "my car name:[" + this.carName + "] model:[" + this.carModel
                + "].";
    }
}

 

 

hessian.service.HelloHessian.java

package hessian.service;

import hessian.pojo.MyCar;

import java.util.List;
import java.util.Map;

public interface HelloHessian {
    String sayHello();

    MyCar getMyCar();

    List myLoveFruit();

    Map myBabays();
}

 

hessian.service.impl.HelloHessianImpl.java

package hessian.service.impl;

import com.caucho.hessian.server.HessianServlet;
import hessian.pojo.MyCar;
import hessian.service.HelloHessian;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HelloHessianImpl extends HessianServlet implements HelloHessian {

    public MyCar getMyCar() {
        MyCar car = new MyCar();
        car.setCarName("阿斯顿·马丁");
        car.setCarModel("One-77");
        return car;
    }

    public Map myBabays() {
        Map map = new HashMap();
        map.put("son", "孙吴空");
        map.put("daughter", "孙小美");
        return map;
    }

    public List myLoveFruit() {
        List list = new ArrayList();
        list.add("apple");
        list.add("kiwi");
        list.add("orange");
        return list;
    }

    public String sayHello() {
        return "welcom to Hessian";
    }

}

 

hessian.Main.java

package hessian;

import com.caucho.hessian.client.HessianProxyFactory;
import hessian.service.HelloHessian;

public class Main {

    public static String url = "http://localhost:8888/InfoWeb/Hello";

    public static void  main(String[] args)throws Exception{
        HessianProxyFactory factory = new HessianProxyFactory();

        HelloHessian helloHession = (HelloHessian) factory.create(HelloHessian.class, url);

        System.out.println(helloHession.sayHello());
        System.out.println(helloHession.getMyCar());
        System.out.println(helloHession.myBabays());
        System.out.println(helloHession.myLoveFruit());
    }
}

 

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
	
	<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>hession.service.impl.HelloHessianImpl</param-value>
		</init-param>
		<init-param>
			<param-name>home-api</param-name>
			<param-value>hession.service.HelloHessian</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>Hello</servlet-name>
		<url-pattern>/Hello</url-pattern>
	</servlet-mapping>
</web-app>

 

pom.xml

    <dependencies>
        <!--=========================== hession =========================== -->
        <dependency>
            <groupId>com.caucho</groupId>
            <artifactId>hessian</artifactId>
            <version>4.0.33</version>
        </dependency>

        <!--=========================== Servlet Api ===========================-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

 

你可能感兴趣的:(java,Web,service,hessian)