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:eionet.transfer.TransferWebAppInitializer.java

@Override
public void onStartup(ServletContext container) {
    XmlWebApplicationContext appContext = new XmlWebApplicationContext();
    appContext.setConfigLocation("/WEB-INF/classes/spring-db-config.xml "
            + "/WEB-INF/classes/spring-init-config.xml " + "/WEB-INF/classes/spring-mvc-config.xml");

    // Listeners/*from ww w . j a v  a2  s  .c  om*/
    //SingleSignOutHttpSessionListener ssoListener = new SingleSignOutHttpSessionListener(appContext);
    //container.addListener(ssoListener);

    //ContextLoaderListener contextLoaderListener = new ContextLoaderListener(appContext);
    //container.addListener(contextLoaderListener);

    ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher",
            new DispatcherServlet(appContext));

    String location = null;
    try {
        location = System.getProperty("upload.dir", null);
    } catch (Exception e) {
        location = null;
    }

    logger.info("Upload directory configured to: " + (location == null ? "[default]" : location));

    //long maxFileSize = 5000000L;
    //long maxRequestSize = 5000000L;
    //int fileSizeThreshold = 0;
    MultipartConfigElement multipartConfigElement = new MultipartConfigElement(location);
    //MultipartConfigElement multipartConfigElement = new MultipartConfigElement(location, maxFileSize, maxRequestSize, fileSizeThreshold);
    dispatcher.setMultipartConfig(multipartConfigElement);

    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
}

From source file:com.haulmont.cuba.web.sys.singleapp.SingleAppWebContextLoader.java

protected void registerAppServlet(ServletContext servletContext) {
    CubaApplicationServlet cubaServlet = new CubaApplicationServlet();
    cubaServlet.setClassLoader(Thread.currentThread().getContextClassLoader());
    try {//  w  w w . j ava 2s .co m
        cubaServlet.init(new CubaServletConfig("app_servlet", servletContext));
    } catch (ServletException e) {
        throw new RuntimeException("An error occurred while initializing app_servlet servlet", e);
    }
    ServletRegistration.Dynamic cubaServletReg = servletContext.addServlet("app_servlet", cubaServlet);
    cubaServletReg.setLoadOnStartup(0);
    cubaServletReg.setAsyncSupported(true);
    cubaServletReg.addMapping("/*");
}

From source file:com.jjcosare.calculator.config.WebInitializer.java

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

    // Create the 'root' Spring application context
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(WebConfig.class);

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

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

    // Register and map the dispatcher servlet
    ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher",
            new DispatcherServlet(dispatcherContext));
    dispatcher.setLoadOnStartup(1);/*  w w  w .ja v a  2s  . co  m*/
    dispatcher.addMapping("/");
}

From source file:com.olegchir.fadeok.init.AppInitializer.java

public void onStartup(ServletContext servletContext) throws ServletException {
    System.out.println("Initializing Application for " + servletContext.getServerInfo());

    //Create webapp context
    AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
    applicationContext.setConfigLocation(CONFIG_LOCATION);

    servletContext.addListener(new ContextLoaderListener(applicationContext));
    //        servletContext.addListener(new CompleteAutoloadTilesListener());

    // Add the servlet mapping manually and make it initialize automatically
    DispatcherServlet dispatcherServlet = new DispatcherServlet(applicationContext);
    ServletRegistration.Dynamic servlet = servletContext.addServlet("mvc-dispatcher", dispatcherServlet);

    servlet.addMapping("/");
    servlet.setAsyncSupported(true);/*w ww  .  ja v  a2s  . c o m*/
    servlet.setLoadOnStartup(1);
}

From source file:ch.javaee.basicMvc.WebInitializer.java

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

    // Create the 'root' Spring application context
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(AppConfiguration.class);

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

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

    // Register and map the dispatcher servlet
    ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher",
            new DispatcherServlet(dispatcherContext));
    dispatcher.setLoadOnStartup(1);//  w  w  w . ja  va2  s.  c o m
    dispatcher.addMapping("/");

}

From source file:com.digitgroup.fullstackroad.spring.config.web.WebAppInitializer.java

