public interface RemoteService extends Remote { public String helloWorld() throws RemoteException; } public class RemoteServiceImpl extends UnicastRemoteObject implements RemoteService { public RemoteServiceImpl() throws RemoteException { super(); } public String helloWorld() throws RemoteException { return "Hello World!"; } public static void main(String[] args) { try { RemoteService remoteService = new RemoteServiceImpl(); Registry registry = LocateRegistry.createRegistry(1099); registry.rebind("RemoteService", remoteService); System.out.println("RemoteService bound"); } catch (RemoteException e) { e.printStackTrace(); } } } public class Client { public static void main(String[] args) { try { Registry registry = LocateRegistry.getRegistry("localhost", 1099); RemoteService remoteService = (RemoteService) registry.lookup("RemoteService"); System.out.println(remoteService.helloWorld()); e.printStackTrace(); } } } public class HelloWorldService { public String helloWorld() { return "Hello World!"; } public static void main(String[] args) throws Exception { HelloWorldService helloService = new HelloWorldService(); SerializerFactory serializerFactory = new SerializerFactory(); HessianServlet servlet = new HessianServlet(helloService, HelloWorldService.class, serializerFactory); HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0); HttpContext context = server.createContext("/"); context.setHandler(servlet); server.start(); System.out.println("Server started on port 8080"); } } public class Client { public static void main(String[] args) { String url = "http://localhost:8080/"; HessianProxyFactory factory = new HessianProxyFactory(); try { HelloWorldService helloService = (HelloWorldService) factory.create(HelloWorldService.class, url); System.out.println(helloService.helloWorld()); } catch (MalformedURLException e) { e.printStackTrace(); } } }


上一篇:
下一篇:
切换中文