• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can two different web application share a variable

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can two different web application share a variable with the help of Servlets or JSPs
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using JNDI + RMI you can access objects in other apps, a stateless session bean might work well for you.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jagmohan Negi
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can i have the code example for that
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or you can use shared cache or messaging is also an option.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

... 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.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just like OLA has said response.sendRedirect() should do what u want.
 
Iris Hoekstra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Iris have u actually tried this?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Iris Hoekstra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I were you, then I used a cookie for this purpose.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic