Question

I'd like to configure one or more ServletContextListener's programmatically, that is, without configuring them via web.xml. I'm currently doing something similar by adding servlets and filters programmatically.

Is this possible? If so, could someone provide an example?

Was it helpful?

Solution

With Tomcat 7 you have two options to avoid web.xml. The first is to use the @WebListener annotation but I suspect that isn't quite what you want. The second it so use a ServletContainerInitializer (SCI). For an exmaple, see how Tomcat's WebSocket SCI does it. That SCI does a lot of things. The relevant line for you is servletContext.addListener(new WsContextListener());

Note that an SCI uses the services API to register itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top