You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Bygrave, Robin" <rb...@silverstream.com> on 2000/12/19 15:51:53 UTC

RE: creation of application wide objects - Singleton?

Maybe I missing something in your question, or could you also use a
Singleton?
This is available to not only servlets... but maybe what you are looking
for.


public class MySingletonObject {

	// private constructor
	private MySingletonObject () {
	}

	// static getInstance Method
	public static MySingletonObject getInstance() {
		return me;
	}

	// static var, only one instance of me
	private static MySingletonObject me = new MySingletonObject ();
}

Cheers, Rob.

-----Original Message-----
From: Guillaume Barreau [mailto:guillaume@runtime-collective.com]
Sent: 19 December 2000 12:13
To: tomcat-user@jakarta.apache.org
Subject: creation of application wide objects


Dear Tomcat users,

What is the best way to create some objects which are going to be available
application-wide to a bunch of servlets from the same web-app? Or in other
words, is there an equivalent of an init() method but for the application as
a
whole?

At the moment, I am having to clutter the init() method of all my servlets
with
code to check if the object has been created and create it if it hasn't. I
have
to do that because I am not sure which servlet will be invoked first.

Am I missing something? Is there any way to do that in the xml descriptor
file?

Thanks for your help,

Guillaume Barreau