Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ServletContextListener with Jetty

I want to test my ServletContextListener with embeded jetty. It calls contextDestroyed on server stop but doesnt call contextInitialized on server start. How can I fix it?

Server server = new Server(PORT);
ServletContextListener myListener = ...;
WebAppContext webapp = new WebAppContext();
webapp.addEventListener(myListener);
webapp.setServer(server);
server.setHandler(webapp);
server.start(); -> doesnt call myListener.contextInitialized()

server.stop();  -> call myListener.contextDestroyed()
like image 856
Stan Kurilin Avatar asked Mar 22 '13 16:03

Stan Kurilin


1 Answers

I had a similar problem with Jetty 9.3.10

Have you tried setting extended listener types to true? If it is set to false (default), ServletContextListener implementations will not be considered in org.eclipse.jetty.server.handler.ContextHandler.Context#checkListener

    webapp.getServletContext().setExtendedListenerTypes(true);
like image 173
RobertG Avatar answered Oct 02 '22 21:10

RobertG