Tomcat的会话超时可以在多个级别上设置:tomcat实例级别、Web应用级别、servlet级别以及运行时Context代码级别。 较低级别的设定会覆盖较高级别的设定。
- Web容器级别
<!--在conf/web.xml中-->
<!-- ==================== Default Session Configuration ================= -->
<!-- You can set the default session timeout (in minutes) for all newly -->
<!-- created sessions by modifying the value below. -->
<session-config>
<!--以分钟为单位的,默认是30分-->
<session-timeout>30</session-timeout>
</session-config>
- webapp级别
<!--在webapp中的 WEB-INF/web.xml-->
<!-- 配置Session失效时间 -->
<session-config>
<!--以min为单位-->
<session-timeout>30</session-timeout>
</session-config>
- 代码中硬编码
session.setMaxInactiveInterval(30*60); //以秒为单位
优先级,越细粒度优先级越高,也就是3>2>1
- 还有一种比较少见配置,将Context配置在server.xml里
<!--修改conf/server.xml-->
<!--单位为秒-->
<Context path="/test" docBase="/home/httpd/html/test"
defaultSessionTimeOut="3600" isWARExpanded="true"
isWARValidated="false" isInvokerEnabled="true"
isWorkDirPersistent="false"/>