This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi, In my previous web project I have written a servlet and in its init() method i perform the initialization(such as creating a database handler etc). Now i want to implement the above functionality in JSF also ie: when the container is started the database handler instance should already be created before the user(client) visit the site(any jsp/jsf page).
I am new to JSF so i would appreciate how to perform the above thing in JSF. Should I configure the above servlet's load on start up parameter in the web.xml or is there any alternative way to perform it in JSF?
Actually, since JSF is an IoC architecture, "Load-on-startup" isn't what I'd recommend. Instead, create a Managed Bean to manage the database handler and inject it into the beans that need it. This isn't literally load-on-startup (depends on what scope you use), but it is Just-in-Time.
You might want to google to see what people are doing with Spring+JSF. Spring allows you to offload a lot of data management to the Spring Framework.
Customer surveys are for companies who didn't pay proper attention to begin with.
Joined: May 30, 2002
Originally posted by Tim Holloway: Actually, since JSF is an IoC architecture, "Load-on-startup" isn't what I'd recommend. Instead, create a Managed Bean to manage the database handler and inject it into the beans that need it. This isn't literally load-on-startup (depends on what scope you use), but it is Just-in-Time.
If pre-initialization isn't a "has to be" thing for Sahil, I would too recommend this appraoch.
Joined: May 26, 2005
Thanks Tim and Varun,
Behind the inialization of the servlet my intention was that DatabaseHandler class instance should be created once only before it receives any client request and that databaseHandler class instance will be used by rest of the beans.
Well as I dont have much knowledge of Spring I would like to know only one thing, when will the managed bean will create the instance of the database handler class? 1) After the web container is started? or 2) After the web container receives the first request from the client?
I didnt looked forward to Spring because as already I am learnign JSF at this moment so didnt wanted to increase the learning curve for the Project, but now i think its time to start readin Spring too
You don't need Spring to do this in JSF. JSF's builtin Managed Bean facility is sufficient. If I used Spring it would be more for its ability to handle the database I/O for me.
When a bean gets instantiated depends on its scope. A bean with application scope should be instantiated at application startup. The next level down is session scope, which would get instantiated when a user "logs in" to the app. Request-scope beans might be instantiated before session scopes, but requests go out of scope, so they also get destroyed faster.
That's all according to the standard J2EE rules, of course. JSF just provides a way of automatically instantiating and injecting them.