How can I initialize custom "application scope" objects for use in a Struts application?

Ted Husted

One way would be to subclass the ActionServlet, to to initialize whatever application scope objects you might need. Just remember to call the super.init() method!

A better approach would be to write another servlet, and auto-load both the Struts controller and your own servlet through the web.xml file. The Example application does this to load a "database" servlet, which is then accessed throughout the application. This way you can initialize application-specific things through your specialized servlet, and leave the normal Struts controller servlet alone. 

In a servlet 2.3 environment (such as Tomcat 4.0), you will also be able to use the new application event listener APIs to catch the contextInitialized() and contextDestroyed() events. This will become the preferred method, once servlet 2.3 containers are widely available.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact