Search code examples
coldfusionvariables

How to set a server-wide variable in ColdFusion


I have to maintain a variable in my Application.(cfm|cfc) to set the environment which the application currently runs under, the environment being (development|test|production).

I'd like to set an environment variable on the server itself, so that I can read its value in the Application.cfm.

Is that possible?


Solution

  • Easiest is to set a OS environment variable (at the system level, or for the user ColdFusion runs under), and restart the service. The variable is then available in the CGI scope:

    <cfset EnvName = CGI.COLDFUSION_ENVIRONMENT>
    <cfoutput>#EnvName#</cfoutput>
    

    You could also use Java system properties. In your ColdFusion Administrator, go to "Server Settings/Java and JVM", and add something like this to the "JVM Arguments":

    -Dcom.mycompany.environment=development

    You can then ask for that value in ColdFusion:

    <cfset System  = CreateObject("java", "java.lang.System")>
    <cfset EnvName = System.getProperty("com.mycompany.environment")>
    <cfoutput>#EnvName#</cfoutput>
    

    You would have to restart the CF Service every time you make a change, but the value seems pretty static so this should not be a problem.