D:\DEV\PROJECTS\Harbor_Tests_Servlets\src\java\harbor\test\servlets\RemoteServletTest.java
/*
 * RemoteServletTest.java
 */

package harbor.test.servlets;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;
import harbor.interfaces.*;
import kewlstuff.harbor.client.Vessel;

/**
 *
 * @author Johnny Kewl
 * @version 1.0
 * Simple example of how a servlet can use harbor
 */
public class RemoteServletTest extends HttpServlet {
     private Vessel vessel = null;
    
    // This function runs once when started 
    public void init() throws ServletException{ 
     try{    
        String harborUrl = "http://localhost:8080/harbor/service"; //location of container harbor
        vessel = new Vessel(harborUrl); //get a vessel from the harbor 
      }catch(Exception e){}        
    }
    
    //You would probably never go as crazy as this with remote objects...
    //This is actually the whole serialization example, but if this works, anything will.
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
       String msg1 = "Not Set";
       String msg2 = "Not Set"; 
    try{    
        //Get a remote class and make instance... it runs on local machine... add a message to it
        Class c_serialObject = vessel.getRemoteClass("harbor.classContainer.serialObject");
        I_serialObject i_serialObject = (I_serialObject)vessel.newInst(c_serialObject);
        i_serialObject.addMessageToBox("My first message to class on local machine");
        
        //load class on remote machine
        I_serialContainer i_serialContainer = (I_serialContainer)vessel.loadRemoteClassInst(I_serialContainer.class,"harbor.classContainer.serialContainer");//load a class instance ON the harbor
        
        //put local serialObject into remote container... the local object has to stream down to container
        i_serialContainer.setBoxOfMessages(i_serialObject);
        
        //add another message to the classes running at the harbor
        i_serialContainer.addMessageToBox("My second message added to messagebox in remote container");       
        
        //get the new serialObject on remote container... the remote object has to stream up to us
        I_serialObject i_serialObjectNew = i_serialContainer.getBoxOfMessages(); 
        
        //display the messages... class running on local machine
        msg1 = "Msg1 " + i_serialObjectNew.getMessageFromBox(0);
        msg2 = "Msg2 " + i_serialObjectNew.getMessageFromBox(1); 
        
        vessel.releaseRemoteClassInst(i_serialContainer);
    }catch(Exception e){msg1 = e.getMessage();} 
        
        
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet RemoteServletTest</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>msg1 " + msg1 + "</h1>");
        out.println("<h1>msg2 " + msg2 + "</h1>");        
        out.println("</body>");
        out.println("</html>");

        out.close();
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}