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

x33g5p2x  于2022-01-19 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(218)

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

HttpSession.removeAttribute介绍

[英]Removes the object bound with the specified name from this session. If the session does not have an object bound with the specified name, this method does nothing.

After this method executes, and if the object implements HttpSessionBindingListener, the container calls HttpSessionBindingListener.valueUnbound. The container then notifies any HttpSessionAttributeListeners in the web application.
[中]从此会话中删除与指定名称绑定的对象。如果会话没有使用指定名称绑定对象,则此方法不执行任何操作。
执行此方法后,如果对象实现HttpSessionBindingListener,容器将调用HttpSessionBindingListener.valueUnbound。然后容器通知web应用程序中的任何HttpSessionAttributeListener

代码示例

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

@SuppressWarnings("unchecked")
Flash(HttpSession session) {
  this.session = session;
  this.value = (T) session.getAttribute(ATTRIBUTE);
  if (this.value != null) {
    session.removeAttribute(ATTRIBUTE);
  }
}

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

public static void removeAuthenticationError(HttpServletRequest request) {
  request.getSession().removeAttribute(AUTHENTICATION_ERROR);
}

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

/**
 * Set the session attribute with the given name to the given value.
 * Removes the session attribute if value is null, if a session existed at all.
 * Does not create a new session if not necessary!
 * @param request current HTTP request
 * @param name the name of the session attribute
 * @param value the value of the session attribute
 */
public static void setSessionAttribute(HttpServletRequest request, String name, @Nullable Object value) {
  Assert.notNull(request, "Request must not be null");
  if (value != null) {
    request.getSession().setAttribute(name, value);
  }
  else {
    HttpSession session = request.getSession(false);
    if (session != null) {
      session.removeAttribute(name);
    }
  }
}

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

@Override
  public void doFilter(HttpServletRequest wrappedRequest) {
    assertThat(wrappedRequest.getSession().getAttribute(ATTR))
        .isEqualTo(VALUE);
    wrappedRequest.getSession().removeAttribute(ATTR);
    assertThat(wrappedRequest.getSession().getAttribute(ATTR)).isNull();
  }
});

代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server

/**
 * Set the timestamp on the session to mark when the authentication happened,
 * useful for calculating authentication age. This gets stored in the sesion
 * and can get pulled out by other components.
 */
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
  Date authTimestamp = new Date();
  HttpSession session = request.getSession();
  session.setAttribute(AUTH_TIMESTAMP, authTimestamp);
  if (session.getAttribute(AuthorizationRequestFilter.PROMPT_REQUESTED) != null) {
    session.setAttribute(AuthorizationRequestFilter.PROMPTED, Boolean.TRUE);
    session.removeAttribute(AuthorizationRequestFilter.PROMPT_REQUESTED);
  }
  logger.info("Successful Authentication of " + authentication.getName() + " at " + authTimestamp.toString());
  super.onAuthenticationSuccess(request, response, authentication);
}

代码示例来源:origin: jenkinsci/jenkins

@Override
  public boolean toggleCollapsed(String paneId) {
    final HttpSession session = Stapler.getCurrentRequest().getSession();
    final String property = format(attribute, paneId);
    final Object collapsed = session.getAttribute(property);
    if (collapsed == null) {
      session.setAttribute(property, true);
      return true;
    }
    session.removeAttribute(property);
    return false;
  }
}

代码示例来源:origin: Red5/red5-server

/** {@inheritDoc} */
public boolean setAttribute(String name, Object value) {
  if (name == null) {
    return false;
  }
  if (value == null) {
    session.removeAttribute(name);
  } else {
    session.setAttribute(name, value);
  }
  return true;
}

代码示例来源:origin: magro/memcached-session-manager

