简单的Hessian实例

1.下载最新的Hessian的jar文件(我们用的是for java),地址为:http://hessian.caucho.com/

2.用eclipse新建一个web工程,命名为:WebTest;把第一步下载的jar文件放入该工程

3.新建一个接口和三个类,运行BasicClient类即可在控制台看到效果

 接口 IBasic :

 

[java]  view plain copy
  1. public interface IBasic {  
  2.     public String hello();  
  3.     public Car getCar();  
  4. }  
 

 

   类 Car:

 

[java]  view plain copy
  1. import java.io.Serializable;  
  2. public class Car implements Serializable{  
  3.     /** TODO */  
  4.     private static final long serialVersionUID = 1L;  
  5.     private String name;  
  6.     private String length;  
  7.     private String color;  
  8.     public String getName() {  
  9.         return name;  
  10.     }  
  11.     public void setName(String name) {  
  12.         this.name = name;  
  13.     }  
  14.     public String getLength() {  
  15.         return length;  
  16.     }  
  17.     public void setLength(String length) {  
  18.         this.length = length;  
  19.     }  
  20.     public String getColor() {  
  21.         return color;  
  22.     }  
  23.     public void setColor(String color) {  
  24.         this.color = color;  
  25.     }  
  26.       
  27. }  
 

    类 BasicService :
 
[java]  view plain copy
  1. public class BasicService implements IBasic {  
  2.     public Car getCar() {  
  3.         Car c = new Car();  
  4.         c.setColor("红色");  
  5.         c.setLength("2m");  
  6.         c.setName("火车");  
  7.         return c;  
  8.     }  
  9.     private String hello = "Hello World";  
  10.     public String hello() {       
  11.         return hello;  
  12.     }  
  13. }  
 

  类 BasicClient :

[java]  view plain copy
  1. import java.net.MalformedURLException;  
  2. import com.caucho.hessian.client.HessianProxyFactory;  
  3. public class BasicClient {  
  4.     public static void main(String[] args){  
  5.         String url = "http://127.0.0.1:8081//WebTest/hello";     
  6.         HessianProxyFactory factory = new HessianProxyFactory();     
  7.         IBasic basic;  
  8.         try {  
  9.           basic = (IBasic) factory.create(IBasic.class, url);  
  10.           Car car   = basic.getCar();     
  11.           System.out.println("Hello: " + basic.hello());     
  12.           System.out.println("color: " + car.getColor());  
  13.           System.out.println("length: " + car.getLength());  
  14.           System.out.println("name: " + car.getName());  
  15.         } catch (MalformedURLException e) {  
  16.             e.printStackTrace();  
  17.         }    
  18.     }  
  19. }  
 

  web.xml文件改成如下:

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.   <display-name>WebTest</display-name>  
  4.   <welcome-file-list>  
  5.     <welcome-file>index.html</welcome-file>  
  6.     <welcome-file>index.htm</welcome-file>  
  7.     <welcome-file>index.jsp</welcome-file>  
  8.     <welcome-file>default.html</welcome-file>  
  9.     <welcome-file>default.htm</welcome-file>  
  10.     <welcome-file>default.jsp</welcome-file>  
  11.   </welcome-file-list>  
  12.   <servlet>    
  13.     <servlet-name>hello</servlet-name>    
  14.     <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>    
  15.     <init-param>    
  16.         <param-name>home-class</param-name>    
  17.         <param-value>BasicService</param-value>    
  18.     </init-param>    
  19.     <init-param>    
  20.         <param-name>home-api</param-name>    
  21.         <param-value>IBasic</param-value>    
  22.     </init-param>    
  23.    </servlet>    
  24.     
  25. <servlet-mapping>    
  26.     <servlet-name>hello</servlet-name>    
  27.     <url-pattern>/hello</url-pattern>    
  28. </servlet-mapping>   
  29. </web-app>  
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值