This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Can two different web application share a variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can two different web application share a variable" Watch "Can two different web application share a variable" New topic
Author

Can two different web application share a variable

Jagmohan Negi
Greenhorn

Joined: Aug 24, 2005
Posts: 27
Can two different web application share a variable with the help of Servlets or JSPs
Daniel Rhoades
Ranch Hand

Joined: Jun 30, 2004
Posts: 186
Using JNDI + RMI you can access objects in other apps, a stateless session bean might work well for you.


Drinking more tea is the key...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41137
    
  45
Different web apps are usually loaded using different ClassLoaders, which means that static fields will not be the same across applications. The servlet spec also has no provisions for sharing data between web apps; they are meant to be separate. You could use a shared database, of course.


Ping & DNS - my free Android networking tools app
Jagmohan Negi
Greenhorn

Joined: Aug 24, 2005
Posts: 27
can i have the code example for that
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Jagmohan Negi:
can i have the code example for that


No.

Could you please try to do something on your own. You have got the ideas. Now just try to implement that. Having a shared DB is an easy option. Where is the problem??

Lets get your hands dirty with the code. We will here to help you in getting a clean code.

Thanks.
[ August 24, 2005: Message edited by: Adeel Ansari ]
Vicky Pandya
Ranch Hand

Joined: Dec 16, 2004
Posts: 148
or you can use shared cache or messaging is also an option.
Iris Hoekstra
Greenhorn

Joined: Aug 10, 2005
Posts: 29
You can fire requests from one webapp to another. You might define the variable in one webapp and pass it as an attribute of the request to the other.
Ola Daniel
Ranch Hand

Joined: Jul 27, 2005
Posts: 105
... and pass it as an attribute of the request to the other.

You cannot pass(or forward) a request attribute from one web app to another. You could however send a parameter and use response.sendRedirect to get it across to the second app.

Using a shared cache is what I would recommend.


SCJP 1.4, SCWCD 1.4
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler et al, Refactoring: Improving the Design of Existing Code, 1999
lexander Bosco
Ranch Hand

Joined: Feb 21, 2005
Posts: 65
just like OLA has said response.sendRedirect() should do what u want.


there is no knowledge that is not power<br />-<br />SCJP 1.4<br />SCWCD in progress<br />SCMAD in progress
Iris Hoekstra
Greenhorn

Joined: Aug 10, 2005
Posts: 29
Originally posted by Ola Daniel:
[QB]
You cannot pass(or forward) a request attribute from one web app to another.


Yes, you can. For example, in the doGet or doPost of a servlet in app A you might do:

lexander Bosco
Ranch Hand

Joined: Feb 21, 2005
Posts: 65
Iris have u actually tried this?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You will probably need to relax the settings for your app server to do this.
With Tomcat it's done via the "crossContext" attribute of the Contex attribute in server.xml (or in the context xml fragment file).

http://jakarta.apache.org/tomcat/tomcat-5.5-doc/config/context.html


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Iris Hoekstra
Greenhorn

Joined: Aug 10, 2005
Posts: 29
Yes, I have tried this, works fine. I don't see why it wouldn't. A HttpRequest is a HttpRequest, and you can add attributes to it. Simple as that, as far as I know it has the same functionality no matter where you forward it to. I'm not sure about the crossContext thingy Ben mentions, at work I am not the one who configures the servers.
Ola Daniel
Ranch Hand

Joined: Jul 27, 2005
Posts: 105
originally posted by Iris Hoekstra
A HttpRequest is a HttpRequest, and you can add attributes to it....I'm not sure about the crossContext thingy Ben mentions, at work I am not the one who configures the servers.


I maintain you cannot forward request objects across two servlet contexts(web apps)...

unless of course you set the crossContext attribute like Ben stated above.
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Technically you can share objects between different web applications in most cases if the web applications are packaged within the same ear on your application server. There would have to be some sort of intermediary object/mechanism.

Keep in mind this is somewhat app server dependent, but something like the following would work, even though I should add I wouldn't necessarily do it. If I wanted to keep track of the total number of hits between multiple webapps in the same ear, I might have a class with a static variable hits:



Now if we included this class in each WAR, it won't work as intended. Each WAR has its own classloader so basically each webapp will have its own hit counter and won't be recording or accessing hit counts from the other webapps. If however you package the class in a JAR which is packaged in the ear, and you set the Manifest of each WAR to include that JAR in its classpath, then the HitCounter will work as intended, since the JAR's classloader will be a parent to the WAR classloaders.

When app servers try to load a class, they typically first try to load it using the classloader's parent classloader. So when webapp A accesses HitCounter, it will first try to load it using its parent classloader, which is the classloader that would load the jar. When webapp B accesses HitCounter it will first try to load it using the parent to the classloader for webapp B, which is the classloader for the jar and is the same classloader that webapp A used to load HitCounter, which allows it to work as intended.

This isn't clean or even something you necessarily should do, but it will work in most circumstances. I guess the bottom line is that you can't share data without using an intermediary, be it something like the example above, going through RMI and JNDI, a session bean, a cookie, or whatever.
[ August 25, 2005: Message edited by: Jason Menard ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Tomcat doesn't recognize ear files but you can achieve the exact same results by putting the static variable in a class who's file is located in TOMCAT/common/classes or jared and put into TOMCAT/common/lib.

Or TOMCAT/shared/classes, TOMCAT/shared/lib respectively.

As Jason said, it's not a clean solution.
The servlet spec came out with the notion of self contained web applications in servlet spec 2.2.
Ignore this principal at your own peril.
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

If I were you, then I used a cookie for this purpose.


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Can two different web application share a variable
 
Similar Threads
sharing session among two servers in a distributed application
how to share data between servlet in different webapplication
how to find session btn two web appl
how to pass session to different web pages
Sharing Session Between Different Web apps