最近对分布式,很感兴趣。EJB那厮太复杂了,hessian 从j2ee without EJB上看到,不多,试试。下面就是我的代码,代码已经测试过了。
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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>welcomeService</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>home-class</param-name>
<param-value>
cn.anyf.skycliff.server.impl.WelcomeServiceImpl
</param-value>
</init-param>
<init-param>
<param-name>home-api</param-name>
<param-value>cn.anyf.skycliff.server.WelcomeService</param-value>
</init-param>
<!--
随应用启动而启动
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>welcomeService</servlet-name>
<url-pattern>/service/welcomeService.service</url-pattern>
</servlet-mapping>
</web-app>
服务器端程序:
package cn.anyf.skycliff.server;
public interface WelcomeService {
public String speak() ;
}
package cn.anyf.skycliff.server.impl;
import cn.anyf.skycliff.server.WelcomeService;
public class WelcomeServiceImpl implements WelcomeService {
public String speak() {
return "Hello ,欢迎到skycliff来,要给我留言哦,我的QQ:19045962";
}
}
客户端程序:
package cn.anyf.skycliff.client;
public interface WelcomeClient {
public String speak();
}
package cn.anyf.skycliff.client.impl;
import java.net.MalformedURLException;
import com.caucho.hessian.client.HessianProxyFactory;
import cn.anyf.skycliff.client.WelcomeClient;
public class WelcomeClientImpl {
public static void main(String[] args) throws MalformedURLException {
// TODO Auto-generated method stub
String url = "http://localhost:8080/springHessian2/service/welcomeService.service";
HessianProxyFactory factory = new HessianProxyFactory();
WelcomeClient welcome = (WelcomeClient) factory.create(WelcomeClient.class, url);
System.out.println("下面调用Hessian服务端: " + welcome.speak());
}
}
想交朋友的留Q