服务器端
工程目录结构
代码:
pom.xml
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.hessiantest.client
hessianServer
0.0.1-SNAPSHOT
war
hessianServer
http://maven.apache.org
UTF-8
junit
junit
3.8.1
test
com.caucho
hessian
4.0.38
web.xml:
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
hello
com.caucho.hessian.server.HessianServlet
service-class
com.hessiantest.client.hessianServer.HelloServiceImpl
hello
/hello
然后把服务器端的war包部署到Tomcat,如图
Client端:
Client.java
服务器端请求类
HelloService.java
远程接口的存根
package com.hessiantest.client.hessianClient;
public interface HelloService
{
public String sayHello();
}
package com.hessiantest.client.hessianClient;
import java.net.MalformedURLException;
import com.caucho.hessian.client.HessianProxy;
import com.caucho.hessian.client.HessianProxyFactory;
import com.caucho.hessian.io.Hessian2Constants;
/**
* Hello world!
*
*/
public class Client
{
public static void main(String[] args) throws MalformedURLException, ClassNotFoundException
{
HessianProxyFactory factory = new HessianProxyFactory();
String url = "http://localhost:8080/hessianServer-0.0.1-SNAPSHOT/hello";
HelloService i = (HelloService)factory.create(HelloService.class,url);
System.out.println(i.sayHello());
}
}
邮件->run ,如图