Example usage for javax.servlet ServletContext addServlet

List of usage examples for javax.servlet ServletContext addServlet

Introduction

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

Prototype

public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass);

Source Link

Document

Adds the servlet with the given name and class type to this servlet context.

Usage

From source file:ca.unx.template.config.WebAppInitializer.java

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

    /* Let super do its thing... */
    super.onStartup(servletContext);

    /* Add the Spring Security filter. */
    servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy())
            .addMappingForUrlPatterns(null, false, "/*");

    // Add metrics servlet.
    ServletRegistration.Dynamic metricsServlet = servletContext.addServlet("metrics", AdminServlet.class);
    metricsServlet.addMapping("/metrics/*");
}

From source file:com.dm.estore.config.WebAppInitializer.java

private void registerServlets(ServletContext servletContext) {
    // Enable Spring Data REST in the DispatcherServlet
    AnnotationConfigWebApplicationContext webCtx = new AnnotationConfigWebApplicationContext();
    webCtx.register(RestAPIModule.class);

    // REST API dispatcher
    DispatcherServlet dispatcherServlet = new DispatcherServlet(webCtx);
    ServletRegistration.Dynamic restDispatcher = servletContext.addServlet("restAPI", dispatcherServlet);
    restDispatcher.setLoadOnStartup(1);/*from  w w w  .j  a  v a 2  s .c o  m*/
    restDispatcher.setAsyncSupported(true);
    restDispatcher.addMapping(REST_SERVLET_MAPPING);
}

From source file:com.dominion.salud.nomenclator.configuration.NOMENCLATORInitializer.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.scan("com.dominion.salud.nomenclator.configuration");
    ctx.setServletContext(servletContext);
    ctx.refresh();/*from  ww w  .  j  a  v a 2  s. c o m*/

    logger.info("     Registrando servlets de configuracion");
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
            new DispatcherServlet(ctx));
    dispatcher.setInitParameter("contextClass", ctx.getClass().getName());
    dispatcher.setLoadOnStartup(1);
    logger.info("          Agregando mapping: /");
    dispatcher.addMapping("/");
    logger.info("          Agregando mapping: /controller/*");
    dispatcher.addMapping("/controller/*");
    logger.info("          Agregando mapping: /services/*");
    dispatcher.addMapping("/services/*");
    servletContext.addListener(new ContextLoaderListener(ctx));
    logger.info("     Servlets de configuracion registrados correctamente");

    // Configuracion general
    logger.info("     Iniciando la configuracion del modulo");
    NOMENCLATORConstantes._HOME = StringUtils.endsWith(servletContext.getRealPath("/"), File.separator)
            ? servletContext.getRealPath("/")
            : servletContext.getRealPath("/") + File.separator;
    NOMENCLATORConstantes._TEMP = NOMENCLATORConstantes._HOME + "WEB-INF" + File.separator + "temp"
            + File.separator;
    NOMENCLATORConstantes._VERSION = ResourceBundle.getBundle("version").getString("version");
    NOMENCLATORConstantes._LOGS = NOMENCLATORConstantes._HOME + "WEB-INF" + File.separator + "classes"
            + File.separator + "logs";
    NOMENCLATORConstantes._CONTEXT_NAME = servletContext.getServletContextName();
    NOMENCLATORConstantes._CONTEXT_PATH = servletContext.getContextPath();
    NOMENCLATORConstantes._CONTEXT_SERVER = servletContext.getServerInfo();
    NOMENCLATORConstantes._ENABLE_TECHNICAL_INFORMATION = StringUtils.isNotBlank(
            ResourceBundle.getBundle("application").getString("nomenclator.enable.technical.information"))
                    ? Boolean.parseBoolean(ResourceBundle.getBundle("application")
                            .getString("nomenclator.enable.technical.information"))
                    : false;
    PropertyConfigurator.configureAndWatch("log4j");

    logger.info("          Configuracion general del sistema");
    logger.info("               nomenclator.home: " + NOMENCLATORConstantes._HOME);
    logger.info("               nomenclator.temp: " + NOMENCLATORConstantes._TEMP);
    logger.info("               nomenclator.version: " + NOMENCLATORConstantes._VERSION);
    logger.info("               nomenclator.logs: " + NOMENCLATORConstantes._LOGS);
    logger.info("               nomenclator.context.name: " + NOMENCLATORConstantes._CONTEXT_NAME);
    logger.info("               nomenclator.context.path: " + NOMENCLATORConstantes._CONTEXT_PATH);
    logger.info("               nomenclator.context.server: " + NOMENCLATORConstantes._CONTEXT_SERVER);
    logger.info("          Parametrizacion del sistema");
    logger.info("               nomenclator.enable.technical.information: "
            + NOMENCLATORConstantes._ENABLE_TECHNICAL_INFORMATION);
    logger.info("     Modulo configurado correctamente");
    logger.info("MODULO INICIADO CORRECTAMENTE");
}

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);/* www . jav a2s.c  o  m*/
    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:fi.m1kah.web.WebAppInitializer.java

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

    servletContext.addListener(new ContextLoaderListener(rootContext));

    AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
    dispatcherContext.register(DispatcherConfig.class);

    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
            new DispatcherServlet(dispatcherContext));
    dispatcher.setLoadOnStartup(1);//  w ww  .  java  2 s .co m
    dispatcher.addMapping("/api/*");
}

