This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "passing objects between 2 servlets" Watch "passing objects between 2 servlets" New topic
Author

passing objects between 2 servlets

georgejk
Greenhorn

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.
Regards
George
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.
Brian
David O'Meara
Rancher

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.
Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"georgejk",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp.
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.
Thanks.
Dave
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: passing objects between 2 servlets