Original address: http://blog.csdn.net/caiwenfeng_for_23/article/details/45666831
PS: Until 2015-05-12 is not supported TOMCAT8, details see official website: Https://github.com/jcoleman/tomcat-redis-session-manager
Prerequisite: You have deployed Redis, have not learned, can be moved here: http://blog.csdn.net/caiwenfeng_for_23/article/details/45511007
My case Download: http://download.csdn.net/detail/caiwenfeng_for_23/8689847
Actually very simple, just a few steps:
1. Configure the context.xml file under Tomcat's conf directory:
1> Single Point Reids configuration
<!--
Jedis save Session--
<valve classname= " Com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve "/>
<manager classname=" Com.orangefunction.tomcat.redissessions.RedisSessionManager "
host=" localhost "
port=" 6379 "
database = "0"
maxinactiveinterval= "/>"
1 2 3 4 5 6 7 8 9
2> Sentinel Cluster Configuration:
<!--Sentinel configuration-
<valve classname= " Com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve "/>
<manager classname=" Com.orangefunction.tomcat.redissessions.RedisSessionManager "
maxinactiveinterval=" "
sentinelmaster=" MyMaster "
sentinels=" 127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381,127.0.0.1:26382 "
/>
1 2 3 4 5 6 7
2. Add jar
3. Testing
1>
Storage session:
protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { C1/>system.out.println ("Hello");
Get Session Object
HttpSession session=request.getsession ();
Set Session property
for (int i=0;i<100000;i++) {
session.setattribute ("name" +i, "Magci_" +i);
}
}
1 2 3 4 5 6 7 8 9
2> reboot Tomcat: If the session is saved under Tomcat, the session does not exist after the reboot, and if it is saved under Redis, the Tomcat restart has no effect on the session.
3> Remove Session:
protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { C1/>system.out.println ("Hello");
Get Session Object
HttpSession session=request.getsession ();
Remove the session property for
(int i=0;i<100000;i++) {
System.out.println (session.getattribute ("name" +i));}
}
1 2 3 4 5 6 7 8 9
Note: Starting from TOMCAT6, the session persistence setting is turned on by default, the local session persistence can be closed during testing, in fact it is very simple, in the context.xml file under Tomcat's Conf directory, uncomment the following configuration:
<!--uncomment this to disable session persistence across Tomcat restarts-
<!--
<manager pathname= ""/>
-