可能你不知道这个单词:Hessian,我查过,是:hessian n. 粗麻布,我晕倒,怎么这么多的Java爱好者,把这些名字都取得稀奇古怪。
言规正传,我们莱说这块:粗麻布(它的英文发音也很奇怪),这个140K的东西,可以替代Webservice,或者说它就是一个webService的构建者,可以实现非SOAP协议的远程事务调用,对象传递和大文件传递。
先看看怎么部署它,我用的Java Container是他的哥哥Resin(也是一个奇怪的名字),先把这个140kb的Jar拷贝到你的webapp目录的WEB-INF下的lib目录里面。,接下来我们做一个tutorship:
先建立一个接口,相当于EJB里面的stub,如下:
package app.demo;
public interface BasicAPI {
public String hello();
public User getUser();
}
再建立一个实现类:
package app.demo;
public class BasicService implements BasicAPI{
private String _greeting = "Hello, world";
public void setGreeting(String greeting)
{
_greeting = greeting;
}
public String hello()
public String hello()
public String hello()
public String hello()
public String hello()
public String hello()
{
return _greeting;
}
public User getUser(){
public User getUser(){
public User getUser(){
public User getUser(){
public User getUser(){
public User getUser(){
return new User("prance","meshow");
}
}
为了查看它是否可以传递对象,我们再建立一个可以序列化的User对象
package app.demo;
public class User implements java.io.Serializable {
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;
}
}
好了接下部署在你的webapp下面,申明一下,这个麻布可以在它哥哥的早期版本下面使用,我在resin2.1.10下面测试是可以的,废话不说,直接配置web.xml如下
<servlet servlet-name="hello"
servlet-class="com.caucho.hessian.server.HessianServlet">
<init-param service-class="app.demo.BasicService"/>
<init-param api-class="app.demo.BasicAPI"/>
</servlet>
<servlet-mapping url-pattern="/hello" servlet-name="hello"/>
人们的好奇心就是没有救,我访问了一下这个servlet,大家可能很奇怪,我没有写servlet呀,当然,在hessian-3.0.13.jar里面有,你如果看到一行字:
Hessian Requires POST
没错,就是它。
先试一下用客户程序来访问它:
代码如下:
package app.demo;
import com.caucho.hessian.client.HessianProxyFactory;
public class BasicClient {
public static void main(String []args)
throws Exception
{
String url = "http://127.0.0.1:81/demo/hello";
HessianProxyFactory factory = new HessianProxyFactory();
HessianProxyFactory factory = new HessianProxyFactory();
HessianProxyFactory factory = new HessianProxyFactory();
HessianProxyFactory factory = new HessianProxyFactory();
HessianProxyFactory factory = new HessianProxyFactory();
HessianProxyFactory factory = new HessianProxyFactory();
BasicAPI basic = (BasicAPI) factory.create(BasicAPI.class, url);
System.out.println("Hello: " + basic.hello());
System.out.println("Hello: " + basic.hello());
System.out.println("Hello: " + basic.hello());
System.out.println("Hello: " + basic.hello());
System.out.println("Hello: " + basic.hello());
System.out.println("Hello: " + basic.hello());
System.out.println("Hello: " + basic.getUser() .getUserName() );
System.out.println("Hello: " + basic.getUser().getPassword() );
}
}
}
}
}
}
}
直接执行,结果是这样的:
Hello: Hello, world
Hello: prance
Hello: meshow
这个结果说明,从一个client application是可以调用hessian的,而且可以传递对象。
最后用一个jsp来检验一下:
<%@ page import="com.caucho.hessian.client.HessianProxyFactory,
app.demo.BasicAPI"%>
<%@page language="java" %>
<%
String url = ("http://" +request.getServerName() + ":" + request.getServerPort() +
request.getContextPath() + "/hello");
out.println(url);
HessianProxyFactory factory = new HessianProxyFactory();
BasicAPI basic = (BasicAPI) factory.create(BasicAPI.class, url);
out.println("Hello: " + basic.hello());
out.println("Hello: " + basic.getUser() .getUserName() );
out.println("Hello: " + basic.getUser().getPassword() );
%>
好了,全部测试完毕,简简单单,快快乐乐。