在春季使用复述,如何实现共享会话

  介绍

这期内容当中小编将会给大家带来有关在春季使用复述,如何实现共享会话,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

<强> 1,添加依赖

& lt; dependency>   & lt; groupId> org.springframework.session   & lt; artifactId> spring-session-data-redis   & lt; version> 1.2.1.RELEASE   & lt;/dependency>   & lt; dependency>   & lt; groupId> redis.clients   & lt; artifactId> jedis   & lt; version> 2.8.1   & lt;/dependency>

<强> 2,配置

spring mvc。xml:

& lt; bean id=皉edisHttpSessionConfiguration"   类=皁rg.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"比;   & lt;属性名=癿axInactiveIntervalInSeconds"https://www.yisu.com/zixun/value=" 600 "/>            <属性名=" maxTotal " value=" 100 "/>   <属性名=" maxIdle " value=" 10 "/>            <属性名="端口" value=" $ {redis_port} "/>   <属性名="密码" value=" $ {redis_pwd} "/>   <属性名="超时" value=" 3000 "/>   <属性名=" usePool " value=" true "/>   <属性名=" poolConfig“ref=" jedisPoolConfig "/>   

web。xml添加拦截器:

& lt; filter>   & lt; filter-name> springSessionRepositoryFilter   & lt; filter-class> org.springframework.web.filter.DelegatingFilterProxy   & lt;/filter>   & lt; filter-mapping>   & lt; filter-name> springSessionRepositoryFilter   & lt; url-pattern>/* & lt;/url-pattern>   & lt;/filter-mapping>

<强> 3,使用春季

只要使用标准的servlet api调用会话,在底层就会通过春季会议得到的,并且会存储到复述或其他你所选择的数据源中。

这里是我写的一个演示:

/* *   * @author fengzp   * @date 17/2/23下午三19   */@ controller   @RequestMapping (value=https://www.yisu.com/zixun/爸甘?   公开课IndexController {      私人最终Gson Gson=new GsonBuilder () .setDateFormat (yyyyMMddHHmmss)共创();      @RequestMapping (value="登录")   公共字符串登录(HttpServletRequest请求,字符串的用户名){      request.getSession ()。gson setAttribute(“用户”。toJson(新用户(用户名,“123456”)));      返回“登录”;   }      @RequestMapping (value="指数")   公共字符串索引(HttpServletRequest请求,模型模型){      用户用户=gson.fromJson (request.getSession () .getAttribute(“用户”).toString (), User.class);      模型。addAttribute(“用户”,用户);      返回“指数”;   }   }

索引。jsp:

第一个tomcat

& lt; html>   & lt; body>   & lt; h3> Hello World ! & lt;/h3>   & lt; p> $ {user.username} & lt;/p>   & lt;/body>   & lt;/html>

第二个tomcat

& lt; html>   & lt; body>   & lt; h3> Hello World !我是第二个! & lt;/h3>   & lt; p> $ {user.username} & lt;/p>   & lt;/body>   & lt;/html>

测试

这里利用上一篇nginx负载配置的两个tomcat来测试。

首先访问http://192.168.99.100/feng/index/login.htm& # 63;用户名=nginx来触发生成会话。

查看复述,发现会话已经保存到复述。

在春季使用复述,如何实现共享会话”> </p> <p>访问http://192.168.99.100/feng/index/index。htm来读取会话,并刷新多次。</p> <p> <img src=上述就是小编为大家分享的在春季使用复述,如何实现共享会话了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。

在春季使用复述,如何实现共享会话