javax.servlet.ServletContext.addServlet()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(15.2k)|赞(0)|评价(0)|浏览(245)

本文整理了Java中javax.servlet.ServletContext.addServlet()方法的一些代码示例,展示了ServletContext.addServlet()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ServletContext.addServlet()方法的具体详情如下:
包路径:javax.servlet.ServletContext
类名称:ServletContext
方法名:addServlet

ServletContext.addServlet介绍

[英]Add servlet to context.
[中]将servlet添加到上下文中。

代码示例

代码示例来源:origin: stagemonitor/stagemonitor

@Override
  public void onStartup(ServletContext ctx) {
    if (ServletContainerInitializerUtil.avoidDoubleInit(this, ctx)) return;
    final AlertingPlugin alertingPlugin = Stagemonitor.getPlugin(AlertingPlugin.class);
    ctx.addServlet(AlerterTypeServlet.class.getSimpleName(), new AlerterTypeServlet(alertingPlugin, Stagemonitor.getMeasurementSession()))
        .addMapping("/stagemonitor/alerter-types");
    ctx.addServlet(IncidentServlet.class.getSimpleName(), new IncidentServlet(alertingPlugin))
        .addMapping("/stagemonitor/incidents");
    ctx.addServlet(TestAlertSenderServlet.class.getSimpleName(), new TestAlertSenderServlet())
        .addMapping("/stagemonitor/test-alert");
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
  String servletName = getServletName();
  Assert.hasLength(servletName, "getServletName() must not return null or empty");
  ApplicationContext applicationContext = createApplicationContext();
  Assert.notNull(applicationContext, "createApplicationContext() must not return null");
  refreshApplicationContext(applicationContext);
  registerCloseListener(servletContext, applicationContext);
  HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(applicationContext).build();
  ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
  ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, servlet);
  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(getServletMapping());
  registration.setAsyncSupported(true);
}

代码示例来源:origin: spring-projects/spring-framework

dispatcherServlet.setContextInitializers(getServletApplicationContextInitializers());
ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet);
if (registration == null) {
  throw new IllegalStateException("Failed to register servlet with name '" + servletName + "'. " +
registration.setLoadOnStartup(1);
registration.addMapping(getServletMappings());
registration.setAsyncSupported(isAsyncSupported());

代码示例来源:origin: jersey/jersey

/**
 * Add new servlet according to {@link Application} subclass with {@link ApplicationPath} annotation or existing
 * {@code servlet-mapping}.
 */
private static void addServletWithApplication(final ServletContext context,
                       final Class<? extends Application> clazz,
                       final Set<Class<?>> defaultClasses) throws ServletException {
  final ApplicationPath ap = clazz.getAnnotation(ApplicationPath.class);
  if (ap != null) {
    // App is annotated with ApplicationPath
    final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(clazz, defaultClasses)
        .addProperties(Utils.getContextParams(context));
    final ServletContainer s = new ServletContainer(resourceConfig);
    final ServletRegistration.Dynamic dsr = context.addServlet(clazz.getName(), s);
    dsr.setAsyncSupported(true);
    dsr.setLoadOnStartup(1);
    final String mapping = createMappingPath(ap);
    if (!mappingExists(context, mapping)) {
      dsr.addMapping(mapping);
      LOGGER.log(Level.CONFIG, LocalizationMessages.JERSEY_APP_REGISTERED_MAPPING(clazz.getName(), mapping));
    } else {
      LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_MAPPING_CONFLICT(clazz.getName(), mapping));
    }
  }
}

代码示例来源:origin: jersey/jersey

/**
 * Add new servlet according to {@link Application} subclass with {@link ApplicationPath} annotation or existing
 * {@code servlet-mapping}.
 */
private static void addServletWithApplication(final ServletContext context,
                       final Class<? extends Application> clazz,
                       final Set<Class<?>> defaultClasses) throws ServletException {
  final ApplicationPath ap = clazz.getAnnotation(ApplicationPath.class);
  if (ap != null) {
    // App is annotated with ApplicationPath
    final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(clazz, defaultClasses)
        .addProperties(Utils.getContextParams(context));
    final ServletContainer s = new ServletContainer(resourceConfig);
    final ServletRegistration.Dynamic dsr = context.addServlet(clazz.getName(), s);
    dsr.setAsyncSupported(true);
    dsr.setLoadOnStartup(1);
    final String mapping = createMappingPath(ap);
    if (!mappingExists(context, mapping)) {
      dsr.addMapping(mapping);
      LOGGER.log(Level.CONFIG, LocalizationMessages.JERSEY_APP_REGISTERED_MAPPING(clazz.getName(), mapping));
    } else {
      LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_MAPPING_CONFLICT(clazz.getName(), mapping));
    }
  }
}

代码示例来源:origin: stagemonitor/stagemonitor

