Servlet, Filters and Listeners

Servlets, filters and listeners can be detected and registered by SeedStack which makes them injectable and interceptable.

Servlets

You can register a servlet by annotating your servlet class with @WebServlet:

@WebServlet("/my-servlet")
public class MyServlet extends HttpServlet {
    ...
}

You can specify a priority on each listener to order them in the context initialization and destruction sequences: add a @Priority annotation to the listener class with the absolute priority as value. Non-annotated listeners have a default priority of 0.

Filters

Similarly, you can register a filter by annotating your filter class with @WebFilter:

@WebFilter("/*")
public class MyFilter implements Filter {
    ...
}

You can specify a priority on each filter to order them in the chain filter: add a @Priority annotation to the filter class with the absolute priority as value. Non-annotated filters have a default priority of 0.

You can find the priorities of SeedStack built-in filters by looking at the SeedFilterPriority class.

Listeners

Also, you can register a listener by annotating your listener class with @WebListener

@WebListener
public class MyListener implements ServletContextListener {
    ...
}

Any class annotated with WebListener must implement one or more of the ServletContextListener, ServletContextAttributeListener, ServletRequestListener, ServletRequestAttributeListener, HttpSessionListener, HttpSessionAttributeListener or HttpSessionIdListener interfaces.

Disabling container scanning

If you are running in a Web container also scanning those annotations (like Tomcat), you need to disable the server detection and let SeedStack do the registration. This allows to use injection and interception in servlets, filters and listeners.

To disable container scanning, add or modify the web.xml file under the WEB-INF/ directory of the WAR archive. This file must have the metadata-complete attribute set to true.

Example for Servlet 3.1 level:

<?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"
    metadata-complete="true">
</web-app>    
   

On this page


Edit