我需要在Java中实现我自己的HttpSession版本.我发现很少的信息可以解释如何实现这样的壮举.
我想我的问题是 - 无论应用服务器的实现如何,我如何覆盖现有的HttpSession?
我确实遇到过质量但相当旧的阅读,这有助于我实现自己的目标 - http://java.sun.com/developer/technicalArticles/Servlets/ServletControl/
还有其他方法吗?
创建一个新类,并实现 HttpSession:
public class MyHttpSession implements javax.servlet.http.HttpSession {
// and implement all the methods
}
Run Code Online (Sandbox Code Playgroud)
免责声明:我自己没有测试过:
然后用 /* 和 extend 的 url-pattern 编写一个过滤器HttpServletRequestWrapper。您的包装器应该HttpSession在getSession(boolean). 在过滤器中,使用您自己的HttpServletRequestWrapper.
它有两种方式.
HttpSession在您自己的HttpServletRequestWrapper实现中"包装"原始文件.
我在不久前做了这个,用于使用Hazelcast和Spring Session集群分布式会话.
这里解释得很好.
首先,实施自己的 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 ...
}
Run Code Online (Sandbox Code Playgroud)
之后,从您自己的Filter中包装原始文件HttpSession,并将其放在FilterChainServlet容器提供的内部.
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);
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
最后,在web.xml的开头设置Filter,以确保它在任何其他之前执行.
实现它的第二种方式是为您的Servlet容器提供自定义的SessionManager.
例如,在Tomcat 7中.
| 归档时间: |
|
| 查看次数: |
9594 次 |
| 最近记录: |