The discussion includes five main parts and a "Run the Servlet" link:
Interface
import java.rmi.*; public interface jbsRMIHelloIntCO extends java.rmi.Remote { jbsRMIHelloCO getCO() throws java.rmi.RemoteException; } // end jbsRMIHelloIntCO
Content Object
import java.io.*; import java.util.*; public class jbsRMIHelloCO extends Object implements Serializable { String msg = ""; private long timeMillis; private Date date; public jbsRMIHelloCO () { super (); msg = "Hello, World, from jbsRMIHelloCO"; timeMillis = System.currentTimeMillis (); date = new Date ( timeMillis ); } // end constructor public void setMsg ( String s ) { msg = s; } // end setMsg () public String getMsg () { return ( msg ); } // end getMsg () public Date getDate () { return ( date ); } // end getDate () } // end jbsRMIHelloCO
Implementation
import java.rmi.*; import java.rmi.server.UnicastRemoteObject; public class jbsRMIHelloImplCO extends UnicastRemoteObject implements jbsRMIHelloIntCO { public jbsRMIHelloImplCO () throws java.rmi.RemoteException { super (); } public jbsRMIHelloCO getCO () throws java.rmi.RemoteException { jbsRMIHelloCO obj = new jbsRMIHelloCO (); return ( obj ); } // end sayHello } // end jbsRMIHelloImplCO
Servlet
import java.net.*; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import java.rmi.*; public class jbsRMIHelloServletCO extends HttpServlet { //***** RMI Servlet //***** Expects rmiregistry on port: 4444 public boolean firsttime = true; String host; String rmiString; String codebaseString; String DEFAULT_HOST = "jbspc.cs.unc.edu"; String DEFAULT_SERVER_PORT = "8080"; String DEFAULT_PATH = "Courses/wwwp-s98/applets/jbsRMIHelloAppletCO"; String DEFAULT_APPLET = "jbsRMIHelloAppletCO.class"; String DEFAULT_RMI_PORT = "4444"; String DEFAULT_RMI_NAME = "jbsRMIHelloImplCO"; String DEFAULT_RMI_STRING = "rmi://wwwj.cs.unc.edu:4444/jbsRMIHelloImplCO"; String DEFAULT_CODEBASE = "http://jbspc.cs.unc.edu:8080/Courses/wwwp-s98/applets/jbsRMIHelloAppletCO"; jbsRMIHelloImplCO obj; public void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if ( firsttime ) { firsttime = false; try { buildPorts (); obj = new jbsRMIHelloImplCO (); Naming.rebind(rmiString, obj); System.out.println("jbsRMIHelloImplCO bound in rmiregistry: port 4444"); } catch (Exception e) { System.out.println("jbsRMIHelloImplCO error: " + e.getMessage()); e.printStackTrace(); } returnHTML ( resp ); } // end firsttime else { returnHTML ( resp ); } // end else } // end doGet public void returnHTML ( HttpServletResponse resp ) { ServletOutputStream out; try { resp.setContentType("text/html"); out = resp.getOutputStream(); out.println("<html>"); out.println("<head><title>jbsServerThreadedCO</title></head>"); out.println("<body>"); out.println("<center><font color=AA0000>"); out.println("<h3>jbsRMIHelloServletCO Running on " + host + ":" + DEFAULT_SERVER_PORT + "</h3>"); out.println("<h3>Applet Tag for jbsRMIHelloAppletCO Returned</h3>"); out.println("</font>"); out.println("<applet code=" + DEFAULT_APPLET + " codebase=" + codebaseString + " width=400 height=200>"); out.println("<param name=rmihost value=" + host +">"); out.println("<param name=rmiport value=" + DEFAULT_RMI_PORT +">"); out.println("</center>"); out.println("</applet>"); out.println("</body>"); out.println("</html>"); out.flush(); out.close(); } // end try catch ( IOException except) { } // end catch } // end returnHTML public void buildPorts () { try { InetAddress here = InetAddress.getLocalHost (); host = here.getHostName (); rmiString = "rmi://" + host + ":" + DEFAULT_RMI_PORT + "/" + DEFAULT_RMI_NAME; codebaseString = "http://" + host + ":" + DEFAULT_SERVER_PORT + "/" + DEFAULT_PATH; } // end try catch (Exception e) { System.out.println("Error build ports: " + e.getMessage()); e.printStackTrace(); host = DEFAULT_HOST; rmiString = DEFAULT_RMI_STRING; codebaseString = DEFAULT_CODEBASE; } // end catch } // end setHostPort } // end jbsRMIHelloServletCO
Applet
import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.*; import java.net.*; import java.rmi.*; import java.rmi.server.*; public class jbsRMIHelloAppletCO extends Applet implements ActionListener, WindowListener { String message; Date date; int hour, minute, second; long time; String rmiHost; String rmiPort; jbsRMIHelloIntCO intObj; Frame outerBox; TextField rmiDisplay; TextArea logDisplay, msgDisplay; Panel topPanel; Panel middlePanel; Panel buttonPanel; Button connectButton, updateButton, quitButton; public void init () { buildRMIHostPort (); buildUI (); } // end init //*********** Utility Methods *********** // ************** buildRMIHostPort private void buildRMIHostPort () { String hostString, portString; hostString = getParameter ( "rmihost" ); portString = getParameter ( "rmiport" ); if ( hostString == null ) { rmiHost = "wwwj.cs.unc.edu"; System.out.println("Null rmihost"); } else { rmiHost = hostString; System.out.println("Non-null rmihost: "+rmiHost); } // end else if ( portString == null ) { rmiPort = "4444"; System.out.println("Null rmiport"); } else { rmiPort = portString; System.out.println("Non-null rmiport: "+rmiPort); } // end else } // end buildRmiHostPort // ************** buildUI private void buildUI () { rmiDisplay = new TextField ( new String (rmiHost + ":" + rmiPort), 40 ); logDisplay = new TextArea ( "Log Display\n", 40, 10 ); msgDisplay = new TextArea ( "Message Display\n", 40, 10 ); middlePanel = new Panel (); middlePanel.setLayout ( new GridLayout ( 2, 1 ) ); middlePanel.add ( logDisplay ); middlePanel.add ( msgDisplay ); connectButton = new Button ( "Connect" ); updateButton = new Button ( "Update" ); quitButton = new Button ( "Quit" ); connectButton.addActionListener ( this ); updateButton.addActionListener ( this ); quitButton.addActionListener ( this ); buttonPanel = new Panel ( ); buttonPanel.add ( connectButton ); buttonPanel.add ( updateButton ); buttonPanel.add ( quitButton ); outerBox = new Frame ("Client_Applet"); outerBox.addWindowListener ( this ); outerBox.add ( "North", rmiDisplay ); outerBox.add ( "Center", middlePanel ); outerBox.add ( "South", buttonPanel ); outerBox.resize ( 400, 450 ); outerBox.show (); } // end buildUI //*********** Interface Methods *********** //**** ActionListener methods public void actionPerformed ( ActionEvent evt ) { Object s = evt.getSource(); // *** process Button actions if ( s instanceof Button ) { if ( s == connectButton ) { try { intObj = (jbsRMIHelloIntCO)Naming.lookup("rmi://" + rmiHost + ":" + rmiPort + "/jbsRMIHelloImplCO"); logDisplay.appendText ( "RMI lookup successful.\n" ); } // end try catch (NotBoundException e) { logDisplay.appendText ( "NotBoundException: rmi://" + rmiHost + ":" + rmiPort + "/jbsRMIHelloImplCO\n" ); System.out.println("jbsRMIHelloAppletCO NotBoundException: " + e.getMessage()); e.printStackTrace(); } // end catch catch (MalformedURLException e) { logDisplay.appendText ( "MalformedURLException.\n" ); System.out.println("jbsRMIHelloApplet MalformedURLException: " + e.getMessage()); e.printStackTrace(); } // end catch catch (java.rmi.UnknownHostException e) { logDisplay.appendText ( "UnknownHostException.\n" ); System.out.println("jbsRMIHelloApplet UnknownHostException: " + e.getMessage()); e.printStackTrace(); } // end catch catch (RemoteException e) { logDisplay.appendText ( "RemoteException.\n" ); System.out.println("jbsRMIHelloApplet RemoteException: " + e.getMessage()); e.printStackTrace(); } // end catch } // end connectButton if ( s == updateButton ) { try { jbsRMIHelloCO coObj = intObj.getCO(); message = coObj.getMsg (); date = coObj.getDate (); hour = date.getHours(); minute = date.getMinutes(); second = date.getSeconds(); time = date.getTime(); msgDisplay.appendText ( "Message returned from Server at: "+hour+" : "+minute+" : "+second+" : "+time+"\n" ); } // end try catch (RemoteException e) { logDisplay.appendText ( "RemoteException.\n" ); System.out.println("jbsRMIHelloApplet RemoteException: " + e.getMessage()); e.printStackTrace(); } // end catch } // end updateButton if ( s == quitButton ) { logDisplay.appendText ( "Quitting in 2 seconds.\n" ); try {Thread.sleep ( 2000 );} catch ( InterruptedException except) {;} outerBox.hide (); outerBox.dispose (); System.exit ( 0 ); } // end quitButton } // end process Button actions } // end actionPerformed //**** WindowListener methods public void windowActivated ( WindowEvent e ) { } public void windowDeactivated ( WindowEvent e ) { } public void windowOpened ( WindowEvent e ) { } public void windowClosed ( WindowEvent e ) { } public void windowClosing ( WindowEvent e ) { outerBox.hide (); outerBox.dispose (); System.exit(0); } public void windowIconified ( WindowEvent e ) { } public void windowDeiconified ( WindowEvent e ) { } } // end jbsRMIHelloAppletCO
Run the Servlet ( jbspc )Run the Servlet ( wwwj )