Stagemonitor.init();
if (ServletContainerInitializerUtil.avoidDoubleInit(this, ctx)) return;
ctx.addServlet(ConfigurationServlet.class.getSimpleName(), new ConfigurationServlet())
    .addMapping(ConfigurationServlet.CONFIGURATION_ENDPOINT);
ctx.addServlet(StagemonitorMetricsServlet.class.getSimpleName(), new StagemonitorMetricsServlet())
    .addMapping("/stagemonitor/metrics");
ctx.addServlet(ClientSpanServlet.class.getSimpleName(), new ClientSpanServlet())
    .addMapping("/stagemonitor/public/eum");
final ClientSpanJavaScriptServlet servlet = new ClientSpanJavaScriptServlet();
Stagemonitor.getPlugin(ServletPlugin.class).setClientSpanJavaScriptServlet(servlet);
ctx.addServlet(ClientSpanJavaScriptServlet.class.getSimpleName(), servlet)
    .addMapping("/stagemonitor/public/eum.js");
ctx.addServlet(StagemonitorFileServlet.class.getSimpleName(), new StagemonitorFileServlet())
    .addMapping("/stagemonitor/static/*", "/stagemonitor/public/static/*");
ctx.addServlet(WidgetServlet.class.getSimpleName(), new WidgetServlet())
    .addMapping("/stagemonitor");
final ServletRegistration.Dynamic spanServlet = ctx.addServlet(SpanServlet.class.getSimpleName(), new SpanServlet());
spanServlet.addMapping("/stagemonitor/spans");
spanServlet.setAsyncSupported(true);
final ServletRegistration.Dynamic healthServlet = ctx.addServlet(HealthCheckServlet.class.getSimpleName(), new HealthCheckServlet(Stagemonitor.getHealthCheckRegistry()));
healthServlet.addMapping("/stagemonitor/status");
healthServlet.setAsyncSupported(true);

代码示例来源:origin: stackoverflow.com

public class MyWebAppInitializer implements WebApplicationInitializer {

 @Override
 public void onStartup(ServletContext container) {
 // Create the 'root' Spring application context
 AnnotationConfigWebApplicationContext rootContext =
            new AnnotationConfigWebApplicationContext();
 rootContext.register(WebSpringConfig.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);
  dispatcher.addMapping("/");
 }
}

代码示例来源:origin: aol/micro-server

private void handleServlet(ServletConfiguration servlet,ServletContext webappContext){
  servlet.getServlet().fold(clazz-> {
    setInitParameters(webappContext.addServlet(getName(servlet),
          clazz), servlet)
      .addMapping(servlet.getMapping());
    return 1; 
    }, obj-> {
      ServletRegistration.Dynamic servletReg = webappContext.addServlet(
          servlet.getName(), obj);
      servletReg.addMapping(servlet.getMapping());
            return 2;
          });
}
private void addAutoDiscoveredServlets(ServletContext webappContext) {

代码示例来源:origin: aol/micro-server

private void addExplicitlyDeclaredServlets(ServletContext webappContext) {
  for (ServletData servletData : servletData) {
    ServletRegistration.Dynamic servletReg = webappContext.addServlet(
        servletData.getServletName(), servletData.getServlet());
    servletReg.addMapping(servletData.getMapping());
    logServlet(servletData);
  }
}

代码示例来源:origin: org.springframework/spring-web

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
  String servletName = getServletName();
  Assert.hasLength(servletName, "getServletName() must not return null or empty");
  ApplicationContext applicationContext = createApplicationContext();
  Assert.notNull(applicationContext, "createApplicationContext() must not return null");
  refreshApplicationContext(applicationContext);
  registerCloseListener(servletContext, applicationContext);
  HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(applicationContext).build();
  ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
  ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, servlet);
  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(getServletMapping());
  registration.setAsyncSupported(true);
}

代码示例来源:origin: jersey/jersey

final ServletRegistration.Dynamic dynamicRegistration = context.addServlet(clazz.getName(), servlet);
dynamicRegistration.setAsyncSupported(true);
dynamicRegistration.setLoadOnStartup(1);
registration = dynamicRegistration;

代码示例来源:origin: jersey/jersey

final ServletRegistration.Dynamic dynamicRegistration = context.addServlet(clazz.getName(), servlet);
dynamicRegistration.setAsyncSupported(true);
dynamicRegistration.setLoadOnStartup(1);
registration = dynamicRegistration;

代码示例来源:origin: org.springframework/spring-webmvc

