Java Code Examples for javax.servlet.ServletContext#addServlet()

The following examples show how to use javax.servlet.ServletContext#addServlet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestServletInjector.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public Dynamic inject(ServletContext servletContext, String urlPattern) {
  String[] urlPatterns = splitUrlPattern(urlPattern);
  if (urlPatterns.length == 0) {
    LOGGER.warn("urlPattern is empty, ignore register {}.", SERVLET_NAME);
    return null;
  }

  String listenAddress = ServletConfig.getLocalServerAddress();
  if (!ServletUtils.canPublishEndpoint(listenAddress)) {
    LOGGER.warn("ignore register {}.", SERVLET_NAME);
    return null;
  }

  // dynamic deploy a servlet to handle serviceComb RESTful request
  Dynamic dynamic = servletContext.addServlet(SERVLET_NAME, RestServlet.class);
  dynamic.setAsyncSupported(true);
  dynamic.addMapping(urlPatterns);
  dynamic.setLoadOnStartup(0);
  LOGGER.info("RESTful servlet url pattern: {}.", Arrays.toString(urlPatterns));

  return dynamic;
}
 
Example 2
Source File: WebAppInitializer.java    From Spring with Apache License 2.0 6 votes vote down vote up
@Override
	public void onStartup(ServletContext servletContext) {
		final AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
		context.register(WebConfig.class);

		servletContext.addListener(new ContextLoaderListener(context));

		final ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcherServlet", new DispatcherServlet(context));
		dispatcher.setLoadOnStartup(1);

		dispatcher.addMapping("/");
		dispatcher.addMapping("*.html");
		dispatcher.addMapping("*.pdf");
		dispatcher.addMapping("*.json");

//		dispatcher.setInitParameter("contextConfigLocation", "com.spring4.application.configuration.ApplicationConfig");
//		dispatcher.setInitParameter("contextClass", "org.springframework.web.context.support.AnnotationConfigWebApplicationContext");
	}
 
Example 3
Source File: WebAppConfiguration.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.setServletContext(servletContext);
    rootContext.register(ThymeleafConfig.class);

    servletContext.addListener(new SakaiContextLoaderListener(rootContext));

    servletContext.addFilter("sakai.request", RequestFilter.class)
            .addMappingForUrlPatterns(
                    EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE),
                    true,
                    "/*");

    Dynamic servlet = servletContext.addServlet("sakai.onedrive", new DispatcherServlet(rootContext));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);
}
 
Example 4
Source File: TraceeInterceptorSpringApplicationInitializer.java    From tracee with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void initialize(AnnotationConfigWebApplicationContext applicationContext) {
	final AnnotationConfigWebApplicationContext rootCtx = new AnnotationConfigWebApplicationContext();
	rootCtx.register(TraceeInterceptorSpringConfig.class);
	final ServletContext servletContext = applicationContext.getServletContext();

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

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

	// Register and map the dispatcher servlet
	ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
	dispatcher.setLoadOnStartup(1);
	dispatcher.addMapping("/");

}
 
Example 5
Source File: WebConfigurer.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
/**
 * Initializes Spring and Spring MVC.
 */
private ServletRegistration.Dynamic initSpring(ServletContext servletContext, AnnotationConfigWebApplicationContext rootContext) {
    LOGGER.debug("Configuring Spring Web application context");
    AnnotationConfigWebApplicationContext dispatcherServletConfiguration = new AnnotationConfigWebApplicationContext();
    dispatcherServletConfiguration.setParent(rootContext);
    dispatcherServletConfiguration.register(DispatcherServletConfiguration.class);

    LOGGER.debug("Registering Spring MVC Servlet");
    ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherServletConfiguration));
    dispatcherServlet.addMapping("/service/*");
    dispatcherServlet.setMultipartConfig(new MultipartConfigElement((String) null));
    dispatcherServlet.setLoadOnStartup(1);
    dispatcherServlet.setAsyncSupported(true);

    return dispatcherServlet;
}
 
