digitalsanctum
digitalsanctum

Reputation: 3299

How to configure a ServletContextListener programmatically in Tomcat 7?

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?

Upvotes: 5

Views: 3501

Answers (1)

Mark Thomas
Mark Thomas

Reputation: 16625

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.

Upvotes: 2

Related Questions