session超时设置

时间:2022-08-29 13:23:50

session的设置方法有三种:

  1、直接在应用服务器中设置

    如果是Tomcat,可以在Tomcat目录下conf/web.xml中找到<session-config>元素,tomcat默认设置是30分钟,只要修改这个值就可以了,我们可以看到Tomcat默认超时也是30分钟

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAogAAACECAIAAACcdk2/AAAPR0lEQVR4nO3dzdmiMBSGYeqioNQxJbCzCrcpwCLcWMJXRGYBQhJyQoKgB3juazbDhyH86CGIvI0DAABqNL/uAAAAmFCYAQBQhMIMAIAiFGYAABShMAMAoAiFGQAARSjMAAAoQmEGAEARCjMAAIpQmAEAUITCDACAIhRmAAAUoTADAKDIZoXZ3pvmbrdqDVf317X/mmb6ZwqPLXufXmWea5b8erT9y9vHa83rPd3t3Zm7Mbfuw9aS7H2DfmYaH/p/616Pdt32jJsK98uu/V9BW39wTSsL8+vRRp+Vr0dLYcaWnmY6op6mqDb/de1G9e/1aOcf0N2t9PwgasHem2afwrwf/1S7P1lZX5i32y+bqNqPQAFrtjiVH9UX5n4oM3ubJT/IPjcOX/x/7ePF9HNMz+17vzD7B5g/mPaOQ39MFvzpaeZL9CtN/8KoM/HxHI/gl6uU+I4Q+u/8Efat9StZcnpmZD/N/z6bGdb3bqLpor+ulc+zq9qX9ovcf2uGmdvuYfzjJ7m/+s54x9h70an9ntmPu25Pbe87pid39Ie6W9P8a7u/DZqqK8yZBe9UmHFdYWEe/9vdpiPw9WiD2pYcmT3t2Ep3Cz6Ip+L6NAuFeXh53UjLK6h+lU33Xxph50fe8352N+8T52nCQjjVlcWzImmGNe3LI+Zk/4f98te14bmUuL/6Gtwv4mmG2YT97rL7ca/tiYsQBq61Sgtzf8aROfhmh+Z42hudbErTc8vVc4bF9G2n5465ZGGejXiC4ydZAMKXfLMw+60N79Vc//23hr8gaXqqn7ORrr0P5wH59Y1JM6xrv7wwh3P6f80X5kRvhf3uqgrzRttT2/uO6UufPx/Vr8Vyuah6xMyJIb4kfSnb5u6i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YII=" alt="" />

  2、通用的设置session失效时间的方法,就是在项目的web.xml中设置

    <!-- 设置session 有效时间为30分钟 -->
<session-config>
  <session-timeout>30</session-timeout>
  </session-config>
    <!-- 设置session为0或-1表示永不超时 -->
<session-config>
  <session-timeout>-1</session-timeout>
  </session-config>

  3、在页面中设置

    在主页面或者公共页面中加入:session.setMaxInactiveInterval(600);参数600单位是秒,即在没有10分钟活动后,session将失效

    HttpSession session = request.getSession();
session.setAttribute(Constants.SESSION_USER_INFO, user);
session.setMaxInactiveInterval(600);

    注:这个session设置的时间是根据服务器来计算的,而不是客户端。所以如果是在调试程序,应该是修改服务器端时间来测试,而不是客户端

  如果再三处都设置了session,那么就有session优先级的问题:(3)>(2)>(1)