This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Struts and the fly likes How to execute a function when struts loads? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to execute a function when struts loads?" Watch "How to execute a function when struts loads?" New topic
Author

How to execute a function when struts loads?

Bruno Dery
Ranch Hand

Joined: Oct 05, 2003
Posts: 37
I didnt developped in struts much, we had a contractor who built our presentation layer using struts, I myself coded the business logic. The thing is, I would like to know how to execute a certain function when struts loads that basicly initializes certain things (loggers, database, etc). I have it as a static initializer in one of my classes but it executes only when struts makes a call to one of our business logic parts. Instead I would like this to be executed right when struts (or even Tomcat maybe I dont know if its possible) starts. How can I do that (without having to call the contractor, who's on vacation ).
Thanks!
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
If you are using at least Struts 1.1, use a Struts Plugin.
In struts-config.xml

Next create a class that implements org.apache.struts.action.PlugIn, and place your initialization code in the init() method.
Bruno Dery
Ranch Hand

Joined: Oct 05, 2003
Posts: 37
Thanks Jason!
It works when I try with a bogus example, but I get a weird problem when trying to run the initialization routine I want : tomcat doesnt start (it seems like it gets stuck in some kind of infinite loop, as my cpu jumps to 100% and I never get the message that tomcat is listening to ...
I've done some trial and error and it seems like the part where I setup my loggers makes it do that (I'm using java.util.logging). If I comment out that part, tomcat starts fine. So anyways, maybe tomcat relies on it's own logging to believe it's started Up to this point I'm guessing tomcat uses the same logging facility and it doesnt like me screwing up with loggers before it's started (log messages dont appear when I init my logging before tomcat is started). Do you know any other way I can perform the same thing?
sandy gupta
Ranch Hand

Joined: Jan 30, 2001
Posts: 228
Simple solution
Make a class and put this in your web.xml
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>mypackage.MyClass</servlet-class>
<init-param>
<param-name>myds</param-name>
<param-value>jdbc/MyDS</param-value>
</init-param>
<load-on-startup>10</load-on-startup>
</servlet>
Note the load-on-startup tag which tells the server, the priority loading order of this servlet. In the init of this class, you can load your objects and store them in some context avl to all applications (appcontext).
I have this set up on both wl8 and tomcat4.1
Sahil


Adios
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to execute a function when struts loads?