views:

68

answers:

2

Is there a way to initialize the ServletContext for a webapp on a Resin server using a method? I need something like that runs once, when the server starts up.

A: 

Use the <load-on-startup/> in the <servlet> tag.

Zach
That's for `Servlet` initialization, not `ServletContext` initialization.
skaffman
But the Servlet object has access to the ServletContext at that point, right?
Zach
+1  A: 

You need to use a ServletContextListener.

http://www.java-tips.org/java-ee-tips/java-servlet/how-to-work-with-servletcontextlistener.html

This J2EE tip demonstrates use of ServletContextListener. This event class handles notifications about changes to the servlet context of the Web application that they are part of. This can be explained as if someone is present on the server and dynamically informing us about the events that are occuring on the server. There acquire need of Listners. Therefore, ServletContextListner is helpful in informing about context Initilazation and destruction.

skaffman