This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Servlets and the fly likes multiple HttpSession objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "multiple HttpSession objects" Watch "multiple HttpSession objects" New topic
Author

multiple HttpSession objects

Brusk Baran
Ranch Hand

Joined: Nov 15, 2001
Posts: 132
Hi,
I'd like to learn whether I can create more than one sesiion object for a single user's single request..and how if yes, the relations between these objects will be arranged..
regards,
c.g.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Due to the way the servlet engine tracks sessions, there can be only one per request. That session can hold an essentially unlimited number of objects (not particularly practical.) If you need to hold lots of data per user consider using a database or writing your own session manager equivalent.
Bill
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
You can. I cannot think of any reason why you would want to, but here's my half-baked idea of how to do it:
1) Create a subdomain with an alias, say "sub."
2) Use a wrapper class "MultipleDomainSession" so that multiple sessions can be added to this wrapper and that calling setAttribute on any session will cause
3) implement a request filter. In the filter, call MultipleDomainSession.getSession(hreq.getSession(true)).
- If null, set the cookie. Give it a value of the session id. Call setDomain(".yourhost.com") on the cookie and add it to the response.
- otherwise, if the value is not that of the sessionid, add a separator and the session id.
You will then have a cookie that looks like: "MDSCookie=2354eddafcc08342345a88aecb-25a83ecd8aaf08344e235ccd4b"
If you are using lots of subdomains, you'll have a very long cookie value. (You can translate the base of number to use Character.MAX_RADIX (base 36) instead of the session's hex (base 16).
You can look up your wrapper with any sessionid in the MDSCookie.
Calling getAttribute/setAttribute on your MultipleDomainSession instance will cause it to use its own Hashtable.
You can implement HttpSessionBindingEvent for each session added to a MultipleDomainSession wrapper.

Discussion:
When a request is made for "http://www.yourhost.com/path," a new session will be created.
Adding the new session to a wrapper provides an object with access to all data in the object.
[ December 02, 2002: Message edited by: Garrett Smith ]
[ December 02, 2002: Message edited by: Garrett Smith ]

comp.lang.javascript FAQ: http://jibbering.com/faq/
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
ewww! That is really twisted!
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
You could probably do a much neater job if you were to first study the source code for HttpSession.
Sun has considered session tracking a lot more than I have, and did so with knowledge, teamwork, and experience than I don't have.
 
 
subject: multiple HttpSession objects