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

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

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

ServletContext.removeAttribute介绍

[英]Removes the attribute with the given name from the servlet context. After removal, subsequent calls to #getAttribute to retrieve the attribute's value will return null.

If listeners are configured on the ServletContext the container notifies them accordingly.
[中]从servlet上下文中删除具有给定名称的属性。删除后,后续调用#getAttribute以检索属性值将返回[$0$]。
如果在ServletContext上配置了侦听器,容器会相应地通知它们。

代码示例

代码示例来源:origin: igniterealtime/Openfire

@Override
public void removeAttribute( String s )
{
  proxy.removeAttribute( s );
}

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

@Override
@Nullable
public Object remove(String name) {
  Object scopedObject = this.servletContext.getAttribute(name);
  if (scopedObject != null) {
    this.servletContext.removeAttribute(name);
    this.destructionCallbacks.remove(name);
    return scopedObject;
  }
  else {
    return null;
  }
}

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

@Override
public void removeAttribute(String name, int scope) {
  Assert.notNull(name, "Attribute name must not be null");
  switch (scope) {
    case PAGE_SCOPE:
      this.attributes.remove(name);
      break;
    case REQUEST_SCOPE:
      this.request.removeAttribute(name);
      break;
    case SESSION_SCOPE:
      this.request.getSession().removeAttribute(name);
      break;
    case APPLICATION_SCOPE:
      this.servletContext.removeAttribute(name);
      break;
    default:
      throw new IllegalArgumentException("Invalid scope: " + scope);
  }
}

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

/**
 * Load {@link org.glassfish.jersey.server.ResourceConfig resource config} from given
 * {@link javax.servlet.ServletContext servlet context}. If found then the resource config is also removed from servlet
 * context. The {@code configName} is used as an attribute name suffix.
 *
 * @param context servlet context to load resource config from.
 * @param configName name or id of the resource config.
 * @return previously stored resource config or {@code null} if no resource config has been stored.
 */
public static ResourceConfig retrieve(final ServletContext context, final String configName) {
  final String attributeName = RESOURCE_CONFIG + "_" + configName;
  final ResourceConfig config = (ResourceConfig) context.getAttribute(attributeName);
  context.removeAttribute(attributeName);
  return config;
}

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

/**
 * Load {@link org.glassfish.jersey.server.ResourceConfig resource config} from given
 * {@link javax.servlet.ServletContext servlet context}. If found then the resource config is also removed from servlet
 * context. The {@code configName} is used as an attribute name suffix.
 *
 * @param context servlet context to load resource config from.
 * @param configName name or id of the resource config.
 * @return previously stored resource config or {@code null} if no resource config has been stored.
 */
public static ResourceConfig retrieve(final ServletContext context, final String configName) {
  final String attributeName = RESOURCE_CONFIG + "_" + configName;
  final ResourceConfig config = (ResourceConfig) context.getAttribute(attributeName);
  context.removeAttribute(attributeName);
  return config;
}

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

@Override
@Nullable
public Object remove(String name) {
  Object scopedObject = this.servletContext.getAttribute(name);
  if (scopedObject != null) {
    this.servletContext.removeAttribute(name);
    this.destructionCallbacks.remove(name);
    return scopedObject;
  }
  else {
    return null;
  }
}

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

/**
 * Close Spring's web application context for the given servlet context.
 * <p>If overriding {@link #loadParentContext(ServletContext)}, you may have
 * to override this method as well.
 * @param servletContext the ServletContext that the WebApplicationContext runs in
 */
public void closeWebApplicationContext(ServletContext servletContext) {
  servletContext.log("Closing Spring root WebApplicationContext");
  try {
    if (this.context instanceof ConfigurableWebApplicationContext) {
      ((ConfigurableWebApplicationContext) this.context).close();
    }
  }
  finally {
    ClassLoader ccl = Thread.currentThread().getContextClassLoader();
    if (ccl == ContextLoader.class.getClassLoader()) {
      currentContext = null;
    }
    else if (ccl != null) {
      currentContextPerThread.remove(ccl);
    }
    servletContext.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
  }
}

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

