Example usage for javax.servlet ServletContext addListener

List of usage examples for javax.servlet ServletContext addListener

Introduction

In this page you can find the example usage for javax.servlet ServletContext addListener.

Prototype

public void addListener(Class<? extends EventListener> listenerClass);

Source Link

Document

Adds a listener of the given class type to this ServletContext.

Usage

From source file:com.sishuok.chapter2.initializer.NoXmlWebAppInitializer.java

@Override
public void onStartup(final ServletContext sc) throws ServletException {

    //1?/*from  w ww  .  j  a  v a2s.c  o m*/
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(RootConfiguration.class);
    sc.addListener(new ContextLoaderListener(rootContext));

    //2?springmvc
    AnnotationConfigWebApplicationContext springMvcContext = new AnnotationConfigWebApplicationContext();
    springMvcContext.register(SpringMvcConfiguration.class);

    //3?DispatcherServlet
    DispatcherServlet dispatcherServlet = new DispatcherServlet(springMvcContext);

    ServletRegistration.Dynamic dynamic = sc.addServlet("dispatcherServlet", dispatcherServlet);
    dynamic.setLoadOnStartup(1);
    dynamic.addMapping("/");

}

From source file:com.tamnd.app.init.WebMvcInitializer.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    // Create the 'root' Spring application context
    WebApplicationContext context = context();

    // Manage the lifecycle of the root application context
    servletContext.addListener(new ContextLoaderListener(context));

    DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
    dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);

    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcherServlet", dispatcherServlet);
    dispatcher.setLoadOnStartup(1);/*  ww  w .  j  av  a2  s.  c  om*/
    dispatcher.addMapping(MAPPING_URL);

    servletContext
            .addFilter("securityFilter",
                    new DelegatingFilterProxy("springSecurityFilterChain"/*Do not change this name*/))
            .addMappingForUrlPatterns(null, false, "/*");
    //      servletContext.addFilter("hibernateFilter", new OpenSessionInViewFilter())
    //            .addMappingForUrlPatterns(null, false, "/*");
}

From source file:org.hdiv.spring.boot.autoconfigure.HdivAutoConfiguration.java

@Bean
public ServletContextInitializer validatorFilter() {
    ServletContextInitializer initializer = new ServletContextInitializer() {

        public void onStartup(ServletContext servletContext) throws ServletException {

            // Register HDIV InitListener and ValidatorFilter

            // InitListener
            servletContext.addListener(new InitListener());

            // ValidatorFilter
            FilterRegistration.Dynamic registration = servletContext.addFilter("validatorFilter",
                    new ValidatorFilter());
            EnumSet<DispatcherType> dispatcherTypes = EnumSet.of(DispatcherType.REQUEST);
            // isMatchAfter false to put it before existing filters
            registration.addMappingForUrlPatterns(dispatcherTypes, false, "/*");
        }/*  ww w . ja v a 2 s .c om*/
    };
    return initializer;
}

From source file:br.eti.danielcamargo.backend.common.config.WebAppInitializer.java

private WebApplicationContext createRootContext(ServletContext servletContext) {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(CoreConfig.class, SecurityConfig.class);
    rootContext.refresh();/*from   w  ww .ja v a 2s  .  co  m*/
    servletContext.addListener(new ContextLoaderListener(rootContext));
    servletContext.setInitParameter("defaultHtmlEscape", "true");
    return rootContext;
}

From source file:net.eusashead.hateoas.springhalbuilder.config.WebInitializer.java

@Override
public void onStartup(ServletContext ctx) throws ServletException {

    // Set up the app ctx
    AnnotationConfigWebApplicationContext rootCtx = new AnnotationConfigWebApplicationContext();
    rootCtx.register(WebConfig.class);

    // Set the listener
    ctx.addListener(new ContextLoaderListener(rootCtx));

    // Register the Spring Dispatcher servlet
    ServletRegistration.Dynamic dispatcher = ctx.addServlet("dispatcher", new DispatcherServlet(rootCtx));
    dispatcher.setLoadOnStartup(1);//from   w w  w .  ja  v  a  2 s .co  m
    dispatcher.addMapping("/*");

}

From source file:com.kajj.tools.logviewer.config.LogViewerWebApplicationInitializer.java

/**
 * Initialization method for the ServletContext. Creates the Spring context and
 * DispatcherServlet.// ww w .ja va 2s  .c  om
 *
 * @param servletContext The ServletContext to initialize.
 * @throws ServletException If the ServletContext fails to initialize.
 */
@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
    final AnnotationConfigWebApplicationContext springContext = new AnnotationConfigWebApplicationContext();
    springContext.setConfigLocation("com.kajj.tools.logviewer.config");
    servletContext.addListener(new ContextLoaderListener(springContext));

    final DispatcherServlet servlet = new DispatcherServlet(springContext);
    final ServletRegistration.Dynamic dispatcher = servletContext.addServlet(DISPATCHER_SERVLET_NAME, servlet);
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping(MAPPING_ANY);
}

From source file:net.oneandone.stool.overview.initializer.ApplicationInitializer.java

@Override
public void afterSpringSecurityFilterChain(ServletContext servletContext) {
    AnnotationConfigWebApplicationContext context;
    ServletRegistration.Dynamic dispatcher;

    context = new AnnotationConfigWebApplicationContext();
    context.setConfigLocation("net.oneandone.stool.overview.config");
    servletContext.addListener(new ContextLoaderListener(context));
    dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
    dispatcher.setLoadOnStartup(1);//  ww w  . ja  va  2 s . c om
    dispatcher.addMapping("/");
}

From source file:com.nebhale.cyclinglibrary.web.ApplicationInitializer.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(UtilConfiguration.class, RepositoryConfiguration.class);

    servletContext.addListener(new ContextLoaderListener(rootContext));

    AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
    webContext.register(WebConfiguration.class);

    ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("dispatcher",
            new DispatcherServlet(webContext));
    dispatcherServlet.setLoadOnStartup(1);
    dispatcherServlet.addMapping("/");

    FilterRegistration.Dynamic gzipFilter = servletContext.addFilter("gzip", new GzipFilter());
    gzipFilter.addMappingForServletNames(null, false, "dispatcher");

    FilterRegistration.Dynamic eTagFilter = servletContext.addFilter("etag", new ShallowEtagHeaderFilter());
    eTagFilter.addMappingForServletNames(null, false, "dispatcher");
}

From source file:echec.spring.SpringConfigWebApplicationInitializer.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {

    // Initialise spring au dmarrage de l'application web.
    AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
    appContext.register(SpringConfig.class);
    servletContext.addListener(new ContextLoaderListener(appContext));

    // Register and map the dispatcher servlet
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
            new DispatcherServlet(appContext));
    dispatcher.setLoadOnStartup(1);/* ww w .j a  v  a2  s  .  c o  m*/
    dispatcher.addMapping("/");
}

From source file:org.kew.rmf.reconciliation.config.WebAppInitializer.java

private WebApplicationContext createRootContext(ServletContext servletContext) {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(CoreConfig.class);
    rootContext.refresh();//from www  . j a v  a 2 s  .c  o m

    servletContext.addListener(new ContextLoaderListener(rootContext));
    servletContext.setInitParameter("defaultHtmlEscape", "true");

    return rootContext;
}