1 / 55

Using the Application Framework

Using the Application Framework. Seree Chinodom Department of Computer Science. Application Framework. The ColdFusion Web Application Framework is a powerful tool you can use to help structure your ColdFusion applications. The Application.cfm Template.

barbie
Télécharger la présentation

Using the Application Framework

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using the Application Framework Seree Chinodom Department of Computer Science

  2. Application Framework • The ColdFusion Web Application Framework is a powerful tool you can use to help structure your ColdFusion applications.

  3. The Application.cfm Template • To use the ColdFusion Web Application Framework, you must first create the Application.cfm file • Place in the root directory of the application. • All the other templates in the application are stored in directories below the application's root directory.

  4. Four basic components: • Application-level settings and functions • Client state management • Custom error handling • Web server security integration

  5. OnRequestEnd.cfm • which is executed after each application page in the same application. • Place in the same directory as the Application.cfm file of the current application page. • The OnRequestEnd.cfm file will never be executed if it resides in another directory. • Not be executed if there is an error or an exception in the called page, or if the called page executes the CFABORT or CFEXIT tag.

  6. Creating the Application.cfm File • The application name • Client state management options • Application and session variables • Default variables • Custom error pages • Data sources • Default style settings • Exclusive locks • Other application-level constants

  7. Naming the application • Define an application by giving it a name using the CFAPPLICATION tag. • By using the same application name in a CFAPPLICATION tag, you define a set of pages as being part of the same logical application.

  8. Syntax <CFAPPLICATION NAME="Name" CLIENTMANAGEMENT="Yes/No" CLIENTSTORAGE="Storage Type" SETCLIENTCOOKIES="Yes/No" SESSIONMANAGEMENT="Yes/No" SESSIONTIMEOUT=#CreateTimeSpan(days, hours, minutes, seconds)# APPLICATIONTIMEOUT=#CreateTimeSpan(days, hours, minutes, seconds)# SETDOMAINCOOKIES="Yes/No" >

  9. Setting up client state management options • If you want to enable client state management, you must do so on every page in an application. • Because the Application.cfm file is included in all of the application's pages, you enable client management in the CFAPPLICATION tag, at the beginning of Application.cfm. <!--- Name the application and enable client management---> <CFAPPLICATION NAME="SearchApp" CLIENTMANAGEMENT="Yes">

  10. Enable client variable storage method • Use the CLIENTSTORAGE attribute in the CFAPPLICATION tag to specify where you want to store client variables, providing one of three values: • Registry • The name of a configured client store • Cookie • <!--- Name the application and enable client management---> <CFAPPLICATION NAME="SearchApp" CLIENTMANAGEMENT="Yes" CLIENTSTORAGE="mydatasource">

  11. Cookie storage • When you set CLIENTSTORAGE="Cookie" the cookie that ColdFusion creates has the application's name. • Storing client data in a cookie is scalable to large numbers of clients, but this storage mechanism has some limitations. • Chief among them is that if the client turns off cookies in the browser, client variables won't work.

  12. Using Client State Management • When client state management is enabled for an application, you can use the system to keep track of any number of variables associated with a particular client.

  13. Creating a client variable • To create a client variable and set the value of the parameter, use the CFSET or CFPARAM tag., for example: <CFSET Client.FavoriteColor="Red"> or <CFPARAM NAME="Client.FavoriteColor" DEFAULT="Red">

  14. Using Client Variables • A client variable is accessed using the same syntax as other types of variables, and can be used anywhere other ColdFusion variables are used. • To display the favorite color that has been set for a specific user, use the following code: <CFOUTPUT> Your favorite color is #Client.FavoriteColor#. </CFOUTPUT>

  15. Standard client variables • shows the date of a user's last visit to your site: <CFOUTPUT> Welcome back to the Web SuperShop. Your last visit was on #DateFormat(Client.LastVisit)#. </CFOUTPUT> • The standard Client object attributes are read-only (they can be accessed but not set by your application) and include CFID, CFToken, URLToken, HitCount, TimeCreated, and LastVisit.

  16. Getting a list of client variables • To obtain a list of the custom client parameters associated with a particular client, use the GetClientVariablesList function. <CFOUTPUT>#GetClientVariablesList</CFOUTPUT>

  17. Deleting client variables • Unlike normal variables, client variables and their values persist over time. • To delete a client variable, use the DeleteClientVariable function. For example: <CFSET IsDeleteSuccessful=DeleteClientVariable("MyClientVariable")>

  18. Deleting All client variables <CFLOOP INDEX = “x” LIST =“#GetClientVariableList()#”> <CFSET Deleted=DeleteClientVariabl (”#x#")> </CFLOOP>

  19. Deleting client Cookies <CFCOOKIE NAME = “cfid” EXPIRES =“NOW”> <CFCOOKIE NAME = “cftoken EXPIRES =“NOW”> <CFCOOKIE NAME = “cfglobal EXPIRES =“NOW”>

  20. Client variables with CFLOCATION behavior • When using CFLOCATION to redirect to a path that contains .DBM or .CFM, the Client.URLToken is automatically appended to the URL. • This behavior can be suppressed by adding the attribute ADDTOKEN="No" to the CFLOCATION tag. <CFLOCATION url =“home.cfm” ADDTOKEN="No" >

  21. Application and Session Variables • In ColdFusion, you use variables to work around the Web's inherent statelessness. Session and application variables are persistent variable "scopes." • You access these variables by prefacing the variable name with the scope name, for example: Session.MyVariable or Application.MyVariable. • And you can pass values between pages with a minimum of effort.

  22. Enabling application and session variables • Session and application variables are similar in operation to client variables. Like client variables, they are enabled with the CFAPPLICATION tag. However, unlike client variables, which are stored in the system registry, a data source, or a cookie, application and session variables are always stored in the ColdFusion server's memory. • This method offers obvious performance advantages. In addition, you can set time-out values for these variables either with CFAPPLICATION, or by specifying time-outs in the ColdFusion Administrator. • You can also simply disable application and session variables entirely.

  23. Example <CFAPPLICATION NAME = “MyApp” SESSIONMANAGEMENT=“Yes” SETCLIENTCOOKIES =“Yes” SESSIONTIMEOUT = “#CreateTimeSpan(0,0,20,0)#”>

  24. Using Session Variables • Use session variables when the variables are needed for a single site visit or set of requests. For example, you might use session variables to store a user's selections in a shopping cart application. (Use client variables when the variable is needed for future visits.)

  25. What is a session? • A session refers to all the connections that a single client might make to a server in the course of viewing any pages associated with a given application. • Sessions are specific to individual users. • As a result, every user has a separate session and has access to a separate set of session variables.

  26. Storing session data in session variables • Session variables are designed to store session-level data. They are a convenient place to store information that all pages of your application might need during a user session. • Using session variables, an application could initialize itself with user-specific data the first time a user hit a page of that application. This information could then remain available while that user continues to use that application.

  27. <!--- This example illustrates CFAPPLICATION ---> <!--- Name the application, and turn on session management ---> <CFAPPLICATION NAME="GetLeadApp" SESSIONMANAGEMENT="Yes"> <!--- set data source for this application ---> <CFSET dsn = "my_dsn"> <!--- set global error handling for this application ---> <CFERROR TYPE="REQUEST" TEMPLATE="request_err.cfm" MAILTO="webmaster@mysite.com"> <CFERROR TYPE="VALIDATION" TEMPLATE="val_err.cfm" MAILTO="webmaster@mysite.com"> <!--- set some global variables for this application to be triggered on every page ---> <CFSET MainPage = "default.cfm"> <CFSET session.current_location = "Davis, Porter, Alewife"> <CFSET sm_location = "dpa"> <CFSET current_page = "#cgi.path_info#?#cgi.query_string#">

  28. Create Session Variables • Use the CFSET or CFPARA tags • Scope variable name with the prefix SESSION <CFLOCK TIMEOUT =“30” SCOPE =“session” TYPE =“Exclusive”> <CFPARAM NAME =“SESSION.user_name” DEFAULT=“#FORM.user_name#”> </CFLOCK> or <CFLOCK TIMEOUT =“30” SCOPE =“session” TYPE =“Exclusive”> <CFPARAM NAME =“SESSION.user_name” DEFAULT=“FORM.user_name”> </CFLOCK>

  29. Store array and record <CFQUERY NAME=“getStates” DATASOURCE=“mydb” > SELECT * FROM states </CFQUERY> <CFLOCK TIMEOUT =“30” SCOPE =“session” TYPE =“Exclusive”> <CFSET SESSION.query_results=Duplicate(getStates)> </CFLOCK>

  30. Referring to Session Variables <CFLOCK TIMEOUT =“30” SCOPE =“session” TYPE =“ReadOnly”> <CFIF NOT IsDefined(“SESSION.user_name”)> …Insert code here </CFIF> </CFLOCK> and <CFLOCK TIMEOUT =“30” SCOPE =“session” TYPE =“ReadOnly”> <CFOUTPUT>#SESSION.user_name#</CFOUTPUT> </CFLOCK>

  31. StructKeyArray • Returns an array of the keys in the specified ColdFusion structure. • Syntax StructKeyArray(structure)

  32. StructKeyList • Returns the list of keys that are in the specified ColdFusion structure. • Syntax StructKeyList(structure, [delimiter])

  33. IsStruct • Returns TRUE if variable is a structure. • Syntax IsStruct(variable )

  34. Putting it all together • Create and populate a structure • Display the number of element in the structure • display the contents of the structure while deleting each element • display the number of element in the structure to verify that it is empty

  35. Delete Session Variables • StructDelete() • StructClear()

  36. Example <!-- Inorder to delete a single session variable use the StructDelete() function ---> <CFLOCK YTIMEOUT =“30” SCOPE=“session” TYPE =“Exclusive”> <CFSET SESSION.test=“foo”> <CFOUTPUT>#SESSION.test#</CFOUTPUT> <CFSET StructDelete(SESSION,”test”> <CFIF IsDefined(“SESSION.test”> test exists <CFELES> delete </CFIF> </CFLOCK>

  37. <!-- Inorder to delete the entire session structure use the StructClear() function ---> <CFLOCK YTIMEOUT =“30” SCOPE=“session” TYPE =“Exclusive”> <CFSET StructClear(session)> </CFLOCK>

  38. Youmay want to delete all reference to a client session in the following instancees • When the user clicks a logout button • When the user close their browser • If the user leaves your Web site

  39. Ending a session when the browser is Closed • The following code can be used to reset the CFID and CFTOKEN cookies: <CFLOCK TIMEOUT =“30” SCOPE =“session” TYPE=“Exclusive”> <CFCOOKIES NAME =“CFID” VALUE “#SESSION.CFID#”> <CFCOOKIES NAME =“CFTOKEN” VALUE “#SESSION.CFTOKEN#”> <?CFLOCK>

  40. Ending a session when the User Logs out • When user log out , you can use the StructClear function to kill their session

  41. Managing Session Management and Cookies • Passing CFID and CFTOKEN variable to each template in application , you should append the session variable SESSION.URL token to any link that you create <!--- This code saves the session variables to a request scope variable inorder to minimize the amount of locks required throughout the template ---> <CFLOCK YTIMEOUT =“30” SCOPE=“session” TYPE =“Exclusive”> <CFSET REQUEST.URLTOKEN=SESSION.URLTOKEN> </CFLOCK>

  42. <!--- An example of passing REQUEST.URLTOKEN through a form ---> <FORM ACTION =“quiZ_results.cfm?#REQUEST.URLTOKEN#” METHOD=“post”> … </FORM> <!--- An example of passing REQUEST.URLTOKEN through a URL---> <A HREF=“quiz_results.cfm? ?#REQUEST.URLTOKEN#”>…</A> <!--- An example of passing REQUEST.URLTOKEN using JavaScript ---> <FORM > <INPUT TYPE =“button” VALUE =“Get New Question!” onClick =“location.href=‘home.cfm? ?#REQUEST.URLTOKEN#’ ”> </FORM>

  43. Using Application Variables • Enable Application variables <CFAPPLICATION NAME=“MyApp” APPLICATIONTIMEOUT=“#CreateTimeSpan(2,0,0,0)#”>

  44. Working with Application Variables • Creating Application Variables <CFLOCK TIMEOUT =“30” SCOPE=“application” TYPE =“Exclusive”> <CFPARA NAME=“APPLICATION.title” DEFAULT =“My Quiz Application”> </CFLOCK> OR <CFLOCK TIMEOUT =“30” SCOPE=“application” TYPE =“Exclusive”> <CFSET APPLICATION.title==“My Quiz Application”> </CFLOCK>

  45. We can store arrays and query recordsets in application variables. <CFLOCK TIMEOUT =“30” SCOPE=“application” TYPE =“Exclusive”> <CFSET APPLICATION.query_results=duplicate(Name_of_Query)> </CFLOCK>

  46. Referring to Application Variables <CFLOCK TIMEOUT =“30” SCOPE=“application” TYPE =“ReadOnly”> <CFIF NOT IsDefined (“APPLICATION.title”)> …insert code here </CFIF> </CFLOCK> and <CFLOCK TIMEOUT =“30” SCOPE=“application” TYPE =“ReadOnly”> <CFOUTPUT> <HEAD><TITLE>#APPLICATION.Title#</TITLE></HEAD> </CFOUTPUT> </CFLOCK>

  47. Retrieving a List of Application Variables <CFLOCK TIMEOUT =“30” SCOPE=“application” TYPE =“ReadOnly”> <CFLOOP COLLECTION =“#application# ITEM=“x”> <CFOUTPUT> key = #x# (value =#application[x]#<BR> </CFOUTPUT> </CFLOOP> </CFLOCK>

  48. Retrieving a List of Application Variables If variable contain complex structures such as queries array and COM/DCOM object . Use this code : <CFLOCK TIMEOUT =“30” SCOPE=“application” TYPE =“ReadOnly”> <CFLOOP COLLECTION =“#application# ITEM=“x”> <CFOUTPUT> key = #x# <BR> </CFOUTPUT> </CFLOOP> </CFLOCK>

  49. Deleting the Application Structure <!-- Inorder to delete a single application variable use the StructDelete() function ---> <CFLOCK YTIMEOUT =“30” SCOPE=“application” TYPE =“Exclusive”> <CFSET APPLICATION.title=“My quiz Application”> # APPLICATION.title # <CFSET StructDelete(APPLICATION,”title”> <CFIF IsDefined(“APPLICATION.title”> still exists <CFELES> deleted </CFIF> </CFLOCK>

More Related