Before explaining what they are be sure to understand a few things.
From your question it is clear you are already aware of the first but perhaps confused about the second item in the below list:
- the two settings use different units:
session-timeout
is in minutes, whereas max-age
is in seconds
- they measure time in different ways:
session-timeout
measures time in a relative way, max-age
measures time in an absolute way (explained further below)
- they are taken into account and enforced by different software components. The
session-timeout
is taken into account by the container, whereas the max-age
is taken into account and enforced by the user's browser. Equivalently, you may say that session-timeout
applies to the server-side, whereas max-age
applies to the client side.
session-timeout
gives the maximum idle duration before the container decides to destroy the session object representing your "connection" in the server. This means that you may set the value of session-timeout
to just 1 minute and still manage to keep the session object in the server forever as long as your browser sends HTTP GET, POST etc. messages to the server once every 59 seconds.
max-age
is used by the user's browser to compute an absolute, fixed point in time, beyond which the session cookie (JSESSIONID
in Java) will no longer be sent to the server. The browser computes this fixed point in time based on the time when the server sent the cookie to the browser (plus max-age
). This is an absolute fixed point in time beyond which the cookie will no longer be sent to the server. As such, activity or inactivity on behalf of the user makes no difference. That's why if you examine the cookies in the developer console of your browser you see an absolute timestamp for the session cookie:
Caveat
An exception to the above description on the value of max-age
denoting a fixed point in time, is if the specially interpreted value -1
is used. In such a case that's what you see in the developer console:
… and also as explained in this answer this means that the browser will keep sending the cookie for the duration of the "browser session". I am putting "browser session" in quotes to differentiate it from server-side sessions. How the concept of a session is understood by a browser (e.g. whether different tabs correspond to different sessions) is implementation-specific.
Given the different semantics of session-timeout
and max-age
, it follows that attempts to "align" the two values like the web.xml
excerpt you provide in your question:
<session-config>
<session-timeout>30</session-timeout> <!-- 30 minutes! -->
<cookie-config>
<http-only>true</http-only>
<max-age>1800</max-age> <!-- 1800 seconds: 30 minutes! -->
</cookie-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
… likely indicate confusion.
max-age
provides a hard limit (unless the special value -1
is used), whereas session-timeout
effectively provides no limit, as long as the user actively uses the session. This being said, I think it makes more sense that max-age
is larger in value than session-timeout
rather than the other way around.
Regarding the default and specially interpreted values (0
for the session-timeout
and -1
for max-age
) and whether you can configure those values for all cookies (as opposed to just the session cookie), these points are explained in this answer.