330
Chapter 15: Using Persistent Data and Locking
Accessing and changing session variables
You use the same syntax to access a session variable as for other types of variables. However, you
must lock any code that accesses or changes session variables.
For example, to display the number of items in a user’s shopping cart, use favorite color that has
been set for a specific user, for example, use the following code:
<cflock timeout=20 scope="Session" type="Exclusive">
<cfoutput>
Your shopping cart has #Session.ShoppingCartItems# items.
</cfoutput>
</cflock>
To change increase the number of items in the shopping cart, use the following code:
<cflock timeout=20 scope="Session" type="Exclusive">
<cfset Session.ShoppingCartItems = Session.ShoppingCar 1>
</cflock>
Ending a session
If you use J2EE session management, the session and all session variables are deleted when the
user closes the browser. If you use ColdFusion session management and do not explicitly
terminate a session, for example when a user logs out, the session variables remain in ColdFusion
server memory until the session time-out period elapses.
To explicitly end a session, you can delete the session variables, or you can clear the session scope,
as follows:
<cfset StructClear(Session)>
If you use J2EE session management, you can also invalidate the session, as follows:
<cfset getPageContext().getSession().invalidate()>
This line creates a pointer to the servlet page context and calls an internal method to reset the
session.
Configuring and using application variables
Application variables are available to all pages within an application, that is, pages that have the
same application name. Because application variables are persistent, you easily can pass values
between pages. You can use application variables for information including the application name,
background color, data source names, or contact information.
You set the application name in the
cfapplication
tag, normally on your application’s
Application.cfm page. The application name is stored in the
Application.applicationName
variable.
Unlike client and session variables, application variables do not require that a client name (client
ID) be associated with them. They are available to any clients that use pages in the application.
Caution:
To preserve data integrity, put code that uses application variables inside
cflock
tags. For
information on using
cflock
tags see
“Locking code with cflock” on page 333
.
The following sections describe how to configure and use application variables.
Содержание ColdFusion MX
Страница 1: ...Developing ColdFusion MX Applications...
Страница 22: ...22 Contents...
Страница 38: ......
Страница 52: ...52 Chapter 2 Elements of CFML...
Страница 162: ......
Страница 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Страница 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Страница 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Страница 266: ......
Страница 314: ...314 Chapter 14 Handling Errors...
Страница 344: ...344 Chapter 15 Using Persistent Data and Locking...
Страница 349: ...About user security 349...
Страница 357: ...Security scenarios 357...
Страница 370: ...370 Chapter 16 Securing Applications...
Страница 388: ...388 Chapter 17 Developing Globalized Applications...
Страница 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Страница 410: ......
Страница 426: ...426 Chapter 19 Introduction to Databases and SQL...
Страница 476: ...476 Chapter 22 Using Query of Queries...
Страница 534: ...534 Chapter 24 Building a Search Interface...
Страница 556: ...556 Chapter 25 Using Verity Search Expressions...
Страница 558: ......
Страница 582: ...582 Chapter 26 Retrieving and Formatting Data...
Страница 668: ......
Страница 734: ...734 Chapter 32 Using Web Services...
Страница 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Страница 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Страница 788: ......
Страница 806: ...806 Chapter 35 Sending and Receiving E Mail...