You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Apache Wiki <wi...@apache.org> on 2005/09/23 06:59:27 UTC

[Struts Wiki] Update of "StrutsInitialization" by FrankZammetti

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Struts Wiki" for change notification.

The following page has been changed by FrankZammetti:
http://wiki.apache.org/struts/StrutsInitialization

------------------------------------------------------------------------------
  Up to StrutsNewFaqs
  ----
- {{{ How to execute an initialization method before the first (web-)user interaction  }}}
+ {{{ How to execute an initialization method before the first (web-)user interaction...  }}}
  
  = Answers =
  
@@ -75, +75 @@

              1 
          </load-on-startup> 
      </servlet> }}}
+ ----
+ = #4 Use a ContextListener =
+ {{{In web.xml:
+ 
+ <listener>
+   <listener-class>my.app.MyContextListener</listener-class>
+ </listener>
+ 
+ Then, MyContextListener.java might be:
+ 
+ package my.app;
+ import javax.servlet.ServletContextEvent;
+ import javax.servlet.ServletContextListener;
+ import java.util.HashMap;
+ public class MyContextListener implements ServletContextListener {
+   private static HashMap myConfigData = new HashMap();
+   public void contextInitialized(ServletContextEvent sce) {
+     myConfigData.put("someKey", "someValue");
+   }
+   public void contextDestroyed(ServletContextEvent sce) { }
+   public static Object getValue(String key) {
+     return myConfigData.get(key);
+   }
+ }
+ 
+ Then, in your code you can call MyContextListener.getValue("someKey"); to retrieve the value.  Naturally, you probably want to do something more complex than this like reading from a config file and creating some "real" configuration holder object, but you get the point :)
+ 
+ Remember, you can do whatever you want here... it can be as simple or as complex as you like!
+ 
+ Note that there is an existing listener in the Java Web Parts project that will take care of all the details and allow you to just provide a configuration file.  It creates a config object for you on-the-fly (and it also has more advanced capabilities if you require more control over things).  Details at http://javawebparts.sourceforge.net in the Listener package.
+ 
+          }}}
  
  
  ----
  Up to StrutsNewFaqs
  
- 

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org