Why is contextInitialized () called multiple times?

I am running the Stripes web application on Jboss 4.2.3.GA and trying to call a method when starting JBoss. I created a ServletContextListener as follows:

public class TimerContextListener implements ServletContextListener { @Inject private TimerManager timerManager; public void contextInitialized(ServletContextEvent servletcontextevent) { ((Injector) servletcontextevent.getServletContext().getAttribute(GuiceServletContextListener.KEY)).injectMembers(this); timerManager.stopAllTimers(); timerManager.startTimer(); } public void contextDestroyed(ServletContextEvent servletcontextevent) { } } 

and I added an entry to web.xml as follows:

 <listener> <listener-class>com.lawless.web.servletContextListeners.TimerContextListener</listener-class> </listener> 

but contextInitialized () is called 3 times when I start my server. Any idea what could be the problem? Thanks.

+7
java servlets jboss stripes
source share
2 answers

Ok, I figured it out. It was called 3 times because I had 3 virtual hosts defined in jboss-web.xml. Not sure why this causes this behavior. If anyone can explain the reason, I would appreciate it.

+5
source share

There will be only one ServletContext for each web application. ServletContext will be created during application deployment (3 virtual hosts mean deployment to 3 different hosts with three different IP addresses). After creating the ServletContext it will be used by all servlets and JSP files in one application. ServletContext also called application scope variables in a web application script.

Source - http://www.javabeat.net/2009/02/servletcontextlistener-example/

+6
source share

All Articles