public void contextDestroyed(ServletContextEvent event) {
  logger.info("Calling application shutdown...");
  VoldemortServer server = (VoldemortServer) event.getServletContext()
                          .getAttribute(SERVER_KEY);
  if(server != null)
    server.stop();
  logger.info("Destroying application...");
  event.getServletContext().removeAttribute(SERVER_KEY);
  event.getServletContext().removeAttribute(SERVER_CONFIG_KEY);
  event.getServletContext().removeAttribute(VELOCITY_ENGINE_KEY);
}

代码示例来源:origin: Netflix/eureka

/**
 * Handles Eureka cleanup, including shutting down all monitors and yielding all EIPs.
 *
 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
 */
@Override
public void contextDestroyed(ServletContextEvent event) {
  try {
    logger.info("{} Shutting down Eureka Server..", new Date());
    ServletContext sc = event.getServletContext();
    sc.removeAttribute(EurekaServerContext.class.getName());
    destroyEurekaServerContext();
    destroyEurekaEnvironment();
  } catch (Throwable e) {
    logger.error("Error shutting down eureka", e);
  }
  logger.info("{} Eureka Service is now shutdown...", new Date());
}

代码示例来源:origin: apache/shiro

protected void removeContextAttribute(String key) {
  getRequiredServletContext().removeAttribute(key);
}

代码示例来源:origin: OryxProject/oryx

@Override
public void contextDestroyed(ServletContextEvent sce) {
 log.info("ModelManagerListener destroying");
 // Slightly paranoid; remove objects from app scope manually
 ServletContext context = sce.getServletContext();
 for (Enumeration<String> names = context.getAttributeNames(); names.hasMoreElements();) {
  context.removeAttribute(names.nextElement());
 }
 close();
 // Hacky, but prevents Tomcat from complaining that ZK's cleanup thread 'leaked' since
 // it has a short sleep at its end
 try {
  Thread.sleep(1000);
 } catch (InterruptedException ie) {
  // continue
 }
}

代码示例来源:origin: oblac/jodd

@Override
  public void doTag() throws JspException {
    PageContext pageContext = (PageContext) getJspContext();
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();

    String scopeValue = scope != null ? scope.toLowerCase() : SCOPE_PAGE;
    if (scopeValue.equals(SCOPE_APPLICATION)) {
      request.getServletContext().removeAttribute(name);
    } else if (scopeValue.equals(SCOPE_SESSION)) {
      request.getSession().removeAttribute(name);
    } else if (scopeValue.equals(SCOPE_REQUEST)) {
      request.removeAttribute(name);
    } else if (scopeValue.equals(SCOPE_PAGE)) {
      pageContext.removeAttribute(name);
    } else {
      throw new JspException("Invalid scope: " + scope);
    }
  }
}

代码示例来源:origin: oblac/jodd

/**
 * Removes scope attribute.
 */
public static void removeScopeAttribute(final String name, final String scope, final PageContext pageContext) {
  HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
  String scopeValue = scope != null ? scope.toLowerCase() : SCOPE_PAGE;
  if (scopeValue.equals(SCOPE_PAGE)) {
    pageContext.removeAttribute(name);
  }
  else if (scopeValue.equals(SCOPE_REQUEST)) {
    request.removeAttribute(name);
  }
  else if (scopeValue.equals(SCOPE_SESSION)) {
    request.getSession().removeAttribute(name);
  }
  else if (scopeValue.equals(SCOPE_APPLICATION)) {
    request.getServletContext().removeAttribute(name);
  }
  else {
    throw new IllegalArgumentException("Invalid scope: " + scope);
  }
}

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

/**
 * Close Spring's web application context for the given servlet context.
 * <p>If overriding {@link #loadParentContext(ServletContext)}, you may have
 * to override this method as well.
 * @param servletContext the ServletContext that the WebApplicationContext runs in
 */
public void closeWebApplicationContext(ServletContext servletContext) {
  servletContext.log("Closing Spring root WebApplicationContext");
  try {
    if (this.context instanceof ConfigurableWebApplicationContext) {
      ((ConfigurableWebApplicationContext) this.context).close();
    }
  }
  finally {
    ClassLoader ccl = Thread.currentThread().getContextClassLoader();
    if (ccl == ContextLoader.class.getClassLoader()) {
      currentContext = null;
    }
    else if (ccl != null) {
      currentContextPerThread.remove(ccl);
    }
    servletContext.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
  }
}

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

