Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A small chicken and the egg situation!

 
Simon Harvey
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Simon Harvey
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64692
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do this sort of thing frequently with a servlet that is loaded upon startup (as specified in the web.xml).
hth,
bear
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64692
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear: Is that different from a listener? How do you specify a servlet "to be loaded upon startup" ?
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64692
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64692
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can make that class singleton.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64692
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64692
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a home-grown connection pool implementation that's perfectly geared for such usage.
bear
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic