• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to execute a function when struts loads?

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic