20

From my understanding, Servlet Containers handle session using some HTTP protocols like,

  1. Hidden Form Fields
  2. URL Rewriting
  3. Cookies

I'm curious how Apache Tomcat handles the session internally, though it's irrelevant to average developers.

Is Tomcat using cookies or others also?

4

2 回答 2

17

By default, Tomcat directly sends cookies in the HTTP response , like SET COOKIE:JSESSIONID.... back to the browser and rewrites the URL to add a JSESSIONID parameter in it , for the first request, so that it can fall back on the later in case cookies are disabled in the client browser.

The next time if the browser requests the server with the JSESSIONID in its request , Tomcat will use the JSESSIONID cookie for maintaining the session.

You can overide the session cookie behavior in Tomcat by modifying context.xml:

<Context cookies="false">
</Context>

and disable the url re-writing the same way :

<Context disableURLRewriting="true">
</Context>

Even read this Servlet Session Tracking with cookies (JSESSIONID)

于 2013-07-24T13:00:39.227 回答
0

Tomcat sends cookies by default unless they are blocked by the user from the browser (though this practice is not encouraged). Moreover, the session cookies (JSESSIONID) that are created are not persistent cookies and they die off whenever that instance(window) of the browser is closed.

于 2013-07-25T04:30:57.437 回答