简体   繁体   中英

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM