Create Servlet Listener using @WebListener

[Updated: Sep 3, 2016, Created: Jan 22, 2016]

@WebListener annotation is used to define a servlet listener component in a web application.

Java Servlet events involving life cycles of ServletContext, HttpSession and ServletRequest along with corresponding listeners allow application developers a better design and code modularization. Even though many listeners are defined in javax.servlet and javax.servlet.http packages, only following are supported for @WebListener

  • javax.servlet.ServletContextListener
  • javax.servlet.ServletContextAttributeListener
  • javax.servlet.ServletRequestListener
  • javax.servlet.ServletRequestAttributeListener
  • javax.servlet.http.HttpSessionListener
  • javax.servlet.http.HttpSessionAttributeListener

In following example we are going to show ServletContextListener with @WebListener annotation. ServletContextListener is used in scenarios where we want to do some initializing at the container start up before any web components like servlets get initialized. In this example we are going to initialize a global cache (Guava Cache) in the listener which can be used by multiple components.

Let's consider following web.xml listener configuration first.


We going to create equivalent annotation based listener.

  1. Prepare project
    • Create web application using maven-archetype-webapp, steps here.
    • Delete web.xml, we don't need it at all.
    • In pom.xml add dependency of javax.servlet-api:3.0.1
    • In pom.xml add dependency of guava:19.0
    • In pom.xml add tomcat7-maven-plugin to run it as embedded server.
  2. Create a context listener class AppContextListener, annotated with @WebListener.
  3. Create a servlet class AppController annotated with @WebServlet.
  4. Now we are going to run our web application from root folder:
    mvn clean tomcat7:run-war
  5. Put following url in your browser:
    You should see the page output as printed in the servlet.
    Cached data for key
    Also see the console output where we ran the tomcat server:
    context initialized com.logicbig.listener.AppContextListener@751d58bc
    servlet loaded com.logicbig.servlet.AppController@7588c230
    Dec 18, 2015 8:15:30 PM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler ["http-bio-8080"]
    getting data for key 
    Note that the servlet AppController loaded after AppContextListener even though we set the servlet as loadOnStartup = 1

Example Project

Dependencies and Technologies Used:

  • Java Servlet API 3.0.1
  • Guava: Google Core Libraries for Java 19.0: Guava is a suite of core and expanded libraries that include utility classes, google's collections, io classes, and much much more. Guava has only one code dependency - javax.annotation, per the JSR-305 spec.
  • JDK 1.8
  • Maven 3.0.4

Web Listener Example Select All Download
  • web-listener-example
    • src
      • main
        • java
          • com
            • logicbig
              • listener
          • com.logicbig.servlet
        • webapp

See Also