You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Benjamin Swanson <ba...@yahoo.com> on 2003/10/06 23:43:00 UTC

Instantiate a bean on startup

Hello all. Is there an easy way to instantiate a bean
as soon as tomcat starts? Basically, I want a bean to
be instantiated and available to all sessions as soon
tomcat starts. Any ideas?

ben

__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


RE: Instantiate a bean on startup

Posted by Peter Guyatt <pg...@telesoft-technologies.com>.
Hi There,

	If you only want to have one instance of a class for many objects then use
the following

class myClass {

	private static myClass c = null;

	public myClass () {
		//set stuff
	}

	public static myClass Instance () {
		if (c==null) {
			c = new myClass();
		}
		return c;
	}

}

now the data member c holds a reference to itself and is returned every time
you call the Instance method, it is only instantiated once.

If you put this in a session listeners constructor it will be instantiated
when the listener is.

Now every time you call the static method Instance from any servlet or JSP
page you will be working with that version of the class only.

Hope this helps

Thanks

Pete


-----Original Message-----
From: Nikola Milutinovic [mailto:Nikola.Milutinovic@ev.co.yu]
Sent: 07 October 2003 06:22
To: Tomcat Users List
Subject: Re: Instantiate a bean on startup


> Hello all. Is there an easy way to instantiate a bean
> as soon as tomcat starts? Basically, I want a bean to
> be instantiated and available to all sessions as soon
> tomcat starts. Any ideas?

There is no such thing as "Tomcat scope" for variables, so you're hosed. Not
just with Tomcat...

Why would you want to do something like that? There is no way to specify
what is to be done on Tomcat's startup (there is for a particular
application). And even if you hack it somehow, how will any Servlet access
that object?

Nix.


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: Instantiate a bean on startup

Posted by Nikola Milutinovic <Ni...@ev.co.yu>.
> Hello all. Is there an easy way to instantiate a bean
> as soon as tomcat starts? Basically, I want a bean to
> be instantiated and available to all sessions as soon
> tomcat starts. Any ideas?

There is no such thing as "Tomcat scope" for variables, so you're hosed. Not just with Tomcat...

Why would you want to do something like that? There is no way to specify what is to be done on Tomcat's startup (there is for a particular application). And even if you hack it somehow, how will any Servlet access that object?

Nix.

RE: Instantiate a bean on startup

Posted by "Richard Mixon (qwest)" <rn...@custco.biz>.
You did not say which Tomcat version you are using, but if you are using 4.0 or higher you should be able to use a listener that can
do whatever you want at startup.

Check the web.xml docs or the servlet spec.

- Richard

-----Original Message-----
From: Benjamin Swanson [mailto:baswanson@yahoo.com]
Sent: Monday, October 06, 2003 2:43 PM
To: tomcat-user@jakarta.apache.org
Subject: Instantiate a bean on startup


Hello all. Is there an easy way to instantiate a bean
as soon as tomcat starts? Basically, I want a bean to
be instantiated and available to all sessions as soon
tomcat starts. Any ideas?

ben

__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org