Tomcat7+redis Storage Session

Source: Internet
Author: User
Tags redis tomcat

Original address: http://blog.csdn.net/caiwenfeng_for_23/article/details/45666831

PS: Until 2015-05-12 is not supported TOMCAT8, details see official website: Https://github.com/jcoleman/tomcat-redis-session-manager

Prerequisite: You have deployed Redis, have not learned, can be moved here: http://blog.csdn.net/caiwenfeng_for_23/article/details/45511007

My case Download: http://download.csdn.net/detail/caiwenfeng_for_23/8689847

Actually very simple, just a few steps:
1. Configure the context.xml file under Tomcat's conf directory:

1> Single Point Reids configuration

    <!-- 
    Jedis save Session--
    <valve classname= " Com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve "/>        
    <manager classname=" Com.orangefunction.tomcat.redissessions.RedisSessionManager " 
        host=" localhost " 
        port=" 6379 " 
        database = "0" 
        maxinactiveinterval= "/>"
1 2 3 4 5 6 7 8 9

2> Sentinel Cluster Configuration:

        <!--Sentinel configuration-
     <valve classname= " Com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve "/>        
    <manager classname=" Com.orangefunction.tomcat.redissessions.RedisSessionManager " 
        maxinactiveinterval=" "
        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. Add jar

3. Testing

1>
Storage session:

    protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { C1/>system.out.println ("Hello");
           Get Session Object
        HttpSession session=request.getsession (); 
        Set Session property
        for (int i=0;i<100000;i++) {
            session.setattribute ("name" +i, "Magci_" +i); 
        }
    }
1 2 3 4 5 6 7 8 9

2> reboot Tomcat: If the session is saved under Tomcat, the session does not exist after the reboot, and if it is saved under Redis, the Tomcat restart has no effect on the session.

3> Remove Session:

    protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { C1/>system.out.println ("Hello");
           Get Session Object
        HttpSession session=request.getsession (); 
        Remove the session property for
        (int i=0;i<100000;i++) {
            System.out.println (session.getattribute ("name" +i));}
    }
1 2 3 4 5 6 7 8 9

Note: Starting from TOMCAT6, the session persistence setting is turned on by default, the local session persistence can be closed during testing, in fact it is very simple, in the context.xml file under Tomcat's Conf directory, uncomment the following configuration:

    <!--uncomment this to disable session persistence across Tomcat restarts-
    <!--
    <manager pathname= ""/>

-

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.