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();
}
}
}