aspose file tools*
The moose likes JSF and the fly likes Equivalent of servlet init() in JSF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Equivalent of servlet init() in JSF" Watch "Equivalent of servlet init() in JSF" New topic
Author

Equivalent of servlet init() in JSF

Sahil Saxena
Greenhorn

Joined: May 26, 2005
Posts: 22
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.


SCJP 1.4 & SCWCD
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400

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.


- Varun
Sahil Saxena
Greenhorn

Joined: May 26, 2005
Posts: 22
Thanks Varun for replying,

I will do it by using the load-on-startup parameter.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

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.
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
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.
Sahil Saxena
Greenhorn

Joined: May 26, 2005
Posts: 22
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.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Equivalent of servlet init() in JSF