final HttpSession session = request.getSession(false);
LOG.info( "Invalidating session " + session.getId() );
session.invalidate();
final HttpSession session = request.getSession();
  LOG.info("Removing " + (keys.length > 1 ? "keys " : "key ") + Arrays.asList(keys));
  for (final String key : keys) {
    session.removeAttribute(key);
while ( attributeNames.hasMoreElements() ) {
  final String name = attributeNames.nextElement().toString();
  final Object value = session.getAttribute( name );
  out.println( name + "=" + value );

代码示例来源:origin: org.thymeleaf/thymeleaf-testing

static final HttpSession createMockHttpSession(final ServletContext context, final Map<String, Object> attributes) {
  
  final HttpSession session = Mockito.mock(HttpSession.class);
  
  Mockito.when(session.getServletContext()).thenReturn(context);
  Mockito.when(session.getAttributeNames()).thenAnswer(new GetVariableNamesAnswer(attributes));
  Mockito.when(session.getAttribute(Matchers.anyString())).thenAnswer(new GetAttributeAnswer(attributes));
  Mockito.doAnswer(new SetAttributeAnswer(attributes)).when(session).setAttribute(Matchers.anyString(), Matchers.anyObject());
  Mockito.doAnswer(new RemoveAttributeAnswer(attributes)).when(session).removeAttribute(Matchers.anyString());
  return session;
  
}

代码示例来源:origin: nutzam/nutz

public void clear() {
  synchronized (session) {
    Enumeration<String> ems = session.getAttributeNames();
    List<String> keys = new ArrayList<String>();
    while (ems.hasMoreElements()) {
      String key = ems.nextElement();
      if (null == key)
        continue;
      Object value = session.getAttribute(key);
      if (value instanceof ObjectProxy) {
        keys.add(key);
        ((ObjectProxy) value).depose();
      }
    }
    for (String key : keys) {
      session.removeAttribute(key);
    }
  }
}

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

DiscoveryInformation discovered = (DiscoveryInformation) request.getSession()
    .getAttribute(DISCOVERY_INFO_KEY);
    .getSession().getAttribute(ATTRIBUTE_LIST_KEY);
request.getSession().removeAttribute(DISCOVERY_INFO_KEY);
request.getSession().removeAttribute(ATTRIBUTE_LIST_KEY);

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

public static void setUserId(HttpServletRequest request, Long id) {
  if (id == null || id == NOT_PERSISTED) {
    request.getSession().removeAttribute(CURRENT_USER_ID);
  } else {
    request.getSession().setAttribute(CURRENT_USER_ID, id);
  }
}

代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server

HttpSession session = request.getSession();
    session.setAttribute(LOGIN_HINT, loginHint);
  } else {
    session.removeAttribute(LOGIN_HINT);
      if (session.getAttribute(PROMPTED) == null) {
        session.setAttribute(PROMPT_REQUESTED, Boolean.TRUE);
        session.removeAttribute(PROMPTED);
        chain.doFilter(req, res);
      Date authTime = (Date) session.getAttribute(AuthenticationTimeStamper.AUTH_TIMESTAMP);

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

/**
 * Remove Object in session.
 * @param key a String specifying the key of the Object stored in session
 */
public Controller removeSessionAttr(String key) {
  HttpSession session = request.getSession(false);
  if (session != null)
    session.removeAttribute(key);
  return this;
}

代码示例来源:origin: Red5/red5-server

/**
 * Cleans up the remoting connection client from the HttpSession and client registry. This should also fix APPSERVER-328
 */
public void cleanup() {
  if (session != null) {
    RemotingClient rc = (RemotingClient) session.getAttribute(CLIENT);
    session.removeAttribute(CLIENT);
    if (rc != null) {
      rc.unregister(this);
    }
  }
}

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

HttpSession session = req.getSession(true);
 try{
   if(null != (session.getAttribute("error"))){
     session.removeAttribute("error");
   }else{
     session.setAttribute( "error", "Invalid username or password");
   }
 }catch(Exception er){
   er.printStackTrace();
 }
 RequestDispatcher dispatcher = req.getRequestDispatcher("/jsp/LoginTest.jsp");
 dispatcher.forward( req, res);

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

public void touch() throws InvalidSessionException {
  //just manipulate the session to update the access time:
  try {
    httpSession.setAttribute(TOUCH_OBJECT_SESSION_KEY, TOUCH_OBJECT_SESSION_KEY);
    httpSession.removeAttribute(TOUCH_OBJECT_SESSION_KEY);
  } catch (Exception e) {
    throw new InvalidSessionException(e);
  }
}

代码示例来源:origin: thymeleaf/thymeleaf-testing

static final HttpSession createMockHttpSession(final ServletContext context, final Map<String, Object> attributes) {
  
  final HttpSession session = Mockito.mock(HttpSession.class);
  
  Mockito.when(session.getServletContext()).thenReturn(context);
  Mockito.when(session.getAttributeNames()).thenAnswer(new GetVariableNamesAnswer(attributes));
  Mockito.when(session.getAttribute(Matchers.anyString())).thenAnswer(new GetAttributeAnswer(attributes));
  Mockito.doAnswer(new SetAttributeAnswer(attributes)).when(session).setAttribute(Matchers.anyString(), Matchers.anyObject());
  Mockito.doAnswer(new RemoveAttributeAnswer(attributes)).when(session).removeAttribute(Matchers.anyString());
  return session;
  
}

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

response.setTimeout(session.getMaxInactiveInterval());
final String user_UUID = (String)session.getAttribute("user_UUID");
response.setUserId(user_UUID);
final String domain_UUID = (String)session.getAttribute("domain_UUID");
response.setDomainId(domain_UUID);
  session.removeAttribute("user_UUID");
  session.removeAttribute("domain_UUID");
  while (attrNames.hasMoreElements()) {
    final String attrName = (String) attrNames.nextElement();
    final Object attrObj = session.getAttribute(attrName);
    if (ApiConstants.USERNAME.equalsIgnoreCase(attrName)) {
      response.setUsername(attrObj.toString());

代码示例来源:origin: com.liferay.portal/com.liferay.portal.kernel

protected static void setXXSSProtection(
  HttpServletRequest request, HttpServletResponse response) {
  HttpSession session = request.getSession(false);
  if ((session != null) &&
    (session.getAttribute(_DISABLE_XSS_AUDITOR) != null)) {
    session.removeAttribute(_DISABLE_XSS_AUDITOR);
    response.setHeader(HttpHeaders.X_XSS_PROTECTION, "0");
    return;
  }
  if (_X_XSS_PROTECTION == null) {
    return;
  }
  response.setHeader(HttpHeaders.X_XSS_PROTECTION, _X_XSS_PROTECTION);
}

相关文章