How to Share Tomcat Sessions via Redis: Step-by-Step Setup Guide

This tutorial walks through configuring Redis for Tomcat session sharing on Windows, covering environment setup, Gradle compilation of the tomcat-redis-session-manager plugin, jar deployment, context.xml configuration, and verification of shared session IDs across multiple Tomcat instances.

21CTO
21CTO
21CTO
How to Share Tomcat Sessions via Redis: Step-by-Step Setup Guide

1. Introduction

This article tests how to implement session sharing using Redis, without discussing Nginx load balancing.

2. Environment Configuration

The test is performed on Windows. The components used are:

Tomcat1 – version 7.0.61 – port 127.0.0.1:8081

Tomcat1 – version 7.0.61 – port 127.0.0.1:8082

Redis – version 2.4.5 – port 127.0.0.1:6379

JDK – version 1.7

3. Install tomcat-redis-session-manager Plugin

1. Source code download: https://github.com/jcoleman/tomcat-redis-session-manager The latest source requires JDK 1.7; using JDK 1.6 caused the following error:

java.lang.UnsupportedClassVersionError: com/orangefunction/tomcat/redissessions/RedisSessionHandlerValve : Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    ... (stack trace omitted for brevity) ...

If you need compatibility with older Tomcat/JDK versions, you may try other branches (unverified).

2. Jar package download:

https://github.com/jcoleman/tomcat-redis-session-manager/downloads

Many users report bugs in the jar, so the author rebuilt it from source.

3. Recompile the source (Gradle is required):

Download the complete Gradle distribution.

Extract it and set GRADLE_HOME (e.g., E:\java\gradle-2.12-all\gradle-2.12) and add %GRADLE_HOME%\bin to PATH.

Verify the installation with gradle -v.

C:\Users\Administrator>gradle -v

------------------------------------------------------------
Gradle 2.12
------------------------------------------------------------

Build time:   2016-03-14 08:32:03 UTC
Build number: none
Revision:     b29fbb64ad6b068cb3f05f7e40dc670472129bc0

Groovy:       2.4.4
Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM: 1.7.0_79 (Oracle Corporation 24.79-b02)
OS: Windows 7 6.1 amd64

4. Modify build.gradle to use Maven Central and adjust dependencies (excerpt):

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'signing'

group = 'com.orangefunction'
version = '2.0.0'

repositories {
    mavenLocal
    mavenCentral
}

compileJava {
    sourceCompatibility = 1.7
    targetCompatibility = 1.7
}

dependencies {
    compile group: 'org.apache.tomcat', name: 'tomcat-catalina', version: '7.0.61'
    compile group: 'redis.clients', name: 'jedis', version: '2.5.2'
    compile group: 'org.apache.commons', name: 'commons-pool2', version: '2.2'
    // other dependencies omitted for brevity
}

// task to copy runtime jars
task copyJars(type: Copy) {
    from configurations.runtime
    into 'dist'
}

5. Build the project and copy the generated jar: gradle build -x test copyJars The jar appears in the dist directory (see screenshot):

6. Import the jar into Tomcat and modify context.xml:

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

Place the jar in Tomcat's lib directory (remove duplicate packages if any). Repeat the configuration for each Tomcat instance.

4. Test Results

Start Redis, Tomcat1, and Tomcat2, then access the session example servlet on both ports:

http://localhost:8081/examples/servlets/servlet/SessionExample

Session ID: 9E5BA1DB4BF56A025A66AF567057EBCB

http://localhost:8082/examples/servlets/servlet/SessionExample

Session ID: 9E5BA1DB4BF56A025A66AF567057EBCB (identical to the first).

Connect to Redis with redis-cli and run KEYS * to view stored sessions (screenshot):

The three entries share the same Session ID, confirming successful session sharing via Redis.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaRedisGradleTomcatsession sharing
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.