程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在Tomcat中终止所有会话?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何在Tomcat中终止所有会话??

开发过程中遇到如何在Tomcat中终止所有会话?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何在Tomcat中终止所有会话?的解决方法建议,希望对你解决如何在Tomcat中终止所有会话?有所启发或帮助;

我假设您的应用程序实际上是独立的上下文。我所做的事情与您在httpSessionListener每种情况下使用的要求类似。这里最棘手的部分是,您需要具有一个由根类加载器而不是上下文类加载器加载的Session Collection。这是我记得的方式:

创建一个类,其中包含每个上下文的活动会话。此类 位于tomcat / lib目录中,以便每个上下文均可访问。它不能是任何上下文的一部分。

public class SessionMap {

  private static Map<ServletContext, Set<httpSession>> map =
    new HashMap<ServletContext, Set<httpSession>>();

  private SessionMap() {
  }

  public static Map<ServletContext, Set<httpSession>> getInstance() {
    return map;
  }

  public static voID invalIDate(String[] contexts) {
    synchronized (map) {
      List<String> l = Arrays.asList(contexts);     
      for (Map.Entry<ServletContext, Set<httpSession>> e : map.entrySet()) {
        // context name without the leading slash
        String c = e.getKey().getcontextpath().substring(1);
        if (l.contains(c)) {
          for (httpSession s : e.getValue()) 
            s.invalIDate();
        }
      }
    }
  }

}

为每个上下文创建一个侦听器。

public class ApplicationContextListener implements httpSessionListener {

  public voID sessionCreated(httpSessionEvent event) {
    ConcurrentMap<ServletContext, Set<httpSession>> instance = SessionMap.getInstance();
    synchronized (instance) {
      ServletContext c = event.getSession().getServletContext();
      Set<httpSession> set = instance.get(c);
      if (c == null) {
        set = new HashSet<httpSession>();
        instance.put(c, set);
      }
      set.add(event.getSession());
    }
  }

  public voID sessionDestroyed(httpSessionEvent event) {
    ConcurrentMap<ServletContext, Set<httpSession>> instance = SessionMap.getInstance();
    synchronized (map) {
      ServletContext c = event.getSession().getServletContext();
      Set<httpSession> set = instance.get(c);
      if (c != null) {
        set.remove(event.getSession());
      }
    }
  }

}

在相应上下文的中注册每个侦听器web.xml

<Listener>
  <Listener-class>ApplicationContextListener</Listener-class>
</Listener>

然后,您可以调用以下行以使任何上下文中的所有内容均无效。

SessionMap.invalIDate();

为了安全起见,我正在地图上进行同步。

解决方法

我想在Tomcat中终止所有会话。我们在Fitnesse下测试我们的产品,并且仍有一些会话,会话结束导致测试之间的依赖性。我使用以下代码手动完成此操作,但仍保留一些会话(我可以使用http://
localhost:8080 / manager / html /
list url进行检查)

public static void expireAllSessions() {
    String[] applications = { "a","b","c","d","e" };
    for (String application : applications) {
        try {
            expireAllSessions(application);
        } catch (Exception e) {
            logger.error(e);
        }
    }
}

private static void expireAllSessions(final String application) throws Exception {
    // cf doc http://hc.apache.org/httpclient-3.x/authentication.html
    HttpClient client = new HttpClient();
    client.getParams().setAuthenticationPreemptive(true);
    Credentials userPassword = new UsernamePasswordCredentials("tomcat","tomcat");
    client.getState().setCredentials(AuthScope.ANY,userPassword);

    String url = "http://localhost:8080/manager/html/expire";
    NameValuePair[] parametres = new NameValuePair[] {
            new NameValuePair("path","/" + application),new NameValuePair("idle","0")
    };
    HttpMethod method = new GetMethod(url);
    method.setQueryString(parametres);
    client.executeMethod(method);
}

有没有办法在没有剩余会话的情况下更有效,更快捷地执行此操作?

大佬总结

以上是大佬教程为你收集整理的如何在Tomcat中终止所有会话?全部内容,希望文章能够帮你解决如何在Tomcat中终止所有会话?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: