Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Equivalent of servlet init() in JSF

 
Sahil Saxena
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Varun Khanna
Ranch Hand
Posts: 1400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Sahil Saxena
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Varun for replying,

I will do it by using the load-on-startup parameter.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Varun Khanna
Ranch Hand
Posts: 1400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic