java中 会话的定义_如何在java中实现自定义http会话?

  • Post author:
  • Post category:java


其两种方式。

将原始HttpSession“包装”到您自己的HttpServletRequestWrapper实施中。

我在很短的时间之前对Hazelcast和Spring Session进行了分布式会话聚类。

Here解释得很好。

首先,实现自己的HttpServletRequestWrapper

public class SessionRepositoryRequestWrapper extends HttpServletRequestWrapper {

public SessionRepositoryRequestWrapper(HttpServletRequest original) {

super(original);

}

public HttpSession getSession() {

return getSession(true);

}

public HttpSession getSession(boolean createNew) {

// create an HttpSession implementation from Spring Session

}

// … other methods delegate to the original HttpServletRequest …

}

,之后从自己的筛选,包装了原HttpSession,然后把它放在你的Servlet容器提供的FilterChain内。

public class SessionRepositoryFilter implements Filter {

public doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {

HttpServletRequest httpRequest = (HttpServletRequest) request;

SessionRepositoryRequestWrapper customRequest =

new SessionRepositoryRequestWrapper(httpRequest);

chain.doFilter(customRequest, response, chain);

}

// …

}

最后,在web.xml中的开头设置你的过滤器,以确保它在任何其他之前执行。

实现它的第二种方式是向您的Servlet容器提供您的自定义SessionManager。



版权声明:本文为weixin_31141099原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。