今天写了个hessian练练手,下面将介绍一个完整的案例。
首先实现服务器端,服务器我使用的是myeclipse10.0,新建一个web工程,导入hessian包,我这里导入的是hessian-3.1.6,顺便说一下,之前导入了hessian-4.0.7一直报错com.caucho.hessian.io.HessianProtocalException这个错误,更换成hessian-3.1.6错误解决。 这里将jar包放入WEB-INF/lib文件下即可。
开始定义接口,定义接口用来暴露给客户端,让客户端进行调用。
package com.hession.service;
import com.hession.domain.User;
public interface BasicApi {
public void setUser(User user);
public String print();
public User getUser();
public boolean login(String username,String password);
}
我这里简单的写了几个方法。
然后需要在服务器端实现接口的功能。
package com.hession.service.impl;这里还用到了一个User类,附上:
import com.hession.domain.User;
import com.hession.service.BasicApi;
public class BasicApiServiceImpl implements BasicApi{
private String hello = "this is hession test";
private User user;
public User getUser() {
return user;
}
public boolean login(String username, String password) {
if(username.equals("tdw") && password.equals("123456")){
System.out.println("登录成功");
return true;
}
System.out.println("登录失败");
return false;
}
public void setUser(User user) {
this.user = user;
}
public String print() {
return hello;
}
}
package com.hession.domain;
import java.io.Serializable;
public class User implements Serializable{
private static final long serialVersionUID = 1L;
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
接下来是最关键的地方,需要在web.xml中定义hessian,给出一个示例
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>hessian-service</servlet-name>
<servlet-class>
com.caucho.hessian.server.HessianServlet
</servlet-class>
<init-param>
<param-name>home-class</param-name>
<param-value>
<!-- 服务实现类 -->
com.hession.service.impl.BasicApiServiceImpl
</param-value>
</init-param>
<init-param>
<param-name>home-api</param-name>
<!-- 服务接口 -->
<param-value>com.hession.service.BasicApi</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>hessian-service</servlet-name>
<url-pattern>/hessian</url-pattern>
</servlet-mapping>
</web-app>
在这里你需要改动的就是服务实现类,跟服务接口的param-value的值。其他不动也行。
这样服务器端好了,然后将整个工程打成jar包(这里其实只需要将接口和实体类打成jar即可)。等下需要导入到客户端。
客户端需要导入之前上面导出的jar和hessian包(hessian包注意要跟服务器一致)。导入jar包,写一个测试类:
package com.hession.test;
import java.net.MalformedURLException;
import com.caucho.hessian.client.HessianProxyFactory;
import com.hession.domain.User;
import com.hession.service.BasicApi;
public class HessionServiceTest {
public static void main(String[] args) throws MalformedURLException {
String url = "http://192.168.1.114:8080/HessionWeb/hessian";
HessianProxyFactory factory = new HessianProxyFactory();
BasicApi basic = (BasicApi) factory.create(BasicApi.class, url);
basic.setUser(new User("tdw", "123456"));
System.out.println("用户名:"+basic.getUser().getUsername()+",密码:"+basic.getUser().getPassword());
System.out.println("----------------------------");
System.out.println(basic.print());
System.out.println("----------------------------");
if(basic.login("tdw", "123456"))
System.out.println("登录成功");
else
System.out.println("登录失败");
}
}
运行结果:
用户名:tdw,密码:123456
----------------------------
this is hession test
----------------------------
登录成功
服务器端代码和客户端代码下载链接:http://download.csdn.net/detail/tan313/9230169