zoukankan      html  css  js  c++  java
  • Hessian知识学习总结(二)——Hessian的helloworld

    一、下载Hessian

            可在hessian官网http://hessian.caucho.com/ 或者http://download.csdn.net/detail/wodediqizhang/9543682下载jar包。
    此处用的是hessian-4.0.3.jar

    二、 搭建Hessian的Server

            2.1.新建一个web项目,HessianTest,如图1。将hessian-4.0.3.jar放在lib下。

            2.2.提供服务接口HessianService,代码如下:

    [java] view plain copy
     
    1. package com.cn.wjztr.service;  
    2.   
    3. import com.cn.wjztr.model.HelloWorld;  
    4. /** 
    5.  *  
    6.  *  
    7.  * 类名称:HessianService 
    8.  * 类描述: 定义对外提供服务的接口 
    9.  * 创建人:wodediqizhang@163.com 
    10.  * 修改时间:2016-6-7 下午4:39:33 
    11.  * Modification History: 
    12.  * ============================================================================= 
    13.  *   Author         Date          Description 
    14.  *   ------------ ---------- --------------------------------------------------- 
    15.  *   ghb<span style="font-family: Arial, Helvetica, sans-serif;">                         2016-6-7        创建文档 </span> 
    16.  * ============================================================================= 
    17.  * @version 1.0.0 
    18.  * 
    19.  */  
    20. public interface HessianService {  
    21.   
    22.     public HelloWorld sayHelloWorld();  
    23. }  


            2.3.HessianService接口要使用HelloWorld对象,HelloWorld代码如下:

    [java] view plain copy
     
    1. package com.cn.wjztr.model;  
    2.   
    3. import java.io.Serializable;  
    4.   
    5. public class HelloWorld implements Serializable{  
    6.   
    7.     /** 
    8.      * serialVersionUID:TODO(用一句话描述这个变量表示什么) 
    9.      * 
    10.      * @since 1.0.0 
    11.      */  
    12.     private static final long serialVersionUID = 2303638650074878501L;  
    13.     /** 
    14.      * 名字 
    15.      */  
    16.     private String name;  
    17.     public HelloWorld() {  
    18.           
    19.     }  
    20.     public HelloWorld(String name) {  
    21.          this.name = name;  
    22.     }  
    23.       
    24.     public String getName() {  
    25.         return name;  
    26.     }  
    27.     public void setName(String name) {  
    28.         this.name = name;  
    29.     }  
    30.   
    31. }  


            2.4.实现HessianService接口,实现类为HessianServiceImpl:

    [java] view plain copy
     
    1. package com.cn.wjztr.service.impl;  
    2.   
    3. import com.cn.wjztr.model.HelloWorld;  
    4. import com.cn.wjztr.service.HessianService;  
    5. /** 
    6.  *  
    7.  *  
    8.  * 类名称:HessianServiceImpl 
    9.  * 类描述:对外提供服务的接口的实现 
    10.  * 创建人:<span style="font-family: Arial, Helvetica, sans-serif;">wodediqizhang@163.com</span> 
    11.  * 修改时间:2016-6-7 下午4:47:40 
    12.  * Modification History: 
    13.  * ============================================================================= 
    14.  *   Author         Date          Description 
    15.  *   ------------ ---------- --------------------------------------------------- 
    16.  *   ghb           2016-6-7        创建文档  
    17.  * ============================================================================= 
    18.  * @version 1.0.0 
    19.  * 
    20.  */  
    21. public class HessianServiceImpl implements HessianService {  
    22.       
    23.     @Override  
    24.     public HelloWorld sayHelloWorld() {  
    25.         // TODO Auto-generated method stub  
    26.         return new HelloWorld("hello,World");  
    27.     }  
    28.   
    29. }  


            2.5.配置web.xml,添加对HessianServlet的配置:

    [java] view plain copy
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app version="3.0"   
    3.     xmlns="http://java.sun.com/xml/ns/javaee"   
    4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    6.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
    7.   <display-name></display-name>   
    8.   <welcome-file-list>  
    9.     <welcome-file>index.jsp</welcome-file>  
    10.   </welcome-file-list>  
    11.     
    12.    <servlet>  
    13.         <!-- 配置 HessianServlet,Servlet的命名任意-->  
    14.         <servlet-name>ServiceServlet</servlet-name>  
    15.         <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>  
    16.           
    17.         <!-- 配置接口的具体实现类 ,param-name命名任意-->  
    18.         <init-param>  
    19.             <param-name>service-class</param-name>  
    20.             <param-value>com.cn.wjztr.service.impl.HessianServiceImpl</param-value>  
    21.         </init-param>  
    22.     </servlet>  
    23.     <!-- 映射 HessianServlet的访问URL地址-->  
    24.     <servlet-mapping>  
    25.         <servlet-name>ServiceServlet</servlet-name>  
    26.         <url-pattern>/ServiceServlet</url-pattern>  
    27.     </servlet-mapping>  
    28.     
    29. </web-app>  


            此时,Hessian Server的配置已经完成了,接下来将应用部署在tomcat并启动。访问链接http://127.0.0.1:8081/HessianTest/ServiceServlet,得到信息如下:

      

    三、实现Hessian的client

            调用Hessian的客户端,创建HessianTestClient类,代码如下:

    [java] view plain copy
     
    1. package com.cn.wjztr.controller;  
    2.   
    3. import java.net.MalformedURLException;  
    4.   
    5. import com.caucho.hessian.client.HessianProxyFactory;  
    6. import com.cn.wjztr.model.HelloWorld;  
    7. import com.cn.wjztr.service.HessianService;  
    8.   
    9.   
    10. /** 
    11.  *  
    12.  *  
    13.  * 类名称:HessianTestClient 
    14.  * 类描述: 
    15.  * 创建人:wodediqizhang@163.com 
    16.  * 修改时间:2016-6-7 下午4:57:11 
    17.  * Modification History: 
    18.  * ============================================================================= 
    19.  *   Author         Date          Description 
    20.  *   ------------ ---------- --------------------------------------------------- 
    21.  *   ghb            2016-6-20        创建文档  
    22.  * ============================================================================= 
    23.  * @version 1.0.0 
    24.  * 
    25.  */  
    26. public class HessianTestClient {  
    27.     public static void main(String[] args) {  
    28.     //在服务器端的web.xml文件中配置的HessianServlet映射的访问URL地址  
    29.     String url = "http://127.0.0.1:8081/HessianTest/ServiceServlet";   
    30.     HessianProxyFactory factory = new HessianProxyFactory();   
    31.     HessianService service = null;  
    32.     try {service = (HessianService) factory.create(HessianService.class, url);}   
    33.     catch (MalformedURLException e) {  
    34.         // TODO Auto-generated catch block  
    35.         e.printStackTrace();}  
    36.     //创建IService接口的实例对象   
    37.     HelloWorld helloWorld = service.sayHelloWorld();  
    38.     //调用Hessian服务器端的ServiceImpl类中的getUser方法来获取一个User对象   
    39.     System.out.println(helloWorld.getName());}}  
    40.     }  
    41. }  



    [java] view plain copy
     
    1. <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">        运行这个类,会得到“hello,World”信息:</span>  

    后记:这篇博是借鉴了孤傲苍狼(http://www.cnblogs.com/xdp-gacl/p/3897534.html)这篇文章的内容实现的,不过他是将server和client分开,并将server的接口打了jar包给client,在client项目里面引用jar包里的接口实现对server的调用。这里我觉得理解原理就好,所以就我在这里就写一起了。

    再后记:上个后记都说了借鉴,那这篇文章还设为转载吧。就这样。

  • 相关阅读:
    hive sql基础了解
    创建自增字段,修改字段
    flysql 里两种传参的方式
    创建有赞商品资料
    cortable 使用方法
    学会如何使用,pycharm,和gitlanb
    进程和线程
    创建商品资料模板
    SAP 实例 5 CFW Events
    SAP 实例 4 CFW
  • 原文地址:https://www.cnblogs.com/huangwentian/p/7160309.html
Copyright © 2011-2022 走看看