From source file:org.jbr.taskmgr.config.WebInitializer.java

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

    // Create the dispatcher servlet's Spring application context
    AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
    dispatcherContext.setParent(containerContext);
    dispatcherContext.register(WebMvcConfig.class);
    dispatcherContext.registerShutdownHook();

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

From source file:com.coffeebeans.services.config.initializer.WebAppInitializer.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(AppConfig.class);
    servletContext.addListener(new ContextLoaderListener(rootContext));

    AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
    dispatcherServlet.register(MvcConfig.class);

    ServletRegistration.Dynamic dispatcher = servletContext.addServlet(MVC_DISPATCHER_NAME,
            new DispatcherServlet(dispatcherServlet));
    dispatcher.setLoadOnStartup(1);/*from  w ww.  j a va  2 s.com*/
    dispatcher.addMapping("/1/*");
    dispatcher.addMapping("/oauth/token");

    FilterRegistration charEncodingFilterReg = servletContext.addFilter("CharacterEncodingFilter",
            CharacterEncodingFilter.class);
    charEncodingFilterReg.setInitParameter("encoding", "UTF-8");
    charEncodingFilterReg.setInitParameter("forceEncoding", "true");
    charEncodingFilterReg.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");

}

From source file:org.statefulj.demo.ddd.config.MVCInitializer.java

@Override
protected void registerDispatcherServlet(ServletContext servletContext) {
    String servletName = getServletName();

    WebApplicationContext servletAppContext = createServletApplicationContext();

    DispatcherServlet dispatcherServlet = new DispatcherServlet(servletAppContext);

    // throw NoHandlerFoundException to Controller when a User requests a non-existent page
    ///*  ww w .  ja  va2 s  .  com*/
    dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);

    ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet);

    registration.setLoadOnStartup(1);
    registration.addMapping(getServletMappings());
    registration.setAsyncSupported(isAsyncSupported());

    Filter[] filters = getServletFilters();
    if (!ObjectUtils.isEmpty(filters)) {
        for (Filter filter : filters) {
            registerServletFilter(servletContext, filter);
        }
    }

    customizeRegistration(registration);
}

From source file:com.test.config.BackendConsoleWebConfig.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext webCtx = new AnnotationConfigWebApplicationContext();
    webCtx.register(BackendConsoleMVCConfig.class);
    webCtx.register(BackendConsoleConfig.class);

    servletContext.addListener(new ContextLoaderListener(webCtx));

    /* Spring Delegating Dispatcher Servlet */
    Servlet dispatcherServlet = new DispatcherServlet(webCtx);
    ServletRegistration.Dynamic dispatcherServletReg = servletContext.addServlet("dispatcherServlet",
            dispatcherServlet);/* www .  ja  va  2 s.c o  m*/
    dispatcherServletReg.setLoadOnStartup(1);
    dispatcherServletReg.setInitParameter("contextConfigLocation", "");
    dispatcherServletReg.addMapping("/");

    /* Spring Security Delegating Filter */
    FilterRegistration springSecurityFilterChainReg = servletContext.addFilter("springSecurityFilterChain",
            DelegatingFilterProxy.class);
    springSecurityFilterChainReg.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST,
            DispatcherType.FORWARD, DispatcherType.ERROR, DispatcherType.ASYNC), false,
            dispatcherServletReg.getName());
}

From source file:com.kabiliravi.kaman.web.KamanApplicationInitializer.java

private void registerDispatcherServlet(final ServletContext servletContext) {
    WebApplicationContext dispatcherContext = createContext(WebMvcContextConfiguration.class);
    servletContext.addListener(new ContextLoaderListener(dispatcherContext));
    servletContext.addListener(new RequestContextListener());
    DispatcherServlet dispatcherServlet = new DispatcherServlet(dispatcherContext);
    ServletRegistration.Dynamic dispatcher;
    dispatcher = servletContext.addServlet("dispatcher", dispatcherServlet);
    dispatcher.setLoadOnStartup(1);/*from   www .  j a  va  2s  .c  om*/
    dispatcher.addMapping("/");
}