Solved

The difference between application scope and request scope in coldfsuion

Posted on 2014-09-09
5
220 Views
Last Modified: 2014-09-16
What is the difference between application scope and request scope in coldfsuion . Please show me all pros and cons. I read Adobe site but still not clear
0
Comment
Question by:erikTsomik
  • 3
  • 2
5 Comments
 
LVL 39

Expert Comment

by:gdemaria
ID: 40312450
The application scope is a shared scope that persists until the application timeout (similar to session timeout).   All users will see the same value.  If user A changes the value, user b will see the change.   The best use for application scope is to define global variables such as the datasource for your query, that is set once for everyone and never changed.

The request scope is a global variable that does not persist.   It disappears after the page is loaded.   The scope is only for each user and the only for the one request.     So you could set the value at top of the page and it will persist until the page is finished.   A good use for this is to set the test/debug mode for example.    If you want one user to be in test mode and see some things no one else will see, set their variable  request.mode = "test" and all CF templates and cffunctions can see this variable and know to do something different depending on test or live.... just an example
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 40312603
Thank you . Also often times I do see that the datasource is being declared in a request scope. Is that a right approach to take or the application scope is the best thing to do.

Also if the request scope persist only one request can then variables   be declares in the variable scope or local scope?
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 40312628
it is fine to define the datasource in the request scope, you just need to do that onRequestStart which will set the variable every time for every user.   Seems a bit more efficient to set it once for all users.   But if it will ever change or varies by user, then request is the better option as application scope is one variable for everyone and should not change.

> Also if the request scope persist only one request can then variables   be declares in the variable scope or local scope?
Not sure I understand the question, if I don't answer it right, feel free to ask again.

The variables scope should be for local use variables and these variables really should not be used from one template to another (that is, you should not set  variables.FirstName in one CF template and then exect to use it in another template).   That is very possible just not the best coding practice in my opinion.   Use request scope sparingly, only when you have a variable that you want to persist throughout the entire application.   One thing that I do is to fetch the session.user_ID from the login and assign it immediately into request.user_id the global variable.  I do this in application.cfc because I would rather refererence request.user_id everyone instead of the session variable everywhere.  That is one use of a global variable that may change on each page load (the user may logout).
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 40312684
It makes my understanding a little bit better . Can you reference a few more examples of how and where to use request scope. And the benefits over application scope variable?
0
 
LVL 39

Accepted Solution

by:
gdemaria earned 500 total points
ID: 40312744
Well, the use of application scope is very limited.   You would not want to use it anytime when a value may change often and you would not want to use it when one user would have a different value from another user.    For example, if you used application.user_id to track someone's login, the value would change every time another person logged in and it would log off every user except one.   There is only ONE instance of the value and that value is shared by everyone, so there are not a lot of things you do with it.  You could set a variable to defined the path to your image folder, because that path would be the same for all users and would never change.

Request scope is very different.   The value exists for only one user and ONE request (page load).  Then the value is gone.   So it is the global variable you typically think of.   But using a global variable too often is not good!   Since the value of the request scope does not persist between page loads, you cannot use it to maintain a login, or to keep track of a shopping cart - unless you set the value EVERYTIME a page is requested.     I don't use request scope a lot because I don't think it's very good programming to define global variable, it's much better to pass variables such as passing to a cffunction.   But I think it can be used for some things such as "switches" or user preferences.   For example, in the application.cfc file you could fetch the users' preferences from the database and load them into request scope variables..    Which color scheme does the user want for their page?   What language do they use?   What privileges does the user have?  These type of settings are global, can vary by user and may change because the user changes their minds and wants a different color or may be granted different priviledges..
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Stream live video from Raspberry Pi camera 22 264
coldfusion cfloop help 6 36
coldfusion upload spreadsheet into the databse 2 58
Standalone trial or freeware to do SSL scan 4 61
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question