@Override
public void onStartup(ServletContext container) {
    // Create the 'root' Spring application context
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(AppRootConfig.class, PersistenceConfig.class);

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

    // Create the dispatcher servlet's Spring application context
    AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
    dispatcherServlet.register(WebConfig.class);

    // Register and map the dispatcher servlet
    ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher",
            new DispatcherServlet(dispatcherServlet));
    dispatcher.setLoadOnStartup(1);//from  www  .  j a  va  2s  .  c  o m
    dispatcher.addMapping("/");

}

From source file:org.avidj.zuul.rs.ZuulInitializer.java

@Override
public void onStartup(ServletContext container) {
    // Create the 'root' Spring application context
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(RootContextConfiguration.class);

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

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

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

From source file:net.przemkovv.sphinx.config.ApplicationInitializer.java

public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(ApplicationConfig.class, JpaConfig.class);
    //        rootContext.refresh();
    rootContext.setDisplayName("Sphinx Web Application");

    servletContext.addListener(new ContextLoaderListener(rootContext));
    //        servletContext.addListener(new HttpSessionEventPublisher());

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

    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("Spring MVC Servlet",
            new DispatcherServlet(dispatcherContext));
    dispatcher.addMapping("/app/");
    dispatcher.setLoadOnStartup(1);/*w w w  .  j  av a2  s .c om*/

    //        servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy("springSecurityFilterChain"))
    //                .addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");
    servletContext.addFilter("UrlRewriteFilter", new UrlRewriteFilter()).addMappingForUrlPatterns(null, true,
            "/*");
    servletContext.addFilter("HttpMethodFilter", new HiddenHttpMethodFilter()).addMappingForUrlPatterns(null,
            true, "/*");
    servletContext.addFilter("HttpPutFormContentFilter", new HttpPutFormContentFilter())
            .addMappingForUrlPatterns(null, true, "/*");

    FilterRegistration.Dynamic charsetFilter = servletContext.addFilter("charsetFilter",
            new CharacterEncodingFilter());
    charsetFilter.setInitParameter("encoding", "UTF-8");
    charsetFilter.setInitParameter("forceEncoding", "true");
    charsetFilter.addMappingForUrlPatterns(null, true, "/*");

    FilterRegistration.Dynamic dustCompilingFilter = servletContext.addFilter("dustCompilingFilter",
            new DustCompilingFilter());
    dustCompilingFilter.setInitParameter("templateNameRegex", "/template/(.*).dust.js$");
    dustCompilingFilter.addMappingForUrlPatterns(null, true, "/*");

}

