I have my own answer to this question, which I'll post, but I wanted to see if I missed a simpler way. I have two application's running on the same coldfusion server, and I want to access the application scope of one from the other. How might I do this?
UPDATE:
After Reading @Daniel and @Ben's answers, I went back and approached my problem from the standpoint of sub-applications which turned out to be a better solution to my initial problem. My answer is still the "quick and dirty" way to access other application scopes, but putting data in the server scope is a better practice.
I think you should probably think about why it is that you want to do this... Architecturally this doesn't seem very sound, even if it is possible. The Server scope would be better for resources that you want to share across applications.
You might even want to consider whether the two applications should actually be one single application with two small sub-applications.
I put my answer together from two sources. First, Ben Nadel's Massive Exploration of the ColdFusion PageContext object (Thanks Ben). Second, the ColdFusion help page on Interoperating with JSP pages and servlets. Put the two together and I get this:
Directory structure:
Root
|_ App1
|_ Application.cfc
|_ index.cfm
|_ App2
|_ Application.cfc
|_ index.cfm
App1/Application.cfc:
component
{
this.name="App1";
this.application.foo = "bar"
}
App2/Application.cfc:
component
{
this.name="App2"
}
App2/index.cfm
<cfscript>
writeDump(getPageContext().getFusionContext().getServletContext().getAttribute('App1'))
</cfscript>
After hitting the index.cfm in the App1 directory, you can see the application scope from app1 dumped in the index of app2.
I completely agree with @Daniel about the architecture.
@Ryan's answer is a good one.
I thought I would offer the alternative I thought of, which has a few advantages, and a few drawbacks.
Basically, any data to be shared between the apps can be written to the server scope. For example:
// In App 1
application.foo = "bar";
server.sharedData.app1.fpp = "bar";
// In App 2
application.bar = "foo";
server.sharedData.app2.bar = "foo";
// Use shared data from App 1
writeOutput(server.sharedData.app1.foo);
Advantages:
Disadvantages
Anyway, this was my first thought but after reading @Ryan's answer, I'd probaly just write a UDF that takes an application name and a var name, and use it as a facade for what he did.
But seriously, consider whether sharing data across appications is smarter/wiser than merging them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With