my dog learned polymorphism
The moose likes Servlets and the fly likes passing objects between 2 servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "passing objects between 2 servlets" Watch "passing objects between 2 servlets" New topic

passing objects between 2 servlets


Joined: Jul 29, 2002
Posts: 24
Hi all..
I would like to clarify one doubt. I have one requirement to pass the file object which is created in one servlet to another servlet. But the probelm is, both these servlets are residing in different context path. So can anybody suggest an idea in which i can pass information from say servlet A to Servlet B.
Brian Nice
Ranch Hand

Joined: Nov 02, 2000
Posts: 195
If the servlets are on the same application server, you can store the object in the application context. All servlets, JSPs have access to this area.
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

In terms of security and separation of applications, this is a bad idea. The J2EE design of Servlet containers (and EJB containers) encourages them to be treated as completely separate applications, even though they may share the same JVM.
If you start sharing data between apps, you blur the edges between the applications. You can specify what data gets shared, but you can't decide who gets access to it.
If you are just doing this to share Servlets or access to a database, it is better to include the servlet in the other web app or make it read the data from the database itself rather than passing data between applications.
That said, you can reach from one context to another using getContext().getContext( new_context )
Have a look at javax.servlet.ServletContext to see the sort of things you can do from there, including reading and writing data to the other context. Notice that many of the methods are marked as Deprecated... with no direct replacement. This is an indication of the sort of security problems this behaviour has created.
The use of getContext( new_context ) is often poorly implemented anyway, and this should be reason enough to stay away.
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at
We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please edit your profile and select a new name which meets the requirements.
I agree. Here's the link:
subject: passing objects between 2 servlets
It's not a secret anymore!