Forums Register Login

Equivalent of servlet init() in JSF

+Pie Number of slices to send: Send
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?

Thanks.
+Pie Number of slices to send: Send
 


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?


If you have to implement this functionaly, "load on startup" option should be the best choice.
+Pie Number of slices to send: Send
Thanks Varun for replying,

I will do it by using the load-on-startup parameter.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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


Thanks.
+Pie Number of slices to send: Send
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.
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 7108 times.
Similar Threads
Why Init Method, why can't we use constructor instead?
JSF queris
NullPointerException in AutoScrollPhaseListener
why we need to use servlet context listener ?
Question about the init() method.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:53:20.