tomcat 設定session過期時間(四種方式)

weixin_34198881發表於2018-08-16
1、在tomcat——>conf——>servler.xml檔案中定義:
<Context path="/test" docBase="/test"
  defaultSessionTimeOut="3600" isWARExpanded="true"
  isWARValidated="false" isInvokerEnabled="true"
  isWorkDirPersistent="false"/>

2、在web.xml中定義:這個針對具體專案:

<session-config>
<session-timeout>20</session-timeout>
</session-config>

3、在程式中定義:這個就針對具體頁面了:

session.setMaxInactiveInterval(30*60);

4、配置tomcat的session持久化:

<Manager
className="org.apache.catalina.session.PersistentManager"
saveOnRestart="true"
maxActiveSession="-1"
minIdleSwap="0"
maxIdleSwap="30"
maxIdleBackup="0"
>
<Store
className="org.apache.catalina.session.FileStore"
checkInterval=”60”
directory="../session"/>
</Manager>

<Store
calssName="org.apache.catalina.JDBCStore"
driverName="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost/tomsessionDB?user=root&password="
sessionTable="tomcat_session"
sessionIdCol="session_id"
sessionDataCol="session_data"
sessionValidCol="session_valid"
sessionMaxInactiveCol="max_inactive"
sessionLastAccessedCol="last_access"
sessionAppCol="app_name"
checkInterval="60"
debug="99" />

maxActiveSessions-可處於活動狀態的session數,default -1 不限制

checkInterval  檢查session是否過期的時間間隔,default 60s

saveOnRestart-伺服器關閉時,是否將所有的session儲存到檔案中;
minIdleSwap/maxIdleSwap
session處於不活動狀態最短/長時間(s)sesson物件轉移到File Store中;(1表示沒有限制)
maxIdleBackup
超過這一時間,將session備份。(1表示沒有限制)

directory-檔案儲存位置work\Catalina\host name\web app\session\檔名.session




相關文章