Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to execute a function when struts loads?

Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • 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 ).
Posts: 6450
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Simple solution
Make a class and put this in your web.xml
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
You save more money with a clothesline than dozens of light bulb purchases. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic