title: Using JNDI to share servlet session objects and data in Tomcat
body: <p>Over the last few weeks i have been looking at solutions to share an object between two contexts/war files. There are a number of ways this can be done and one of them is JNDI. </p>
  
  <p>I am not very familiar with JNDI used in Tomcat so would like a few questions clarified:</p>
  
  <p>Basically i have an instance of an object that would provide the following services to more than one context/application</p>
  
  <ul>
  <li>Check that the user is logged on</li>
  <li>Check that the user's session is valid</li>
  <li>Logon user - Includes logging the logon details</li>
  <li>Logout user - Remove session</li>
  </ul>
  
  <p>Each application will call this object to validate the user before it processes any requests. What i dont understand is how the object would work if it is stored on JNDI. I have seen a few examples of how JNDI is used in Tomcat but 99% of the examples show how to configure a JDBC datasource. </p>
  
  <ul>
  <li>How exactly is the object initialised in JNDI. For example, the following configuration from the Tomcat docs shown the configuration for JDBC</li>
  </ul>
  
  <blockquote>
  <pre><code>&lt;Resource name="jdbc/db1"
     auth="Container"
     type="oracle.jdbc.pool.OracleDataSource"
     driverClassName="oracle.jdbc.driver.OracleDriver"
     factory="oracle.jdbc.pool.OracleDataSourceFactory"
     url="jdbc:oracle:thin:@oracle.microdeveloper.com:1521:db1"
     user="scott"
     password="tiger"
     maxActive="20"
     maxIdle="10"
     maxWait="-1"&gt;
  </code></pre>
  </blockquote>
  
  <p>How would i do a similar thing in my case and most importantly, how would i initialise the object before it is put on the JNDI tree. Once it is on the JNDI tree, how is it updated?</p>
  
  <p>I guess what i am looking for is a simple example of the use of JNDI in Tomcat but not for database connections but for service provider type objects. </p>
  
  <p>I have been reading this tutorial <a href="http://docs.oracle.com/javase/tutorial/jndi/ops/bind.html" rel="nofollow noreferrer">http://docs.oracle.com/javase/tutorial/jndi/ops/bind.html</a> but it is focused more on LDAP naming directories which doesnt really help. </p>
  
  <h2>Edit</h2>
  
  <p>Ok i did find an example in the tutorial i listed above that shows how to "bind" an object the the JNDI tree. </p>
  
  <pre><code>class SerObj {
      public static void main(String[] args) {
  
      // Set up environment for creating initial context
          Hashtable&lt;String, Object&gt; env = new Hashtable&lt;String, Object&gt;(11);
      env.put(Context.INITIAL_CONTEXT_FACTORY, 
          "com.sun.jndi.ldap.LdapCtxFactory");
      env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");
  
      try {
          // Create the initial context
          Context ctx = new InitialContext(env);
  
          // Create object to be bound
          Button b = new Button("Push me");
  
          // Perform bind
          ctx.bind("cn=Button", b);
  
          // Check that it is bound
          Button b2 = (Button)ctx.lookup("cn=Button");
          System.out.println(b2);
  
          // Close the context when we're done
          ctx.close();
      } catch (NamingException e) {
          System.out.println("Operation failed: " + e);
      }
      }
  }
  </code></pre>
  
  <ul>
  <li>How would i modify it to use the Tomcat environment instead of the LDAP environment as shown above?</li>
  <li>Does the bind store a copy of the object or a reference to the object? What i am not sure of is for example, if 3 webapps access the object, will they all be accesssing the same instance of the object or different instances? I would like there to be only one instance of the object i put in the JNDI tree. </li>
  </ul>
  
  <h2>Edit</h2>
  
  <p>I must be missing something very simple. Here is another example i found which shows how to configure a JNDI object in Tomcat - <a href="http://flylib.com/books/en/4.255.1.275/1/" rel="nofollow noreferrer">http://flylib.com/books/en/4.255.1.275/1/</a></p>
  
  <ul>
  <li>How is the <code>com.jspservletcookbook.StockPriceBean</code> initialised?</li>
  <li>If one webapp uses the com.jspservletcookbook.StockPriceBean object and changes its state, is this change reflected across all other webapps that access this object?</li>
  <li>And most importantly, how many instances of the <code>com.jspservletcookbook.StockPriceBean</code> will be created - I need it to be the same instance for all webapps that would access the object. </li>
  </ul>
  
  <h2>Edit</h2>
  
  <p>@Ben Brunk - At the moment the session data is stored in a Map which is managed by one of the applications(context). All i need is for the other applications to call this application to check if the session is valid. My problem is that for these applications to get the services of the application that manages the session data, i need to use JNDI for them to get a reference to the relevant object. </p>
  
  <p>The data will not be stored in JNDI. I just want to use JNDI as a means for the applications to find and use the Session Manager object. The problem though is that everything i read about JNDI leads me to believe that it is used to create new objects every time an object is searched or 'looked up' in the directory. </p>
  
  <p>Here is diagram i think shows exactly what i want to achieve:</p>
  
  <p><img src="https://i.stack.imgur.com/SaRhf.gif" alt="Obtaining a reference"></p>
  
  <h2>Edit</h2>
  
  <p>@EJP - Just to clarify, the functions i listed above are just examples of what the shared object might do. These were provided just as an example.  All the functions it would do are related to the user and the logged on session. For example things like account information(username, last login etc), password information (expiry date etc), user privileges (i.e. which areas of the application the user is allowed access to). </p>
  
  <p>Given that the question is really related to multi-context communication via JNDI (Or any other means) and not about the requirements of the object it doesn't really make sense to list everything that the object is supposed to do so i just listed examples of what it could do.</p>
  
  <p>Apologies if the way i put the question confused anyone.   </p>