Tomcat 使用Redis存储Session

Tomcat Redis Session Github 地址

下载 commons-pool2-2.2.jar,jedis-2.5.2.jar,tomcat-redis-session-manager-2.0.0.jar 这三个包,将其放到 tomcat 目录下的lib目录下。

修改tomcat 的conf目录下的 context.xml 文件。

在Context中插入下面的代码。

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
    <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
     host="localhost"
     port="6389"
     database="0"
     maxInactiveInterval="60"
     />

更详细的方式可以查看github的配置。

这样就配置好了一件简单的使用redis存储session的环境,对于集群可以采取相同的配置。

测试Servlet:

@WebServlet(urlPatterns = "/myhttp")
public class MyHttpServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取session
        HttpSession httpSession = request.getSession();
        httpSession.setAttribute("name","name");
        //设置为0 永不过期
        httpSession.setMaxInactiveInterval(1000);
        //使httpserssion无效
//        httpSession.invalidate();
        System.out.println(httpSession.getId());
        response.getWriter().print("http");
    }
}

启动tomcat,访问 http://localhost:8080/myhttp 则可以在redis下看到

image

image

证明环境已经配置成功,可以使用了。

posted on 2016-08-24 11:20 NNS 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/hitandrew/p/5802144.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值