File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Sharing session across servlet context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Sharing session across servlet context" Watch "Sharing session across servlet context" New topic

Sharing session across servlet context

srinivasan ganesan
Ranch Hand

Joined: Mar 09, 2001
Posts: 54
I would like to share the same session between 2 or more webapps. I was wondering if anyone could help me on this.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
well, at first sight it looks like "Single Sign On" Mechanism to me which i would not go in details as thats not a subject of this forum.
if i restrict myself to Java Servlets then...
initally i thought we can just pass Session object to another appplication. for this there r two scenarios,
1. if we are invoking one app from another then we can probably pass Session object as an argument/parameter and thus share it.
2. if we can serialize the session object then we can share it by putting it in WEB-INF dir and get it by getResource() or sth like that.
i 'm not sure about how the first option will work, if it can work as if we are only talking about servlets then we cant really pass Object as a parameter. i have never tried creating object of a servlet in another servlet but i assume thats not possible, otherwise what we can do is- we can call a method on another servlet and pass the session object as an argument and do the stuff...
there is a problem here in 2nd case. as we aren't really SHARING the same object. so if the original app changes the session object's content we can't reflect that in the other app to which we passed the session object's reference. again i guess there is something we can do like Version control stuff to let other app have latest version of the serialized object(tho i forgot how exactly it was working).
waiting for experts comments...
Srini Admala
Ranch Hand

Joined: Dec 04, 2001
Posts: 50
I think we dont need to pass session between the apps on the same server because session is created first time around and later on we just need to manage the same among all the web apps.
You need to be careful about the objects you are storing in the session with same "keys" when you want to share session among multiple apps.

You dont have to code anything fancy other than taking the above care to share the session.
But managing session in multiple servlet contexts is difficult. Either you need to serialize sessions or store in db but there is always a possibility of single point of failure. I have never dealt with the issue.
You can create two apps with one servlet in each.
Try the above code and I'm sure it works.

Waiting for more postings....

Srini Admala
Ranch Hand

Joined: Dec 04, 2001
Posts: 50
Try the above code and I'm sure it works

I mean try something similar and I'm sure it works.
I'll try to get back with some code.......
I agree. Here's the link:
subject: Sharing session across servlet context
It's not a secret anymore!