Application level object
I am have apache tomcat 4.0 server and very new to java. Can any one give
me pointers how to create a object which can be accessed thru out the application.
Sort of an singleton object. I need to instanciate this object once the server
starts and destroy it on the shutting down the app.
Thanks in advance.
Re: Application level object
"hrishi" <email@example.com> wrote:
>I am have apache tomcat 4.0 server and very new to java. Can any one give
>me pointers how to create a object which can be accessed thru out the application.
>Sort of an singleton object. I need to instanciate this object once the
>starts and destroy it on the shutting down the app.
>Thanks in advance.
You have several options. You could create static methods on some class
and never have to instantiate an object at all. You could create a singleton
object, as you've suggested. The idiom to do this is to make the constructor
private and provide a getInstance() method that returns a reference to a
private member variable initiliazed to the one and only instance of the class
during class initialization. Try googling for "java singleton" and you'll
find examples and discussions. Finally, you could create a normal instance
of the object and store it in the ServletContext (application intrinsic variable
in JSP). How you choose to bootstrap that instance is up to you and your
environment, but you could simply check for its existence before use and
create it if it doesn't exist yet.
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center