本实例的环境:
操作系统:Mac OS
Tomcat:7.052
JDK:1.7
一、将Session持久化到文件.
1.新建一个WEB应用,编写一个Servlet,在Servlet里面打印当前请求的SessionID,代码如下:
package
com.minutch; import
java.io.IOException; import
javax.servlet.ServletException; import
javax.servlet.annotation.WebServlet; import
javax.servlet.http.HttpServlet; import
javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse; import
javax.servlet.http.HttpSession; @WebServlet ( "/GetInfoServlet" ) public
class GetInfoServlet extends
HttpServlet { private
static final
long serialVersionUID = 1L; public
GetInfoServlet() { super (); } protected
void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { HttpSession session = request.getSession();
PrintWriter out = response.getWriter(); out.println( "Tomcat"
+ new
SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).format( new
Date()) + " sessionId----->"
+ session.getId()); } protected
void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { } } |
2.Tomcat持久化的方式(Tomcat默认带了两种持久化方式,StandardManager,PersistentManager两种)
2.1 StandardManager 默认的方法,在关闭时Tomcat会自动保存Session到session.ser 文件里,所以在默认的Tomcat里,你重启Tomcat,Session依然是上次的Session。如果你想每次关闭后销毁Session,只要把context.xml的这段配置注释去掉就行,请自己动手实践,不知道如何实践可以联系chongjian.min@tqmall.com。
<!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> --> |
2.2 PersistentManager 提供了很灵活的管理方式 配置性强,本次讲解的是通过PersistentManager讲Session持久化到文件。
3. Tomcat使用PersistentManager持久化Session的配置
打开Tomcat的配置文件 ${TomcatHome}/conf/content.xml,配置信息如下:
< Context > <!-- Default set of monitored resources --> < WatchedResource >WEB-INF/web.xml</ WatchedResource > <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> --> <!--添加该Manager节点配置,将Session持久化到文件--> < Manager
className = "org.apache.catalina.session.PersistentManager" > debug=0 saveOnRestart="true" maxActiveSession="-1" minIdleSwap="-1" maxIdleSwap="-1" maxIdleBackup="1" < Store
className = "org.apache.catalina.session.FileStore" directory = "/Users/Minutch/tomcatSession" /> </ Manager > <!-- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle) --> <!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> --> </ Context > |
className:Session的管理类,tomcat将会通过定义的接口来应用该类生成的对象。
debug:Session管理器的跟踪级别。
saveOnRestart:(true/false)配置服务重启工程中对session的处理,若为true,则关闭前把有效的session保存,启动后重新载入
maxActiveSession:活动状态Session的最大数,为-1时则不限制,否则Session Manager将会把超出的Session对象转移到Session Store中。
minIdleSwap:Session不活动的最短时间,超过该时间,Session Manager 可能会把该Session对象转移到Session Store中,单位为秒。
maxidleSwap:Session不活动的最长时间,超过该时间,Session Manager 将会把该Session对象转移到Session Store中,该Session将不在内存中。
maxidleBackup: Session不活动的最长时间,超过该时间,Session Manager 将会把该Session对象备份到Session Store中,但该Session对象依然存在内存中。
<Store>指定实现持久化的类和Session存放的文件位置,如该例子中指定的类是:org.apache.catalina.session.FileStore,而Session对象存放的目录则是tomcat根目录下的 session文件夹(当然自己创建)
4.启动Tomcat
4.1 打开浏览器,访问Servlet,打印Session(浏览器不要关闭)。
4.2 进入配置文件配置的Session存放目录,现在里面没有文件
4.3 重启Tomcat,进入到Session存放目录,里面有一个${sessionId}.session的文件,这里保存的session信息。
4.4 再次访问Servlet,发现打印出的SessionID和上一次一样。
5. Question And Answer:
5.1 Q:为什么两次访问要在一个浏览器,且不能关闭?
A:因为在浏览器的Cookie保存了一个SessionId,请求访问服务器时会通过SessionID找到正确的Session信息。
5.2 Q:为什么我重启后没有生成${sessionId}.session的文件
A:首先你要保证你是正常退出Tomcat的,使用shutdown.sh 并不能保证一定关闭了tomcat,你可以使用ps -ef | grep tomcat查看进程是否存在,如果存在,就是Tomcat还未退出,这时使用kill -15 ${PID}来杀掉进程,Tomcat退出。注意:不能使用kill -9 ,因为这是强制退出,命令发出后,Tomcat还未将Session持久化到文件,进程久已经关闭了。
5.3 Q:为什么保存到的Session的对象一定要序列化
A:因为需要保存到文件里面
二、将Session持久化到Redis
1.还是使用上一个WEB工程。
2.启动Redis服务器,使用默认端口6379
src /redis-server
--port 6379 |
3.启动Redis客户端,连上刚刚启动的服务器
src /redis-cli
-p 6379 |
4.查看当前Redis的数据
keys * |
5.配置Tomcat持久化Session到Redis需要的jar (版本不对可能会报classNotFoundException,我开始用commons-pool-2.3.jar,jredis-1.0rc.jar,都不行)
commons-pool-1.6.jar,
jedis-2.1.0.jar
tomcat-redis-session-manager-1.2-tomcat-7-java-7.jar
讲上面三个jar放到tomcat的lib目录下
6.配置Tomcat'的content.xml文件,详情如下
< Context > <!-- Default set of monitored resources --> < WatchedResource >WEB-INF/web.xml</ WatchedResource > <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> --> <!-- Session持久化到文件的配置 <Manager className="org.apache.catalina.session.PersistentManager"> debug=0 saveOnRestart="true" maxActiveSession="-1" minIdleSwap="-1" maxIdleSwap="-1" maxIdleBackup="1" <Store className="org.apache.catalina.session.FileStore"
directory="/Users/Minutch/tomcatSession"
/> </Manager> --> < Valve
className = "com.radiadesign.catalina.session.RedisSessionHandlerValve"
/> <!--Redis配置--> < Manager
className = "com.radiadesign.catalina.session.RedisSessionManager" host = "localhost" port = "6379" database = "0" maxInactiveInterval = "10" /> <!-- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle) --> <!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> --> </ Context > |
7.启动Tomcat,打开浏览器,访问Servlet,打印SessionId
8.查看当前的Redis的内容 keys *,发现多了一个以SessionId为key的键值对
9.重启Tomcat,再次访问Servlet(假设浏览器未关闭过),查看打印出来的SessionId,和上次一致。服务器的Session并未因为Tomcat重启而丢失Session