dispatcherServlet.setContextInitializers(getServletApplicationContextInitializers());
ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet);
if (registration == null) {
  throw new IllegalStateException("Failed to register servlet with name '" + servletName + "'. " +
registration.setLoadOnStartup(1);
registration.addMapping(getServletMappings());
registration.setAsyncSupported(isAsyncSupported());

代码示例来源:origin: jersey/jersey

/**
 * Enhance default servlet (named {@link Application}) configuration.
 */
private static void addServletWithDefaultConfiguration(final ServletContext context,
                            final Set<Class<?>> classes) throws ServletException {
  ServletRegistration registration = context.getServletRegistration(Application.class.getName());
  if (registration != null) {
    final Set<Class<?>> appClasses = getRootResourceAndProviderClasses(classes);
    final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(ResourceConfig.class, appClasses)
        .addProperties(getInitParams(registration))
        .addProperties(Utils.getContextParams(context));
    if (registration.getClassName() != null) {
      // class name present - complete servlet registration from container point of view
      Utils.store(resourceConfig, context, registration.getName());
    } else {
      // no class name - no complete servlet registration from container point of view
      final ServletContainer servlet = new ServletContainer(resourceConfig);
      registration = context.addServlet(registration.getName(), servlet);
      ((ServletRegistration.Dynamic) registration).setLoadOnStartup(1);
      if (registration.getMappings().isEmpty()) {
        // Error
        LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_NO_MAPPING(registration.getName()));
      } else {
        LOGGER.log(Level.CONFIG,
            LocalizationMessages.JERSEY_APP_REGISTERED_CLASSES(registration.getName(), appClasses));
      }
    }
  }
}

代码示例来源:origin: jersey/jersey

/**
 * Enhance default servlet (named {@link Application}) configuration.
 */
private static void addServletWithDefaultConfiguration(final ServletContext context,
                            final Set<Class<?>> classes) throws ServletException {
  ServletRegistration registration = context.getServletRegistration(Application.class.getName());
  if (registration != null) {
    final Set<Class<?>> appClasses = getRootResourceAndProviderClasses(classes);
    final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(ResourceConfig.class, appClasses)
        .addProperties(getInitParams(registration))
        .addProperties(Utils.getContextParams(context));
    if (registration.getClassName() != null) {
      // class name present - complete servlet registration from container point of view
      Utils.store(resourceConfig, context, registration.getName());
    } else {
      // no class name - no complete servlet registration from container point of view
      final ServletContainer servlet = new ServletContainer(resourceConfig);
      registration = context.addServlet(registration.getName(), servlet);
      ((ServletRegistration.Dynamic) registration).setLoadOnStartup(1);
      if (registration.getMappings().isEmpty()) {
        // Error
        LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_NO_MAPPING(registration.getName()));
      } else {
        LOGGER.log(Level.CONFIG,
            LocalizationMessages.JERSEY_APP_REGISTERED_CLASSES(registration.getName(), appClasses));
      }
    }
  }
}

代码示例来源:origin: aol/micro-server

javax.servlet.ServletRegistration.Dynamic servletRegistration = webappContext.addServlet(config.getName() + "-"
    + serverData.getModule()
          .getContext(), config.getServlet());
servletRegistration.setInitParameter("context", serverData.getModule()
                             .getContext());
servletRegistration.setLoadOnStartup(1);
servletRegistration.addMapping(serverData.getBaseUrlPattern());

代码示例来源:origin: apache/servicecomb-java-chassis

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;
}

代码示例来源:origin: resteasy/Resteasy

ServletRegistration.Dynamic reg = servletContext.addServlet(applicationClass.getName(), HttpServlet30Dispatcher.class);
reg.setLoadOnStartup(1);
reg.setAsyncSupported(true);
reg.setInitParameter("javax.ws.rs.Application", applicationClass.getName());
  reg.addMapping(mapping);

代码示例来源:origin: org.springframework.boot/spring-boot-actuator

private void register(ServletContext servletContext,
    ExposableServletEndpoint endpoint) {
  String name = endpoint.getEndpointId().toLowerCaseString() + "-actuator-endpoint";
  String path = this.basePath + "/" + endpoint.getRootPath();
  String urlMapping = path.endsWith("/") ? path + "*" : path + "/*";
  EndpointServlet endpointServlet = endpoint.getEndpointServlet();
  Dynamic registration = servletContext.addServlet(name,
      endpointServlet.getServlet());
  registration.addMapping(urlMapping);
  registration.setInitParameters(endpointServlet.getInitParameters());
  logger.info("Registered '" + path + "' to " + name);
}

代码示例来源:origin: psi-probe/psi-probe

ServletRegistration.Dynamic probe = servletContext.addServlet("probe", ProbeServlet.class);
probe.setInitParameters(initParameters);
probe.setLoadOnStartup(0);
probe.addMapping("*.htm");
probe.addMapping("*.ajax");
probe.addMapping("/logs/*");
probe.addMapping("/chart.png");

相关文章

微信公众号

最新文章

更多

ServletContext类方法