Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 889
digitalsanctum Avatar asked Nov 02 '22 12:11

digitalsanctum


1 Answers

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.

like image 114
Mark Thomas Avatar answered Nov 15 '22 04:11

Mark Thomas