ServletContextListener is not called

I am creating a Java EE 7 project using the Eclipse Maven plugin. My problem is that when I start the application, the class that implements SerlvetContextListener does not start. What causes this problem?

@WebListener public class ApplicationContextListener implements ServletContextListener{ @Override public void contextInitialized(ServletContextEvent sce) { Request request = new HttpRequest(sce); new Thread (request).start(); HibernateUtil.getSessionFactory(); } @Override public void contextDestroyed(ServletContextEvent sce) { } } 

web.xml:

 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <listener>com.kyrogaming.AppServletContextListener</listener> <!-- Jersey Mapping --> <servlet> <servlet-name>jersey-servlet</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.kyrogaming.webservices</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>jersey-servlet</servlet-name> <url-pattern>/service/*</url-pattern> </servlet-mapping> <!-- end Jersey Mapping --> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> 

+7
servlets servlet-listeners
source share
4 answers

In web.xml you also need to specify <listener-class> .

  <listener> <listener-class> com.kyrogaming.AppServletContextListener </listener-class> </listener> 
+3
source share

Using metadata-complete = "false" in web.xml fixed this problem for me.

 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" metadata-complete="false"> 
+11
source share

To summarize the answers of JNL and Ted Goddard:

For ServletContextListener (or other listeners like ServletContextAttributeListener or ServletRequestAttributeListener) that need to be loaded by the servlet container, you need to report this to the container. As described in the API docs , there are three ways to do this:

1) Declare it in the deployment descriptor (web.xml):

 <listener> <listener-class> com.kyrogaming.AppServletContextListener </listener-class> </listener> 

2) or annotate its @WebListener class (see "Annotation Note" below)

3) or register it programmatically using methods in ServletContext, such as addListener () .

Annotation note

Method 1) and 3) will always work. For method 2) (annotations) to work, the servlet container must be configured to scan classes in the classpath to find classes of annotated listeners.

Custom webapp classes (under WEB-INF/classes ) will always be checked, so annotated classes will always be found. However, libraries (JARs under WEB-INF/lib ) will not be checked if web.xml contains the metadata-complete="true" attribute (the default is false ). See Java Servlet Specification Version 3.0 , Chapter 8, “Annotations and Connectivity”.

So, so that the container can find annotated classes in the JAR, make sure that web.xml sets metadata-complete="false" or doesn't set it at all.

Please note that this may delay the launch of the application; see, for example, What to do with annotations after setting metadata-complete = "true" (which allowed Tomcat 7 to start slowly)? .


Unfortunately, this still does not explain why the ServletContextListener in the question is not loaded. Note that web.xml in the question does not matter metadata-complete , which means that it defaults to false , so class scanning is enabled. There may be other problems; this checklist will hopefully help you find it.

+10
source share

For the record, I add another possible (and rather vicious) reason for ServletContextListener not being called.

This can happen if you have java.lang.LinkageError , that is, when you forgot to add <scope>provided</scope> to your javax.servlet-api dependency. In this case, an instance of the listener is created, but only the static part is executed, and not the contextInitialized and contextDestroyed .

You will only find out when you invoke some servlet, because a binding error does not occur during the creation of the listener instance.

+2
source share

All Articles