@Override
public void removeAttribute(String name, int scope) {
  Assert.notNull(name, "Attribute name must not be null");
  switch (scope) {
    case PAGE_SCOPE:
      this.attributes.remove(name);
      break;
    case REQUEST_SCOPE:
      this.request.removeAttribute(name);
      break;
    case SESSION_SCOPE:
      this.request.getSession().removeAttribute(name);
      break;
    case APPLICATION_SCOPE:
      this.servletContext.removeAttribute(name);
      break;
    default:
      throw new IllegalArgumentException("Invalid scope: " + scope);
  }
}

代码示例来源:origin: oblac/jodd

/**
 * Stops <em>Madvoc</em> web application.
 */
public void stopWebApplication() {
  log.info("Madvoc shutting down...");
  if (servletContext != null) {
    servletContext.removeAttribute(MADVOC_ATTR);
  }
  webapp.shutdown();
  webapp = null;
}

代码示例来源:origin: apache/shiro

/**
 * Destroys the {@link WebEnvironment} for the given servlet context.
 *
 * @param servletContext the ServletContext attributed to the WebSecurityManager
 */
public void destroyEnvironment(ServletContext servletContext) {
  servletContext.log("Cleaning up Shiro Environment");
  try {
    Object environment = servletContext.getAttribute(ENVIRONMENT_ATTRIBUTE_KEY);
    if (environment instanceof WebEnvironment) {
      finalizeEnvironment((WebEnvironment) environment);
    }
    LifecycleUtils.destroy(environment);
  } finally {
    servletContext.removeAttribute(ENVIRONMENT_ATTRIBUTE_KEY);
  }
}

代码示例来源:origin: org.freemarker/freemarker

@Override
public void removeAttribute(String name, int scope) {
  switch(scope) {
    case PAGE_SCOPE: {
      environment.getGlobalNamespace().remove(name);
      break;
    }
    case REQUEST_SCOPE: {
      getRequest().removeAttribute(name);
      break;
    }
    case SESSION_SCOPE: {
      HttpSession session = getSession(false);
      if (session != null) {
        session.removeAttribute(name);
      }
      break;
    }
    case APPLICATION_SCOPE: {
      getServletContext().removeAttribute(name);
      break;
    }
    default: {
      throw new IllegalArgumentException("Invalid scope: " + scope);
    }
  }
}

代码示例来源:origin: Atmosphere/atmosphere

@Override
public void configure(final AtmosphereConfig config) {
  ServletContext sc = config.framework().getServletContext();
  Map<Class<? extends Annotation>, Set<Class<?>>>  annotations= (Map<Class<? extends Annotation>, Set<Class<?>>>) sc.getAttribute(ANNOTATION_ATTRIBUTE);
  sc.removeAttribute(ANNOTATION_ATTRIBUTE);
  boolean useByteCodeProcessor = config.getInitParameter(ApplicationConfig.BYTECODE_PROCESSOR, false);
  boolean scanForAtmosphereAnnotation = false;
  if (useByteCodeProcessor || annotations == null || annotations.isEmpty()) {
    delegate = new BytecodeBasedAnnotationProcessor(handler);
    scanForAtmosphereAnnotation = true;
  } else {
    Map<Class<? extends Annotation>, Set<Class<?>>> clone = new HashMap<Class<? extends Annotation>, Set<Class<?>>>();
    clone.putAll(annotations);
    delegate = new ServletContainerInitializerAnnotationProcessor(handler, clone, config.framework());
  }
  logger.info("AnnotationProcessor {} being used", delegate.getClass());
  if (scanForAtmosphereAnnotation) {
    scanForAnnotation(config.framework());
  }
  delegate.configure(config.framework().getAtmosphereConfig());
}

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

@Test
public void addServerEndpointConfigBeanWithExplicitServerContainer() throws Exception {
  ServerEndpointRegistration endpointRegistration = new ServerEndpointRegistration("/dummy", new DummyEndpoint());
  this.webAppContext.getBeanFactory().registerSingleton("dummyEndpoint", endpointRegistration);
  this.servletContext.removeAttribute("javax.websocket.server.ServerContainer");
  this.exporter.setServerContainer(this.serverContainer);
  this.exporter.setApplicationContext(this.webAppContext);
  this.exporter.afterPropertiesSet();
  this.exporter.afterSingletonsInstantiated();
  verify(this.serverContainer).addEndpoint(endpointRegistration);
}

相关文章

微信公众号

最新文章

更多

ServletContext类方法