To achieve what you're trying to do, use the Singleton Pattern. I have used
it successfully. In the init method, have a class with a private constructor
and a public static synchronized method that creates an instance of the
class and creates and stores the objects in the context. But remember that
before creating the objects, first check to make sure that they do not exist
in the context. If they do, just return from the method. 

I have not encountered situations where init is called more than once.
Tomcat 4.x complies to both the Servlet 2.3 and JSP 1.2 specifications.
Those specifications stipulate that init should be called only once. Since
Tomcat is Sun's (Microsystems) reference implementation for the Servlet and
JSP specs, it duly conforms.

Hope this is helpful.

Charles 


-----Original Message-----
From: Julien OIX [mailto:[EMAIL PROTECTED]]
Sent: Friday, February 08, 2002 9:13 AM
To: Tomcat Users List
Subject: Re: Tomcat + Apache lifecycle question


Daniel Drasin a écrit :
> 
> I've set up Tomcat 4.x to work with Apache 1.3.x using the
> Warp connector stuff.  I followed the standard instructions for
> how to modify the httpd.conf file of apache, etc.  (All pretty
> straightforward...)  My problem is that i need to have a certain
> activity occur when Tomcat starts up.  This activity is to initialize
> a certain set of java objects and make them available to servlets.
> Because of classpath issues (i.e. classloaders) i was unable to use
> the lifecycle hooks of the contexts and had to settle for the
> (hackish) method of making a dummy servlet that only exists to
> have its <load-on-startup> value set (excerpt from the web.xml is
> below).  This servlet has an init() definition that initializes the
> objects in question (example definition below.)
> 
> The issue is that i need to have this activity occur once and only
> once - so i add a static flag into the init method to see if it's
> already run.  However, this doesn't work.  When i start tomcat,
> the initialization code runs and when i start apache, it runs
> again - despite the flag.  (Output included below).
> 
> My guess is that the flag doesn't help because these init() calls
> are occuring in two different object spaces (class loaders) so the
> flag really is false both times.
> 
> So my question is - what's going on here.  Fundamentally, how can
> i achieve my goal outlined in paragraph 1.  At a deeper level, my
> question is - why do i get two calls to startup?  Is tomcat starting
> a new context for apache?  If so, why doesn't it use the one that
> it's already started?  Is there a way to surpress the starting of
> one of the contexts?
> 
> Any help is appreciated
> 
> Dan
> -------------------------CODE AND OUTPUT---------------------------
> 
> ...
>     <servlet>
>       <servlet-name>
>           ServletTest
>       </servlet-name>
>       <servlet-class>
>           ServletTest
>       </servlet-class>
>       <load-on-startup>0</load-on-startup>
> ...
> 
> ...
> public class ServletTest extends HttpServlet {
>     static int i = 0;
>     static boolean flag = false;
>     static {
>         i++;
>         if(!flag) {
>          flag = true;
>          System.out.println("***********GOT " + i + " **************");
>       }
>     }
> ...
> 
> ...
> Starting service Tomcat-Standalon
> Apache Tomcat/4.0.1
> ***********GOT 1 **************

delete this service ( everything in the right <Service> section ) from
server.xml

hope this helps 


> Starting service Tomcat-Apache
> Apache Tomcat/4.0.1
> ***********GOT 1 **************
> ...
> 
> --
>         Daniel Drasin                           Applied Reasoning
>         [EMAIL PROTECTED]             8612 Long Meadow Ct.
>         (410)-461-6168                          Columbia, MD, 21045
>         http://www.appliedreasoning.com
> 
> --
> To unsubscribe:   <mailto:[EMAIL PROTECTED]>
> For additional commands: <mailto:[EMAIL PROTECTED]>
> Troubles with the list: <mailto:[EMAIL PROTECTED]>

-- 
Julien OIX
Service Informatique de Gestion
Tél: 02 40 99 83 65
mail: [EMAIL PROTECTED]

--
To unsubscribe:   <mailto:[EMAIL PROTECTED]>
For additional commands: <mailto:[EMAIL PROTECTED]>
Troubles with the list: <mailto:[EMAIL PROTECTED]>

--
To unsubscribe:   <mailto:[EMAIL PROTECTED]>
For additional commands: <mailto:[EMAIL PROTECTED]>
Troubles with the list: <mailto:[EMAIL PROTECTED]>

Reply via email to