1.下载最新的Hessian的jar文件(我们用的是for java),地址为:http://hessian.caucho.com/
2.用eclipse新建一个web工程,命名为:WebTest;把第一步下载的jar文件放入该工程
3.新建一个接口和三个类,运行BasicClient类即可在控制台看到效果
接口 IBasic :
- public interface IBasic {
- public String hello();
- public Car getCar();
- }
类 Car:
- import java.io.Serializable;
- public class Car implements Serializable{
- /** TODO */
- private static final long serialVersionUID = 1L;
- private String name;
- private String length;
- private String color;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getLength() {
- return length;
- }
- public void setLength(String length) {
- this.length = length;
- }
- public String getColor() {
- return color;
- }
- public void setColor(String color) {
- this.color = color;
- }
- }
类 BasicService :
- public class BasicService implements IBasic {
- public Car getCar() {
- Car c = new Car();
- c.setColor("红色");
- c.setLength("2m");
- c.setName("火车");
- return c;
- }
- private String hello = "Hello World";
- public String hello() {
- return hello;
- }
- }
类 BasicClient :
- import java.net.MalformedURLException;
- import com.caucho.hessian.client.HessianProxyFactory;
- public class BasicClient {
- public static void main(String[] args){
- String url = "http://127.0.0.1:8081//WebTest/hello";
- HessianProxyFactory factory = new HessianProxyFactory();
- IBasic basic;
- try {
- basic = (IBasic) factory.create(IBasic.class, url);
- Car car = basic.getCar();
- System.out.println("Hello: " + basic.hello());
- System.out.println("color: " + car.getColor());
- System.out.println("length: " + car.getLength());
- System.out.println("name: " + car.getName());
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- }
- }
web.xml文件改成如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
- <display-name>WebTest</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>default.html</welcome-file>
- <welcome-file>default.htm</welcome-file>
- <welcome-file>default.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>BasicService</param-value>
- </init-param>
- <init-param>
- <param-name>home-api</param-name>
- <param-value>IBasic</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>hello</servlet-name>
- <url-pattern>/hello</url-pattern>
- </servlet-mapping>
- </web-app>