Home  >  Q&A  >  body text

java - tomcat+redis集群session共享

简单做了一个测试,servlet中post方法存入session(存入之后立马去getAttribute是有值的),redis也存进去了,但是重新再请求get方法中request.getSession().getAttribute确是null,求解?

环境:jdk7+tomcat7+redis2.8

PHP中文网PHP中文网2712 days ago318

reply all(6)I'll reply

  • 巴扎黑

    巴扎黑2017-04-18 09:23:54

    Redis puts the object and it is null when it is taken out. It is good to put the string. It seems that the only way is to convert the object to json

    Problem solved: The bean object was not serialized, causing an exception in redis, but it was normal after serialization

    reply
    0
  • 迷茫

    迷茫2017-04-18 09:23:54

    Session is only valid in the current tomcat instance, so in the case of multiple tomcat instances, you cannot use request.getSession().getAttribute to obtain the session, but obtain it from redis.

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 09:23:54

    Now that redis is used, there is no need to use the session in the container. .

    reply
    0
  • 迷茫

    迷茫2017-04-18 09:23:54

    In the cluster session retention solution, you can store the sessionId as a key in redis (the value is the session information you want to save). Of course, when you retrieve it, you need to retrieve it from redis. Tomcat does not save the session information.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 09:23:54

    There is a third-party jar package that can be integrated with tomcat to save the session in redis. The application code does not need to pay attention to this matter

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 09:23:54

    Try spring-session

    reply
    0
  • Cancelreply