---
title: "使用redis保存httpsession--tomcat8实现"
author: "fly0wings"
date: "2014-11-13T22:51:56Z"
tags: []
categories: ["互联网"]
slug: "redis-httpsession-tomcat8"
draft: false
lastmod: "2014-11-13T22:51:56Z"
id: 2155777
url: "https://fly0wing.iteye.com/blog/2155777"
---

在github上已经有人实现该功能,but 在tomcat8下不可用. 所以只能fork自己动手了.其实主要不兼容的问题是一个强制类型转换的事.

```
 com.orangefunction.tomcat.redissessions.RedisSessionManager#initializeSerializer


   if (getContainer() != null) {
      loader = ((Context)getContainer()).getLoader();
    }
```

 

使用:

1\. 将jar放到 $Catalina\_Home$/lib 目录下.  
    -- tomcat8-redis-session-manager  // git@github.com:fly0wing/tomcat-redis-session-manager.git  
    -- redis.clients:jedis:2.5.1  
    -- org.apache.commons:commons-pool2:2.2

2\. 添加 context.xml 文件到web项目的webapp/Meta-Inf/文件夹下.

```
<?xml version='1.0' encoding='utf-8'?>
<Context useHttpOnly="true" sessionCookiePath="/">
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
   
    <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
    <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
         host="192.168.2.100"
         port="7777"
         database="0"
         maxInactiveInterval="60"/>
</Context>
```