Tomcat offerts some control over the session cookie's path with the sessionCookiePath context parameter, but not enough for your needs. I beleive you can play fast and loose with both the server (servlet container) and the browser with very little code.
In short, write a servlet filter that let the request in, unchanged. When the response comes out, add your servlet path to the JSESSIONID cookie path and let the browser send a different JSESSIONID (thus using a different session) for each servlet.
Here is how it works :
- When a request comes in the first time, it is "naked", it does not have any cookies associated with it. It does not have a session.
- The server will create one and associate a
JSESSIONID to it and add a cookie to let the client do its part of the tracking.
- The client is still waiting on its initial request at this point.
- In the servlet filter (or mod_rewrite) add the servlet path to the cookie's path.
- Send the response to the client.
With the cookie personalized for servlet
/dir1, when the browser will send a request to
/dir2, it will also be "naked", no cookies whatsoever. A new session will be created, the filter will add the path to it and so on...
From the server's point of view, there are two clients (with the same IP). One is always using
/dir1 servlet and the other is only using
/dir2 servlet. Nothing wrong with that.
Here is one filter implementation that can give a head start. You might also consider doing it in Apache with mod_headers.