Steven Elliott
2002-03-29 18:37:08 UTC
I have tried just about every way I know or can invent but cannot seem to
put my head around the problem of implementing a single instance of my
servlet and using <load-on-startup>.
If I put my Singleton servlet in my application directory and in the WEB.XML
file direct it to <load-on-startup> it gets instantiated and initialized
four (4) times.
For instance the following will print out "Singleton started" four times if
<load-on-startup> is in the apps WEB.XML
---------------------------< code start >---------------------------
public class Singleton extends HttpServlet {
public void init()
throws ServletException
{
System.out.println("Singleton started");
}
}
For some reason I was under the impression that initialization and
instantiation of a servlet would only be done once and by only one
classloader. Apparently that is not correct and worse it appears that the
class is getting instantiated by different classloaders.
So because I cannot use a constructor with a servlet I am a little at a loss
of how to apply the Singleton pattern?
I have looked through the archives and over the 2.3 spec w/o much light.
If anyone has any ideas on how to get a single object instance during
startup I would really appreciate it. Lazy initialization after startup
works fine but does not meet the requirements.
Is there a listener for application startup that I can use to que from?
Thanks,
Steven
--
To unsubscribe: <mailto:tomcat-user-***@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-***@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-***@jakarta.apache.org>
put my head around the problem of implementing a single instance of my
servlet and using <load-on-startup>.
If I put my Singleton servlet in my application directory and in the WEB.XML
file direct it to <load-on-startup> it gets instantiated and initialized
four (4) times.
For instance the following will print out "Singleton started" four times if
<load-on-startup> is in the apps WEB.XML
---------------------------< code start >---------------------------
public class Singleton extends HttpServlet {
public void init()
throws ServletException
{
System.out.println("Singleton started");
}
}
For some reason I was under the impression that initialization and
instantiation of a servlet would only be done once and by only one
classloader. Apparently that is not correct and worse it appears that the
class is getting instantiated by different classloaders.
So because I cannot use a constructor with a servlet I am a little at a loss
of how to apply the Singleton pattern?
I have looked through the archives and over the 2.3 spec w/o much light.
If anyone has any ideas on how to get a single object instance during
startup I would really appreciate it. Lazy initialization after startup
works fine but does not meet the requirements.
Is there a listener for application startup that I can use to que from?
Thanks,
Steven
--
To unsubscribe: <mailto:tomcat-user-***@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-***@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-***@jakarta.apache.org>