Example 6
Source File: AbstractDispatcherServletInitializer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Register a {@link DispatcherServlet} against the given servlet context.
 * <p>This method will create a {@code DispatcherServlet} with the name returned by
 * {@link #getServletName()}, initializing it with the application context returned
 * from {@link #createServletApplicationContext()}, and mapping it to the patterns
 * returned from {@link #getServletMappings()}.
 * <p>Further customization can be achieved by overriding {@link
 * #customizeRegistration(ServletRegistration.Dynamic)} or
 * {@link #createDispatcherServlet(WebApplicationContext)}.
 * @param servletContext the context to register the servlet against
 */
protected void registerDispatcherServlet(ServletContext servletContext) {
	String servletName = getServletName();
	Assert.hasLength(servletName, "getServletName() must not return null or empty");

	WebApplicationContext servletAppContext = createServletApplicationContext();
	Assert.notNull(servletAppContext, "createServletApplicationContext() must not return null");

	FrameworkServlet dispatcherServlet = createDispatcherServlet(servletAppContext);
	Assert.notNull(dispatcherServlet, "createDispatcherServlet(WebApplicationContext) must not return null");
	dispatcherServlet.setContextInitializers(getServletApplicationContextInitializers());

	ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet);
	if (registration == null) {
		throw new IllegalStateException("Failed to register servlet with name '" + servletName + "'. " +
				"Check if there is another servlet registered under the same name.");
	}

	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);
}
 
Example 7
Source File: WebConfigurer.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes Spring and Spring MVC.
 */
private ServletRegistration.Dynamic initSpring(ServletContext servletContext, AnnotationConfigWebApplicationContext rootContext) {
  log.debug("Configuring Spring Web application context");
  AnnotationConfigWebApplicationContext dispatcherServletConfiguration = new AnnotationConfigWebApplicationContext();
  dispatcherServletConfiguration.setParent(rootContext);
  dispatcherServletConfiguration.register(DispatcherServletConfiguration.class);

  log.debug("Registering Spring MVC Servlet");
  ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherServletConfiguration));
  dispatcherServlet.addMapping("/service/*");
  dispatcherServlet.setLoadOnStartup(1);
  dispatcherServlet.setAsyncSupported(true);

  return dispatcherServlet;
}
 
Example 8
Source File: WebAppInitializer.java    From Spring with Apache License 2.0 5 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext) {
	final AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.register(WebConfig.class);

	servletContext.addListener(new ContextLoaderListener(context));

	final ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcherServlet", new DispatcherServlet(context));
	dispatcher.setLoadOnStartup(1);
	dispatcher.addMapping("/invoker/*");
}
 
Example 9
Source File: SyncopeBuildToolsApplication.java    From syncope with Apache License 2.0 5 votes vote down vote up
@Override
public void onStartup(final ServletContext sc) throws ServletException {
    sc.addListener(new ConnectorServerStartStopListener());
    sc.addListener(new ApacheDSStartStopListener());
    sc.addListener(new H2StartStopListener());
    sc.addListener(new GreenMailStartStopListener());

    ServletRegistration.Dynamic apacheDS = sc.addServlet("ApacheDSRootDseServlet", ApacheDSRootDseServlet.class);
    apacheDS.addMapping("/apacheDS");
    ServletRegistration.Dynamic sts = sc.addServlet("ServiceTimeoutServlet", ServiceTimeoutServlet.class);
    sts.addMapping("/services/*");

    super.onStartup(sc);
}
 
Example 10
Source File: WebInitializer.java    From springMvc4.x-project with Apache License 2.0 5 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext)
		throws ServletException {
	AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
	ctx.register(MyMvcConfig.class);
	ctx.setServletContext(servletContext); // ②

	Dynamic servlet = servletContext.addServlet("dispatcher",new DispatcherServlet(ctx)); // 3
	servlet.addMapping("/");
	servlet.setLoadOnStartup(1);
}
 
Example 11
Source File: MojarraInitializer.java    From piranha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Initialize Mojarra.
 * 
 * @param classes the classes.
 * @param servletContext the Servlet context.
 * @throws ServletException when a Servlet error occurs.
 */
