PS:截止到2015-05-12前是不支持Tomcat8的,详情见官网:https://github.com/jcoleman/tomcat-redis-session-manager
前提:你已经部署了Redis,尚未学会的,可以移步这里:http://blog.csdn.net/caiwenfeng_for_23/article/details/45511007
我的案例下载:http://download.csdn.net/detail/caiwenfeng_for_23/8689847
其实很简单,就几个步骤:
1.配置Tomcat的conf目录下的context.xml文件:
<!--
Jedis save session
-->
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="localhost"
port="6379"
database="0"
maxInactiveInterval="60"/>
2.添加jar
3.测试
1>
存储Session:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("hello");
//取得Session对象
HttpSession session=request.getSession();
//设置Session属性
for(int i=0;i<100000;i++){
session.setAttribute("name"+i, "Magci_"+i);
}
}
2>重启Tomcat:假如Session保存在tomcat下,重启后Session不存在;如果保存在Redis下,Tomcat重启对Session无影响
3>取出Session:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("hello");
//取得Session对象
HttpSession session=request.getSession();
//取出Session属性
for(int i=0;i<100000;i++){
System.out.println(session.getAttribute("name"+i));
}
}
可以尝试运行上面的demo案例!
原文地址:http://blog.csdn.net/caiwenfeng_for_23/article/details/45666831