3.10.5. Registration of Main Servlets and Filters Programmatically

Usually, the main servlets (CubaApplicationServlet, CubaDispatcherServlet) and filters (CubaHttpFilter) are registered in the Web Client block’s web.xml configuration file:

<servlet>
    <servlet-name>app_servlet</servlet-name>
    <servlet-class>com.haulmont.cuba.web.sys.CubaApplicationServlet</servlet-class>
    <async-supported>true</async-supported>
</servlet>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>com.haulmont.cuba.web.sys.CubaDispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/dispatch/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>app_servlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>cuba_filter</filter-name>
    <filter-class>com.haulmont.cuba.web.sys.CubaHttpFilter</filter-class>
    <async-supported>true</async-supported>
</filter>
<filter-mapping>
    <filter-name>cuba_filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

However, sometimes the main servlets and filters have to be registered programmatically.

Here is an example of a bean that initializes the main servlets and filters:

@Component(MainServletsInitializer.NAME)
public class MainServletsInitializer {

    public static final String NAME = "demo_MainServletsInitializer";

    @Inject
    protected ServletRegistrationManager servletRegistrationManager;

    @EventListener
    public void initServlets(ServletContextInitializedEvent event){
        initAppServlet(event); (1)
        initDispatcherServlet(event); (2)
        initCubaFilter(event); (3)
    }

    protected void initAppServlet(ServletContextInitializedEvent event) {
        CubaApplicationServlet cubaServlet = (CubaApplicationServlet) servletRegistrationManager.createServlet(
                event.getApplicationContext(),
                "com.haulmont.cuba.web.sys.CubaApplicationServlet");
        cubaServlet.setClassLoader(Thread.currentThread().getContextClassLoader());
        ServletRegistration.Dynamic registration = event.getSource()
                .addServlet("app_servlet", cubaServlet); (4)
        registration.setLoadOnStartup(0);
        registration.setAsyncSupported(true);
        registration.addMapping("/*");
        JSR356WebsocketInitializer.initAtmosphereForVaadinServlet(registration, event.getSource());  (5)
        try {
            cubaServlet.init(new AbstractWebAppContextLoader.CubaServletConfig("app_servlet", event.getSource()));  (6)
        } catch (ServletException e) {
            throw new RuntimeException("An error occurred while initializing app_servlet servlet", e);
        }
    }

    protected void initDispatcherServlet(ServletContextInitializedEvent event) {
        CubaDispatcherServlet cubaDispatcherServlet = (CubaDispatcherServlet) servletRegistrationManager.createServlet(
                event.getApplicationContext(),
                "com.haulmont.cuba.web.sys.CubaDispatcherServlet");
        try {
            cubaDispatcherServlet.init(
                    new AbstractWebAppContextLoader.CubaServletConfig("dispatcher", event.getSource()));
        } catch (ServletException e) {
            throw new RuntimeException("An error occurred while initializing dispatcher servlet", e);
        }
        ServletRegistration.Dynamic cubaDispatcherServletReg = event.getSource()
                .addServlet("dispatcher", cubaDispatcherServlet);
        cubaDispatcherServletReg.setLoadOnStartup(1);
        cubaDispatcherServletReg.addMapping("/dispatch/*");
    }

    protected void initCubaFilter(ServletContextInitializedEvent event) {
        CubaHttpFilter cubaHttpFilter = (CubaHttpFilter) servletRegistrationManager.createFilter(
                event.getApplicationContext(),
                "com.haulmont.cuba.web.sys.CubaHttpFilter");
        FilterRegistration.Dynamic registration = event.getSource()
                .addFilter("cuba_filter", cubaHttpFilter);
        registration.setAsyncSupported(true);
        registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
    }
}
1 - registration and initializing of the CubaApplicationServlet.
2 - registration and initializing of the CubaDispatcherServlet.
3 - registration and initializing of the CubaHttpFilter.
4 - we need to register servlet first in order to initialize Atmosphere Framework.
5 - initialize the JSR 356 explicitly.
6 - initialize the servlet.

See SingleAppWebContextLoader for more details.