Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSF - Calling method on startup  RSS feed

 
Daniel Cugler
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I have a JSF project running on tomcat.

I have some database operations that I would like to execute when the server starts up. I do no want to open any page to run such procedure.

My question. Is it possible to run some procedure without the need to open any page, i.e., just when the server (tomcat) starts?

Thank you.
 
K. Tsang
Bartender
Posts: 3630
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure there's a way, given you can specify what servlets/class to run on startup in the web.xml file. But I just wondering what would trigger your own class so that it starts. init()?
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is, and it works whether you are using JSF or not. Actually more than one.

In the olden days, one would designate a servlet as "load-on-startup", give it a start sequence number of 1 (start me first), and put the code in its init() method.

These days, the preferred location is in a servletcontextlistener startup method.

If there is much work involved, you may want to make the startup method spawn a thread so that startup of the rest of the webapp will not be delayed (just make sure that any code that requires full startup has a way to check when it's done!).

It is safe to spawn threads in startup methods, unlike servlet request-handler methods.
 
Daniel Cugler
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, thank you for your response.

I never created a servlet before, so I will google for "Servletcontextlistener startup method" and check how to do this.

Thank you so much.
 
Daniel Cugler
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again!

I've found this example. Simple and easy =)

http://www.mkyong.com/servlet/what-is-listener-servletcontextlistener-example/

Thank you guys!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!