title: pass object to another JVM using serialization - same Java version and jars (both running our app)
body: <p>Updates:
  For now using a Map. Class that wants to send something to other instance sends the object, the routing string. </p>
  
  <p>Use an object stream, use Java serializable to write the object to servlet. </p>
  
  <p>Write String first and then the object.</p>
  
  <p>Receiving servlet wraps input stream around a ObjectInputStream. Reads string first and then the Object. Routing string decides were it goes. </p>
  
  <p>A more generic way might have been to send a class name and its declared method or a Spring bean name, but this was enough for us.</p>
  
  <hr>
  
  <p>Original question</p>
  
  <p>Know the basic way but want details of steps. Also know I can use Jaxb or RMI or EJB ... but would like to do this using pure serialization to a bytearray and then encode that send it from servlet 1 in jvm 1 to servlet 2 in jvm 2 (two app server instances in same LAN, same java versions and jars set up in both J2EE apps)</p>
  
  <p>Basic steps are (Approcah 1) :-</p>
  
  <ol>
  <li><p>serialize any Serializable object to a byte array and make a string. Exact code see below</p></li>
  <li><p>Base64 output of 1. Is it required to base 64 or can skip step 2?</p></li>
  <li><p>use java.util.URLEncode.encode to encode the string</p></li>
  <li><p>use apache http components or URL class to send from servlet 1 to 2 after naming params</p></li>
  <li><p>on Servlet 2 J2EE framework would have already URLDecoced it, now just do reverse steps and cast to object according to param name.
  Since both are our apps we would know the param name to type / class mapping. Basically looking for the fastest &amp; most convenient way of sending objects between JVMs.</p></li>
  </ol>
  
  <p>Example : 
  POJO class to send</p>
  
  <pre><code>package tst.ser;
  
  import java.io.Serializable;
  
  public class Bean1 implements Serializable {
      /**
       * make it 2 if add something without default handling
       */
      private static final long serialVersionUID = 1L;
      private String s;
  
      public String getS() {
          return s;
      }
  
      public void setS(String s) {
          this.s = s;
      }   
  
  }
  </code></pre>
  
  <p><strong>* Utility *</strong></p>
  
  <pre><code>package tst.ser;
  
  import java.io.ByteArrayInputStream;
  import java.io.ByteArrayOutputStream;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.net.URLEncoder;
  
  public class SerUtl {
  
      public static String serialize(Object o) {
          String s = null;
          ObjectOutputStream os = null;
          try {
              os = new ObjectOutputStream(new ByteArrayOutputStream());
              os.writeObject(o);
              s = BAse64.encode(os.toByeArray());
  
  
              //s = URLEncoder.encode(s, "UTF-8");//keep this for sending part
  
          } catch (Exception e) {
              // TODO: logger
              e.printStackTrace();
              return null;
          } finally {
              // close OS but is in RAM
              try {
                  os.close();// not required in RAM
              } catch (Exception e2) {// TODO: handle exception logger
              }
              os = null;
          }
          return s;
      }
  
      public static Object deserialize(String s) {
          Object o = null;
          ObjectInputStream is = null;
  
          try {
              // do base 64 decode if done in serialize
              is = new ObjectInputStream(new ByteArrayInputStream(
                      Base64.decode(s)));
              o = is.readObject();
          } catch (Exception e) {
              // TODO: logger
              e.printStackTrace();
              return null;
          } finally {
              // close OS but is in RAM
              try {
                  is.close();// not required in RAM
              } catch (Exception e2) {// TODO: handle exception logger
              }
              is = null;
          }
          return o;
      }
  
  }
  </code></pre>
  
  <p>**** sample sending servlet ***</p>
  
  <pre><code>    Bean1 b = new Bean1(); b.setS("asdd");
      String s = SerUtl.serialize(b);
              //do UrlEncode.encode here if sending lib does not.
      HttpParam p = new HttpParam ("bean1", s);
      //http components send obj
  </code></pre>
  
  <p>**** sample receiving servlet ***</p>
  
  <pre><code>    String s = request.getParameter("bean1");
      Bean1 b1 = (Beean1)SerUtl.deserialize(s);
  </code></pre>