tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "kc.baltz" <kc.ba...@inforonics.com>
Subject RE: Logging to File
Date Mon, 08 May 2000 18:50:38 GMT
> I would like to initialize some Application wide objects the 
> first time any
> Servlet or JSP page is loaded.  I would like to create one 
> instance of the
> logger, and a connection pool for interacting with an XML 
> datasource.  Is there
> a way to do this without placing code in every single Servlet 
> and JSP page to
> ensure that initialization has taken place?
> 

Yes, it's a software design pattern called the Singleton.  You create a
class like the following:

<pre>
class OneInstance {

	private OneInstance(/* Constructor parameters */) {
		// the only constructor is private
	}

	public static OneInstance getInstance(/* Constructor parameters */)
{
		// See if this class has been allocated yet.
		if( _instance == null ) {
			_instance = new OneInstance();
		}
		return _instance;
	}

	private static OneInstance _instance = null;
}
</pre>

Any servlet/JSP that needs a copy of this just calls getInstance instead of
a constructor.  This way you only get one instance per JVM.  Anyone know how
this works with 2 JVMs like when you have an RMI server?

K.C.

Mime
View raw message