From source file:com.dominion.salud.pedicom.configuration.PEDICOMInitializer.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.scan("com.dominion.salud.pedicom.configuration");
    ctx.setServletContext(servletContext);
    PEDICOMConstantes._NOMBRE_CONFIG = servletContext.getInitParameter("NOMBRE_CONFIG");
    System.setProperty("pedicom.conf.home", findConfigurationAndLogger(ctx));
    ctx.refresh();//from w ww  .  jav a 2s.co  m

    // Spring Dispatcher
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
            new DispatcherServlet(ctx));
    dispatcher.setInitParameter("contextClass", ctx.getClass().getName());
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
    dispatcher.addMapping("/controller/*");
    servletContext.addListener(new ContextLoaderListener(ctx));

    // Configuracion general
    PEDICOMConstantes._HOME = StringUtils.endsWith(servletContext.getRealPath("/"), File.separator)
            ? servletContext.getRealPath("/")
            : servletContext.getRealPath("/") + File.separator;
    PEDICOMConstantes._CONF_HOME = ctx.getEnvironment().getProperty("pedicom.conf.home");
    PEDICOMConstantes._TEMP = PEDICOMConstantes._HOME + "WEB-INF" + File.separator + "temp" + File.separator;
    PEDICOMConstantes._VERSION = ResourceBundle.getBundle("version").getString("version");
    PEDICOMConstantes._LOGS = PEDICOMConstantes._HOME + "WEB-INF" + File.separator + "classes" + File.separator
            + "logs";
    PEDICOMConstantes._CONTEXT_NAME = servletContext.getServletContextName();
    PEDICOMConstantes._CONTEXT_PATH = servletContext.getContextPath();
    PEDICOMConstantes._CONTEXT_SERVER = servletContext.getServerInfo();
    PEDICOMConstantes._ENABLE_TECHNICAL_INFORMATION = StringUtils.isNotBlank(
            ResourceBundle.getBundle("application").getString("pedicom.enable.technical.information"))
                    ? Boolean.parseBoolean(ResourceBundle.getBundle("application")
                            .getString("pedicom.enable.technical.information"))
                    : false;
    PEDICOMConstantes._SCHEDULER_SEND_MAIL_CRON = StringUtils
            .isNotBlank(ResourceBundle.getBundle("application").getString("pedicom_scheduler_send_mail_cron"))
                    ? PEDICOMConstantes._SCHEDULER_SEND_MAIL_CRON = ResourceBundle.getBundle("application")
                            .getString("pedicom_scheduler_send_mail_cron")
                    : PEDICOMConstantes._SCHEDULER_SEND_MAIL_CRON;
    PEDICOMConstantes._SCHEDULER_UPDATE_EXISTENCIAS_CRON = StringUtils.isNotBlank(
            ResourceBundle.getBundle("application").getString("pedicom_scheduler_update_existencias_cron"))
                    ? PEDICOMConstantes._SCHEDULER_SEND_MAIL_CRON = ResourceBundle.getBundle("application")
                            .getString("pedicom_scheduler_update_existencias_cron")
                    : PEDICOMConstantes._SCHEDULER_UPDATE_EXISTENCIAS_CRON;

    // Configuracion de LOGS DEL MODULO
    if (StringUtils.isBlank(
            ((FileAppender) org.apache.log4j.Logger.getRootLogger().getAppender("LOGFILE")).getFile())) {
        ((FileAppender) org.apache.log4j.Logger.getRootLogger().getAppender("LOGFILE"))
                .setFile(PEDICOMConstantes._HOME + "WEB-INF" + File.separator + "classes" + File.separator
                        + "logs" + File.separator + "mpr-desktop.log");
    }
    PEDICOMConstantes._LOGS = new File(
            ((FileAppender) org.apache.log4j.Logger.getRootLogger().getAppender("LOGFILE")).getFile())
                    .getParent();

    Environment env = ctx.getEnvironment();

    XmlUnmarshaler xml = new XmlUnmarshaler();
    Datos datos = (Datos) xml.unmarshal();
    logger.info("          Datasources");
    for (Datasources dat : datos.getDatasources()) {
        if (dat.getNombreDatasource().equals("Central")) {
            PEDICOMConstantes.EXISTENCIAS_EXISTE = true;
        }
        logger.info("               codCentro: " + dat.getCodCentro());
        logger.info("               nombreDatasource: " + dat.getNombreDatasource());
        logger.info("               driverClassName: " + dat.getDriverClassName());
        logger.info("               jndi: " + dat.getJndi());
        logger.info("               url: " + dat.getUrl());
        logger.info("               username: " + dat.getUsername());
        logger.info("               usernameEmail: " + dat.getUsernameEmail());
        logger.info("               passwordEmail: " + dat.getPasswordEmail());
        logger.info("               from: " + dat.getFrom());
        logger.info("               host: " + dat.getHost());
        logger.info("               port: " + dat.getPort());
        logger.info("               TLS: " + dat.getTLS());
        logger.info("               SSL: " + dat.getSSL());
    }
    //        ctx.refresh();
    //        PropertyConfigurator.configureAndWatch("log4j");

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

From source file:eu.enhan.timelord.web.init.TimelordWebInit.java

/**
 * @see org.springframework.web.WebApplicationInitializer#onStartup(javax.servlet.ServletContext)
 *//*w  ww  .  ja  va2  s.co m*/
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootCtx = new AnnotationConfigWebApplicationContext();
    rootCtx.register(AppConfig.class);
    servletContext.addListener(new ContextLoaderListener(rootCtx));

    AnnotationConfigWebApplicationContext webAppCtx = new AnnotationConfigWebApplicationContext();
    webAppCtx.setParent(rootCtx);
    webAppCtx.register(WebConfig.class);

    XmlWebApplicationContext securityCtx = new XmlWebApplicationContext();
    securityCtx.setServletContext(servletContext);
    securityCtx.setParent(rootCtx);
    securityCtx.setConfigLocation("classpath:/spring/security.xml");

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

    FilterRegistration.Dynamic securityFilter = servletContext.addFilter("springSecurityFilterChain",
            new DelegatingFilterProxy("springSecurityFilterChain", securityCtx));
    //   securityFilter.addMappingForUrlPatterns(null, false, "/**");
    securityFilter.addMappingForServletNames(null, false, "dispatcher");

}