天天看點

Hessian知識學習總結(二)——Hessian的helloworld一、下載下傳Hessian二、 搭建Hessian的Server三、實作Hessian的client

一、下載下傳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下。

Hessian知識學習總結(二)——Hessian的helloworld一、下載下傳Hessian二、 搭建Hessian的Server三、實作Hessian的client

        2.2.提供服務接口HessianService,代碼如下:

package com.cn.wjztr.service;

import com.cn.wjztr.model.HelloWorld;
/**
 * 
 * 
 * 類名稱:HessianService
 * 類描述: 定義對外提供服務的接口
 * 建立人:[email protected]
 * 修改時間:2016-6-7 下午4:39:33
 * Modification History:
 * =============================================================================
 *   Author         Date          Description
 *   ------------ ---------- ---------------------------------------------------
 *   ghb<span style="font-family: Arial, Helvetica, sans-serif;">                         2016-6-7        建立文檔 </span>
 * =============================================================================
 * @version 1.0.0
 *
 */
public interface HessianService {

	public HelloWorld sayHelloWorld();
}
           

        2.3.HessianService接口要使用HelloWorld對象,HelloWorld代碼如下:

package com.cn.wjztr.model;

import java.io.Serializable;

public class HelloWorld implements Serializable{

	/**
	 * serialVersionUID:TODO(用一句話描述這個變量表示什麼)
	 *
	 * @since 1.0.0
	 */
	private static final long serialVersionUID = 2303638650074878501L;
	/**
	 * 名字
	 */
	private String name;
	public HelloWorld() {
		
	}
	public HelloWorld(String name) {
		 this.name = name;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

}
           

        2.4.實作HessianService接口,實作類為HessianServiceImpl:

package com.cn.wjztr.service.impl;

import com.cn.wjztr.model.HelloWorld;
import com.cn.wjztr.service.HessianService;
/**
 * 
 * 
 * 類名稱:HessianServiceImpl
 * 類描述:對外提供服務的接口的實作
 * 建立人:<span style="font-family: Arial, Helvetica, sans-serif;">[email protected]</span>
 * 修改時間:2016-6-7 下午4:47:40
 * Modification History:
 * =============================================================================
 *   Author         Date          Description
 *   ------------ ---------- ---------------------------------------------------
 *   ghb           2016-6-7        建立文檔 
 * =============================================================================
 * @version 1.0.0
 *
 */
public class HessianServiceImpl implements HessianService {
	
	@Override
	public HelloWorld sayHelloWorld() {
		// TODO Auto-generated method stub
		return new HelloWorld("hello,World");
	}

}
           

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

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
   <servlet>
        <!-- 配置 HessianServlet,Servlet的命名任意-->
        <servlet-name>ServiceServlet</servlet-name>
        <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
        
        <!-- 配置接口的具體實作類 ,param-name命名任意-->
        <init-param>
            <param-name>service-class</param-name>
            <param-value>com.cn.wjztr.service.impl.HessianServiceImpl</param-value>
        </init-param>
    </servlet>
    <!-- 映射 HessianServlet的通路URL位址-->
    <servlet-mapping>
        <servlet-name>ServiceServlet</servlet-name>
        <url-pattern>/ServiceServlet</url-pattern>
    </servlet-mapping>
  
</web-app>
           

        此時,Hessian Server的配置已經完成了,接下來将應用部署在tomcat并啟動。通路連結http://127.0.0.1:8081/HessianTest/ServiceServlet,得到資訊如下:

Hessian知識學習總結(二)——Hessian的helloworld一、下載下傳Hessian二、 搭建Hessian的Server三、實作Hessian的client

三、實作Hessian的client

        調用Hessian的用戶端,建立HessianTestClient類,代碼如下:

package com.cn.wjztr.controller;

import java.net.MalformedURLException;

import com.caucho.hessian.client.HessianProxyFactory;
import com.cn.wjztr.model.HelloWorld;
import com.cn.wjztr.service.HessianService;


/**
 * 
 * 
 * 類名稱:HessianTestClient
 * 類描述:
 * 建立人:[email protected]
 * 修改時間:2016-6-7 下午4:57:11
 * Modification History:
 * =============================================================================
 *   Author         Date          Description
 *   ------------ ---------- ---------------------------------------------------
 *   ghb            2016-6-20        建立文檔 
 * =============================================================================
 * @version 1.0.0
 *
 */
public class HessianTestClient {
	public static void main(String[] args) {
	//在伺服器端的web.xml檔案中配置的HessianServlet映射的通路URL位址
	String url = "http://127.0.0.1:8081/HessianTest/ServiceServlet"; 
	HessianProxyFactory factory = new HessianProxyFactory(); 
	HessianService service = null;
	try {service = (HessianService) factory.create(HessianService.class, url);} 
	catch (MalformedURLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();}
	//建立IService接口的執行個體對象 
	HelloWorld helloWorld = service.sayHelloWorld();
	//調用Hessian伺服器端的ServiceImpl類中的getUser方法來擷取一個User對象 
	System.out.println(helloWorld.getName());}}
	}
}
           
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">        運作這個類,會得到“hello,World”資訊:</span>
           
Hessian知識學習總結(二)——Hessian的helloworld一、下載下傳Hessian二、 搭建Hessian的Server三、實作Hessian的client

後記:這篇博是借鑒了孤傲蒼狼(http://www.cnblogs.com/xdp-gacl/p/3897534.html)這篇文章的内容實作的,不過他是将server和client分開,并将server的接口打了jar包給client,在client項目裡面引用jar包裡的接口實作對server的調用。這裡我覺得了解原理就好,是以就我在這裡就寫一起了。

再後記:上個後記都說了借鑒,那這篇文章還設為轉載吧。就這樣。