aspose file tools*
The moose likes JSP and the fly likes A small chicken and the egg situation! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "A small chicken and the egg situation!" Watch "A small chicken and the egg situation!" New topic
Author

A small chicken and the egg situation!

Simon Harvey
Ranch Hand

Joined: Jan 26, 2003
Posts: 79
Hi there,
I have a fairly simple problem (I hope !)
I want to make an object that is attached to the application state - so there can only be one of them. This class is going to handle the execution of sql queries for my whole web application.
The problem is, I am unsure of how to create and attach the object in the first place. I was thinking that I could use a jsp page to test to see if the object is already created and if not, make one, but I dont really like that soloution because it would put more logic in the jsp than I really want. So what I'm wondering is if there is a common stratagy for situations like this?
Is there a way to make some code execute as the web app starts up, where I could do initialisation like this? What does everyone else do?
Thanks
Simon
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I think you want to implement a ServletContextListener, and create your object in the contextInitialized() method. You can store your object as an attribute of the ServletContext.
You'll also need to declare the listener in your application's web.xml file.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Simon Harvey
Ranch Hand

Joined: Jan 26, 2003
Posts: 79
Thanks Ron,
Can you offer any more info on what this servletContextlistener is?
I'm looking for info on it now but its a bit confusing. I'll post back if I find anything.
Can anyone else help as well?
Thanks everyone
Simon
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61230
    
  66

I do this sort of thing frequently with a servlet that is loaded upon startup (as specified in the web.xml).
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61230
    
  66

BTW, your instinct that the JSP pages are the wrong place to do this sort of thing is right on. JSP pages are a great place to do display formatting, but a poor choice for control and function code.
Simon Harvey
Ranch Hand

Joined: Jan 26, 2003
Posts: 79
OK, I think I have found what i need. Shouldnt be to hard. If I think its useful I'l post an example here but actually its quite straightforward. Just use google to search for ServletContextListener and then ServletContextListener web.xml example and your sorted
Simon
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Bear: Is that different from a listener? How do you specify a servlet "to be loaded upon startup" ?
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
here's a very simple example of a ServletContextListener:

This stores a Date object in the servlet (application) context, giving the date and time that the application was started.
You declare it in web.xml like this:

And here's a simple JSP that tests it:
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
Hmm, I've always done as Bear suggested, in the init() of a servlet loaded at startup (you can define if a servlet should load when the app starts in the web.xml file also).
Rons way looks a bit "cleaner", is this the preferred way of doing this sort of thing?
Anway, thanks for the info.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61230
    
  66

How do you specify a servlet "to be loaded upon startup" ?

An example from one of my web.xml's:

hth,
bear
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Thanks. Are there good reasons to use a Listener instead of a load-on-startup Servlet, or vice versa?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61230
    
  66

More info: I do use a Listener to know when important servlets are unloaded in order to reclaim resources. But I usually use load-on-startup to get them up in the first place.
bear
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
What sort of resources do you need to reclaim when a servlet context is unloaded, and how do you reclaim them?
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
you can make that class singleton.


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61230
    
  66

What sort of resources do you need to reclaim

Closing and releasing DB connections from a connection pool for example.
bear
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Thanks. Now how do you actually do this? If my connection pool is registered in JNDI, it is a DataSource, and I don't see any close() or destroy() method in that interface.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61230
    
  66

Using a home-grown connection pool implementation that's perfectly geared for such usage.
bear
 
Don't get me started about those stupid light bulbs.
 
subject: A small chicken and the egg situation!