• Java 远程调用之Hessian简例


    1. [代码]1.服务接口(Hello.java)     

    package server;
     
    public interface Hello {
      String hello(String name);
    }
    2. [代码]2.接口实现(HelloImpl.java)     

    package server;
     
    public class HelloImpl implements Hello{
      public String hello(String name) {
          return "hello,"+name+"! 欢迎学习Hessian";
      }
    }
    3. [代码]服务端配置(web.xml)     

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4"
      xmlns="http://java.sun.com/xml/ns/j2ee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
      <servlet>http://www.huiyi8.com/hunsha/
        <servlet-name>hessianService</servlet-name>
        <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
        <init-param>婚纱摄影
          <param-name>service-class</param-name>
          <param-value>server.HelloImpl</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>hessianService</servlet-name>
        <url-pattern>/hessianService</url-pattern>
      </servlet-mapping>
    </web-app>
    4. [代码]4.客户端代码(RomoteTest.java)    
    package client;
     
    import java.net.MalformedURLException;
    import server.Hello;
    import com.caucho.hessian.client.HessianProxyFactory;
     
    public class RomoteTest {
      public static void main(String[] args) {
        //hessian服务的url 其中hessian-v1是项目名
        String url = "http://192.168.1.104:8081/hessian-v1/hessianService";
        //创建HessianProxyFactory实例
        HessianProxyFactory factory = new HessianProxyFactory();
        //获得Hessian服务的远程引用
        try {
          Hello hello = (Hello)factory.create(Hello.class,url);
          System.out.println(hello.hello("kingtoon"));
        } catch (MalformedURLException e) {
          e.printStackTrace();
        }
      }
    }

  • 相关阅读:
    dispatchEvent 的应用
    sql语句中日期时间格式化查询
    Dotfuscator Professional Edition 4.9.7500.9484 混淆工具破解版+使用教程
    多表链接 Left join
    多线程计时器
    C#数据库事务原理及实践
    用户sa 登录失败。原因: 该帐户的密码必须更改 sql2008
    Application.DoEvents()和多线程
    C#中DataSet和DataReader的区别
    ROW_NUMBER() OVER函数的基本用法
  • 原文地址:https://www.cnblogs.com/xkzy/p/3915060.html
Copyright © 2020-2023  润新知