1、JAVA服务器端必须具备以下几点:
(1)包含Hessian的jar包
(2)设计一个接口,用来给客户端调用
(3)实现该接口的功能
(4)配置web.xml,配好相应的servlet
(5)对象必须实现Serializable 接口 (6)对于复杂对象可以使用Map的方法传递 2、客户端必须具备以下几点: (1)java客户端包含Hessian.jar的包。C#中引用hessianCSharp.dll (2)具有和服务器端结构一样的接口。包括命名空间都最好一样(3)利用HessianProxyFactory调用远程接口。
服务器端
新建一个名EasyHession的webProject项目,将Hessian.jar放入web-inf下的lib中
创建接口:
public interface BasicAPI {
public void setGreeting(String greeting);
public String hello();
public User getUser();
}
实现接口:
public class BasicService implements BasicAPI {
private String _greeting = "Hello, world";
public void setGreeting(String greeting) {
_greeting = greeting;
System.out.println("set greeting success:" + _greeting);
}
public String hello() {
return _greeting;
}
public User getUser() {
return new User("prance", "meshow");
}
}
创建一个实现Serializable的projo类也可以是Bean。
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = -4224277848079078755L;
String userName ="snoopy";
String password ="showme";
public User(String user, String pwd) {
this.userName =user;
this.password= pwd;
}
public String getUserName() {
return userName;
}
public String getPassword() {
return password;
}
}
接下来是配置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">
<!-- hessianServlet -->
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>service-class</param-name>
<param-value>com.silver.hessian.BasicService</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
为了测试可以在编辑一个简单的test.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%@ page import="com.caucho.hessian.client.HessianProxyFactory,
com.silver.hessian.BasicAPI" %>
<%
String contextPath = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + contextPath + "/";
HessianProxyFactory factory = new HessianProxyFactory();
String url = basePath + "hello";
out.println(url + "<br/>");
BasicAPI basic = (BasicAPI) factory.create(BasicAPI.class, url);
out.println("Hello: " + basic.hello() + "<br/>");
out.println("Hello: " + basic.getUser().getUserName() + "<br/>");
out.println("Hello: " + basic.getUser().getPassword() + "<br/>");
%>
将一切配置成功后,将项目发布到web 服务器上去,可以是Tomcat,Rain等等。
进入
结果如下:
Hello: Hello, world
Hello: snoopy
Hello: showme
说明服务器端已经编写成功。
java客户端
创建一个名为HessianClient的JAVAProject,载入hessian.jar包
创建与服务器端一样的接口,及基础类
public interface BasicAPI {
public void setGreeting(String greeting);
public String hello();
public User getUser();
}
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = -4224277848079078755L;
String userName ="snoopy";
String password ="showme";
public User(String user, String pwd) {
this.userName =user;
this.password= pwd;
}
public String getUserName() {
return userName;
}
public String getPassword() {
return password;
}
}
创建客户端程序
public class BasicClient {
public static void main(String[] args) throws Exception {
String url ="http://127.0.0.1:8080/archivelib/hello";
HessianProxyFactory factory = new HessianProxyFactory();
BasicAPI basic = (BasicAPI) factory.create(BasicAPI.class, url);
System.out.println("Hello:" + basic.hello());
System.out.println("Hello:" + basic.getUser().getUserName());
System.out.println("Hello:" + basic.getUser().getPassword());
basic.setGreeting("Hello Greeting");
System.out.println("Hello:" + basic.hello());
}
}