@Override
public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException {
    Dynamic dynamic = servletContext.addServlet("Faces Servlet", "javax.faces.webapp.FacesServlet");
    dynamic.addMapping("/faces/*", "*.html", "*.xhtml", "*.jsf");
    servletContext.setAttribute("com.sun.faces.facesInitializerMappingsAdded", TRUE);
    servletContext.addListener("com.sun.faces.config.ConfigureListener");
}
 
Example 12
Source File: TestContextConfig.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx)
        throws ServletException {
    Servlet s = new CustomDefaultServlet();
    ServletRegistration.Dynamic r = ctx.addServlet(servletName, s);
    r.addMapping("/");
}
 
Example 13
Source File: AppInitializer.java    From chronos with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onStartup(ServletContext container) {
  AnnotationConfigWebApplicationContext rootContext =
    new AnnotationConfigWebApplicationContext();
  rootContext.register(TestConfig.class);
  rootContext.registerShutdownHook();
  container.addListener(new ContextLoaderListener(rootContext));

  ServletRegistration.Dynamic dispatcher =
    container.addServlet("chronos-dispatcher", new DispatcherServlet(rootContext));
  dispatcher.setLoadOnStartup(1);
  dispatcher.addMapping("/swagger-ui.html");
  dispatcher.addMapping("/*");
}
 
Example 14
Source File: TestStandardContext.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx)
        throws ServletException {
    // Register and map servlet
    s = new Bug51376Servlet();
    ServletRegistration.Dynamic sr = ctx.addServlet("bug51376", s);
    sr.addMapping("/bug51376");
    if (loadOnStartUp) {
        sr.setLoadOnStartup(1);
    }
}
 
Example 15
Source File: SpringWebInitializer.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
private void addDispatcherContext(ServletContext container) {
  // Create the dispatcher servlet's Spring application context
  AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
  dispatcherContext.register(SpringDispatcherConfig.class); 
	 
  // Declare  <servlet> and <servlet-mapping> for the DispatcherServlet
  ServletRegistration.Dynamic dispatcher = container.addServlet("ch02-servlet", 
  		new DispatcherServlet(dispatcherContext));
  dispatcher.addMapping("*.html");
  dispatcher.setLoadOnStartup(1);
}
 
Example 16
Source File: AppInitializer.java    From FRC-2018-Public with MIT License 5 votes vote down vote up
public void onStartup(ServletContext container) throws ServletException {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(AppConfig.class);
    ctx.setServletContext(container);

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

    servlet.setLoadOnStartup(1);
    servlet.addMapping("/");
}
 
Example 17
Source File: WebApplicationConfiguration.java    From junit-servers with MIT License 5 votes vote down vote up
private void initSpringMvc(ServletContext servletContext, AnnotationConfigWebApplicationContext context) {
	DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
	dispatcherServlet.setContextConfigLocation(configLocation());
	dispatcherServlet.setContextClass(AnnotationConfigWebApplicationContext.class);

	ServletRegistration.Dynamic dispatcher = servletContext.addServlet("spring", dispatcherServlet);
	dispatcher.setLoadOnStartup(1);
	dispatcher.addMapping("/*");
	dispatcher.addMapping("/");
}
 
Example 18
Source File: WebAppInitializer.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
@Override
public void onStartup(final ServletContext servletContext)
        throws ServletException {

    // Register DispatcherServlet
    super.onStartup(servletContext);

    // Register H2 Admin console:
    ServletRegistration.Dynamic h2WebServlet = servletContext.addServlet("h2WebServlet",
            "org.h2.server.web.WebServlet");
    h2WebServlet.addMapping("/admin/h2/*");
    h2WebServlet.setInitParameter("webAllowOthers", "true");

}
 
Example 19
Source File: TestContextConfig.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx)
        throws ServletException {
    Servlet s = new CustomDefaultServlet();
    ServletRegistration.Dynamic r = ctx.addServlet(servletName, s);
    r.addMapping("/");
}
 
Example 20
Source File: TesterServletContainerInitializer2.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx)
        throws ServletException {
    Servlet s = new TesterServlet();
    ServletRegistration.Dynamic r = ctx.addServlet("TesterServlet2", s);
    r.addMapping("/TesterServlet2");
}