742
Chapter 33: Integrating J2EE and Java Elements in CFML Applications
Accessing ColdFusion application and session variables in JSP pages
ColdFusion MX runs as a J2EE application on the J2EE application server. The J2EE application
ServletContext is a data structure that stores objects as attributes. A ColdFusion Application
scope is represented as an attribute named by the Application scope name. The attribute contains
the scope values as a hash table. Therefore, you access ColdFusion Application scope variable in a
JSP page or servlet using the following format:
((Map)application.getAttribute("
CFApplicationName
"))).get("
appVarName
")
Similarly, the ColdFusion Session scope is a structure within the J2EE session. Because
ColdFusion MX identifies sessions by the application name. the session structure is contained in
an attribute of the J2EE session that is identified by the application name. Therefore, you access
ColdFusion session variables as follows:
((Map)(session.getAttribute("
CFApplicationName
"))).get("
sessionVarName
")
Unnamed ColdFusion Application and Session scopes
If you do not specify an application name in the ColdFusion
cfapplication
tag, the application
is unnamed. ColdFusion MX supports only a single unnamed application, so if multiple
cfapplication
tags do not specify an application name, all pages affected by the tags share the
single unnamed application Scope. This scope maps directly to the J2EE application scope.
Similarly, all sessions of unnamed applications correspond directly to the J2EE application server’s
session scope.
You access an Application scope variable from a ColdFusion unnamed application in a JSP page
using the following format:
application.getAttribute("applicationV
ariableName
")
You access Session scope variables in a ColdFusion unnamed application as follows:
session.getAttribute("
sessionVariableName
")
Note:
When you use application and session variables for the unnamed ColdFusion application in
JSP pages and servlets, the variable names must be case-correct. That is, the characters in the
variable name must have the same case as you used when you created the variable in ColdFusion.
You do not have to use case-correct application and session variable names for named ColdFusion
applications.
Examples: using JSP with CFML
The following simple examples show how you can integrate JSP pages, servlets, and ColdFusion
pages. They also show how you can use the Request, Application, and Session scopes to share data
between ColdFusion pages, JSP pages, and servlets.
Calling a JSP page from a ColdFusion page
The following page sets Request, Session, and application variables and calls a JSP page, passing it
a name parameter:
<cfapplication name="myApp" sessionmanagement="yes">
<cfscript>
Request.myVariable = "This";
Session.myVariable = "is a";
Application.myVariable = "test.";
GetPageContext().include("hello.jsp?name=Bobby");
</cfscript>
Summary of Contents for COLDFUSION MX 61-DEVELOPING COLDFUSION MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......