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文件:

1> 单点Reids配置

    <!--
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"/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2> Sentinel集群配置:

        <!-- Sentinel 配置 -->
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
maxInactiveInterval="60"
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.添加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);
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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));
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

注意事项:从Tomcat6开始默认开启了Session持久化设置,测试时可以关闭本地Session持久化,其实也很简单,在Tomcat的conf目录下的context.xml文件中,取消注释下面那段配置即可:

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
  • 1
  • 2
  • 3
  • 4

详见这篇博客:Session持久化的实例分析

可以尝试运行上面的demo案例!

转:http://blog.csdn.net/caiwenfeng_for_23/article/details/45666831

Tomcat7+Redis存储Session(转)的更多相关文章

  1. (转)Tomcat7+Redis存储Session

    原创http://blog.csdn.net/caiwenfeng_for_23/article/details/45666831 PS:截止到2015-05-12前是不支持Tomcat8的,详情见官 ...

  2. Tomcat 使用Redis存储Session

    Tomcat Redis Session Github 地址. 下载 commons-pool2-2.2.jar,jedis-2.5.2.jar,tomcat-redis-session-manage ...

  3. Asp.net Core 使用Redis存储Session

    前言 Asp.net Core 改变了之前的封闭,现在开源且开放,下面我们来用Redis存储Session来做一个简单的测试,或者叫做中间件(middleware). 对于Session来说褒贬不一, ...

  4. Redis存储Session

    net Core 使用Redis存储Session   前言 Asp.net Core 改变了之前的封闭,现在开源且开放,下面我们来用Redis存储Session来做一个简单的测试,或者叫做中间件(m ...

  5. redis存储session配制方法

    redis存储session配制方法需要三个模块: 1.redis 2.express-session 3.connect-redis 项目中的配置方法代码片段如下: 首先连接redis,连接redi ...

  6. 几分钟搞定redis存储session共享——设计实现

    前面我们写过C#在redis中存储常用的5种数据类型demo,没看过的可以点击电梯直达:https://www.cnblogs.com/xiongze520/p/10267804.html 我们上一篇 ...

  7. Spring Boot+redis存储session,满足集群部署、分布式系统的session共享

    本文讲述spring-boot工程中使用spring-session机制进行安全认证,并且通过redis存储session,满足集群部署.分布式系统的session共享. 原文链接:https://w ...

  8. Redis使用示例及在PHP环境中用redis存储session

    在文件夹redis-3.2.0下 1. 启动redis服务 nohup ./src/redis-server redis.conf & 2. 停止服务 #使用客户端 ./src/redis-c ...

  9. [转]Asp.net Core 使用Redis存储Session

    本文转自:http://www.cnblogs.com/hantianwei/p/5723959.html 前言 Asp.net Core 改变了之前的封闭,现在开源且开放,下面我们来用Redis存储 ...

随机推荐

  1. Android开发之使用意图调用内置应用程序

    意图可以调用活动,也常被用来调用内置应用程序,如加载web页面,拨号页面,内置地图应用等等.下面就用例子来说明该用法. 效果图如下: 实现代码如下: 上图中的启动MyBrowser是用意图来调用MyW ...

  2. Customize the SharePoint 2013 search experience with a Content Enrichment web service

    Did you ever wish you had more control over how your content is indexed and presented as search resu ...

  3. 多维数组遍历PHP

    原文出处 <?php /* * ------------------------------------------------- * Author : nowamagic * Url : ww ...

  4. 字符编码笔记:ASCII、Unicode、UTF-8、UTF-16、UCS、BOM、Endian

    转载:http://witmax.cn/character-encoding-notes.html 今天中午,我突然想搞清楚Unicode和UTF-8之间的关系,于是就开始在网上查资料. 结果,这个问 ...

  5. A题笔记(7)

    No. 1468 已知三角形的三条边求面积:海伦公式 S=√[p(p-a)(p-b)(p-c)]   p=(a+b+c)/2 #include <cmath> cmath 是 c++ 语言 ...

  6. hibernate逆向工程生成的实体映射需要修改

    根据实际情况进行修改,主要2处,注释的位置<!-- 把catalog="platform"删掉 -->,<!-- 替换为native -->   <? ...

  7. angularjs跨域调取webservice

    1.配置 web.config <webServices> <!--必须添加--> <protocols> <add name="HttpGet&q ...

  8. 《ln命令》-linux命令五分钟系列之十八

    本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...

  9. /etc/fstab一些信息

    [root@shine shine]# vim /etc/fstab ## /etc/fstab# Created by anaconda on Thu May 9 13:29:35 2013## A ...

  10. [转] 博闻强识:了解CSS中的@ AT规则 ---张鑫旭

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=4900 大家可能在CS ...