Session会话管理

  在Web项目开发中,Session会话管理是一个很重要的部分,用于存储与记录用户的状态或相关的数据;通常情况下session交由容器(tomcat)来负责存储和管理,但是如果项目部署在多台tomcat中,则session管理存在很大的问题;1、多台tomcat之间无法共享session,比如用户在tomcat A服务器上已经登录了,但当负载均衡跳转到tomcat B时,由于tomcat B服务器并没有用户的登录信息,session就失效了,用户就退出了登录;2、一旦tomcat容器关闭或重启也会导致session会话失效;因此如果项目部署在多台tomcat中,就需要解决session共享的问题;

配置文件

1 pom.xml
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.3.1.RELEASE</version>
</dependency> 2 web.xml
  
<filter>
<filter-name>springSessionRepositoryFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSessionRepositoryFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>   <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param> 3 applicationContext.xml
<context:annotation-config/>
<!-- 初始化一切spring-session准备,且把springSessionFilter放入IOC -->
<bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="300"/>
</bean>
  <!-- 配置cookie信息-->
<bean class="org.springframework.session.web.http.DefaultCookieSerializer" id="defaultCookieSerializer">
<property name="cookieName" value="SESSION_NAME"/>
<property name="domainName" value="wangjun.com"/>
<property name="useHttpOnlyCookie" value="true"/>
<property name="cookiePath" value="/"/>
<property name="cookieMaxAge" value="31536000"/>
</bean>
  <!-- 配置redis连接池信息-->  
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="20"/>
</bean>
  <!--配置redis连接信息-->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="127.0.0.1"/>
<property name="port" value="6379"/>
<property name="poolConfig" ref="jedisPoolConfig"/>
</bean>

代码测试

public class SessionServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String sesssionID = request.getSession().getId();
//部署两份,把这个地方8081改成8080就行了,只是为了区分
response.getWriter().write("8081 Server SessionID"+sesssionID);
}
}

最新文章

  1. WebApp开发之--&quot;rem&quot;单位
  2. [Unity3D]自己动手重制坦克舰队ArmadaTank(2)从碰撞说起
  3. python中文分词:结巴分词
  4. JMeter--集合点设置(转)
  5. 应用容器Application container
  6. 使用ADD_CUSTOM_COMMAND 添加自定义命令
  7. UITextField 属性详解
  8. QML之窗口(无边框、透明及拖拽)
  9. Android Listview异步动态加载网络图片
  10. javascript小白学习指南0---1
  11. Spectral Clustering
  12. Jquery右击显示菜单事件,运用smartMenu插件。
  13. Float精度丢失
  14. JVM-类的四种载入方式
  15. Double.valueOf(0.0D) 分析
  16. DDD实践:领域事件
  17. 一次完整的HTTP事务是怎样一个过程?(转)
  18. ng4 路由多参数传参以及接收
  19. (转)对一个deb包的解压、修改、重新打包全过程方法
  20. Centos7.4简单安装使用gitlab+maven+jenkins实现java代码的持续集成部署

热门文章

  1. rxjs入门指南
  2. 面向对象(OOP)笔记
  3. 1632:【 例 2】[NOIP2012]同余方程
  4. 常用SQL之日期格式化和查询重复数据
  5. redhat7.4安装git(按照官网从源码安装)
  6. 64位linux下玩32位汇编编程
  7. H5本地存储详解
  8. git 优雅的撤销中间某次提交
  9. Equals和GetHashCode
  10. unity模型网址