Languages
[Edit]
EN

Tomcat change session timeout

12 points
Created by:
Root-ssh
175400

1. Open web.xml

web.xml location:

apache-tomcat-8.5.38/conf/web.xml

2. Change value of <session-timeout>30</session-timeout>

After we opened web.xml, we need to find <session-timeout> tag in xml and we can change it to other value.

<!-- ==================== Default Session Configuration ================= -->
<!-- You can set the default session timeout (in minutes) for all newly   -->
<!-- created sessions by modifying the value below.                       -->

<session-config>
    <session-timeout>30</session-timeout>
</session-config>
session-timeout value is defined in minutes

Explanation:

  • to set to 1h set session-timeout value to 60
  • to set to 24h set session-timeout value to 1440

Calculations:

  • 60 min - 1h
  • 1440 min - 24h (24*60) - 1 day

Examples:

  • Set session timeout to 60 min (1h)
<session-config>
    <session-timeout>60</session-timeout>
</session-config>
  • Set session timeout to 1440 min (24h)
    1 hour = 60 min
    60 min * 24 hours = 1440 min
<session-config>
    <session-timeout>1440</session-timeout>
</session-config>
  • Set session timeout to 10080 min (7 days)
    1 days = 1440 min
    1440 min * 7 days = 10080 min
<session-config>
    <session-timeout>10080</session-timeout>
</session-config>
  • Set session timeout to 43200 min (30 days)
    1 days = 1440 min
    1440 min * 30 days = 43200 min
<session-config>
    <session-timeout>43200</session-timeout>
</session-config>

3. Restart tomcat

With Intellij IDEA - restart tomcat from IDEA level.

If we run tomcat on windows:

  • apache-tomcat-8.5.38\bin\shutdown.sh
  • apache-tomcat-8.5.38\bin\startup.sh

If we run tomcat on windows:

  • apache-tomcat-8.5.38\bin\shutdown.bat
  • apache-tomcat-8.5.38\bin\startup.bat

Second way to do it - Timeout per individual Session

We can set session timeout programmatically for each request.

The way to do it:

import javax.servlet.http.HttpSession;

HttpSession session = request.getSession();
session.setMaxInactiveInterval(1440); // 24h

Read more

  1. JavaEE - HttpSession.setMaxInactiveInterval(int)
  2. Spring session - setMaxInactiveInterval(java.time.Duration interval)
  3. Apache tomcat - setSessionTimeout(int)
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join