标签:lis nts creat UI index.jsp file sch nload not
一、下载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,代码如下:
- package com.cn.wjztr.service;
-
- import com.cn.wjztr.model.HelloWorld;
- 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{
-
-
- 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;
- public class HessianServiceImpl implements HessianService {
-
- @Override
- public HelloWorld sayHelloWorld() {
-
- 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:
- http:
- <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的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;
-
-
- public class HessianTestClient {
- public static void main(String[] args) {
-
- 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) {
-
- e.printStackTrace();}
-
- HelloWorld helloWorld = service.sayHelloWorld();
-
- System.out.println(helloWorld.getName());}}
- }
- }
- <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的调用。这里我觉得理解原理就好,所以就我在这里就写一起了。
再后记:上个后记都说了借鉴,那这篇文章还设为转载吧。就这样。
Hessian知识学习总结(二)——Hessian的helloworld
标签:lis nts creat UI index.jsp file sch nload not
原文地址:http://www.cnblogs.com/huangwentian/p/7160309.html