The ColdFusion markup language (CFML) has a reputation for being an easy scripting language to learn. The ColdFusion tag-based programming model allows for rapid Web development, and the inherent simplicity of this model makes Internet application development possible for a wider population of developers.
In this article, we show how Macromedia ColdFusion MX for IBM WebSphere Application Server (Application Server) can share session variables between ColdFusion and J2EE components. We show the synergy achieved by using these two powerful products in tandem.
Setting up ColdFusion to work with Application Server
This section shows how to configure ColdFusion MX to share session data with Application Server.
Enabling session memory variables using the ColdFusion MX administrator interface
Before starting, ensure that ColdFusion MX for IBM WebSphere Application Server is running.
- Log in to the ColdFusion MX Administrator interface by either:
- Selecting Start > Programs > Macromedia ColdFusion MX J2EE > ColdFusion MX Administrator, or
- Opening a Web browser and going to
http://<server:port>/<cfmx_contextroot>/CFIDE/administrator/index.cfm
For example,http://localhost/cfmx/CFIDE/administrator/index.cfm
- From the ColdFusion MX Administrator interface, select Memory Variables from the main menu server settings, as shown in Figure 1.
Figure 1. Selecting memory variables from server settings
- On the next panel, make sure both Use J2EE session variables and Enable Session Variables options are checked, as shown in Figure 2. Click Submit Changes to apply the modification.
Figure 2. Enabling J2EE session variables
- Restart the ColdFusion MX for IBM WebSphere Application Server.
Congratulations, you have successfully enabled J2EE session management.
In this section, we'll walk through two sample "Login" applications. The first application, named CFMXSESSIONA, shows how a Java developer would use session objects created in ColdFusion. The second, named CFMXSESSIONB, shows how a ColdFusion developer would use session objects created in Java.
Enabling session management for the ColdFusion MX application
In CFMXSESSIONA, ColdFusion needs to create and manage session objects. ColdFusion uses a <cfapplication> tag, within a file named Application.cfm (in the root directory of the Web application under the CFMX root directory), to define the application's settings, as shown in Figure 3.
Figure 3. APPLICATION.CFM file in the root directory of the Web application
The NAME attribute defines the ColdFusion MX application name, as seen in Listing 1 below. This is a required attribute to use session variables.
Listing 1. CFAPPLICATION's NAME attribute
<cfapplication name="CFMXSESSIONA" clientmanagement="no" sessionmanagement="yes" sessiontimeout="#createtimespan(0,0,15,0)#"> |
Next, we enable the use of session variables within your ColdFusion MX application by setting the sessionmanagement attribute to YES, as shown in Listing 2.
Listing 2. Enabling the ColdFusion MX application's session management
<cfapplication name="CFMXSESSIONA" clientmanagement="no" sessionmanagement="yes" sessiontimeout="#createtimespan(0,0,15,0)#"> |
Optionally, as we did in our sample application, you can override the default session timeout interval defined in the ColdFusion Administration interface by explicitly specifying sessiontimeout with a desired time interval (see Listing 2). We used the ColdFusion function createtimespan to create the desired time interval in the format of (Days, Hours, Minutes, Seconds).
This section shows both ColdFusion and J2EE developers the necessary steps to create session objects that are easily shared between the two technologies, depending on which technology will be responsible for session management.
Creating sessions in ColdFusion MX
First, in a ColdFusion page called LoginAction.cfm, we define the session as a ColdFusion struct collection object to store a set of properties, as shown in Listing 3.
Listing 3. Creating a session structure to store properties
<!--- Create Session Structure ---> <cfset SESSION = StructNew()> <!-- Define Variables within the Session Structure --> <cfset SESSION.SESSIONCREATEDBY = "ColdFusion"> <cfset SESSION.ISAUTHENTICATED = "Y"> <cfset SESSION.USERID = selectUser.userid> <cfset SESSION.USERNAME = selectUser.username> <cfset SESSION.ROLE = selectUser.role> |
Next, we add properties such as userid to the newly defined session collection, as seen in Listing 4.
Listing 4. Creating properties in the session structure
<!--- Create Session Structure ---> <cfset SESSION = StructNew()> <!-- Define Variables within the Session Structure --> <cfset SESSION.SESSIONCREATEDBY = "ColdFusion"> <cfset SESSION.ISAUTHENTICATED = "Y"> <cfset SESSION.USERID = selectUser.userid> <cfset SESSION.USERNAME = selectUser.username> <cfset SESSION.ROLE = selectUser.role> |
Now we'll do what we did in the previous section, but this time from a J2EE developer's perspective. To create session objects in Java that translate easily into ColdFusion, we put the properties into a java.util.Map structure in a file called LoginAction.jsp, as shown in Listing 5.
Listing 5. Creating properties in a java.util.Map structure
// Define Variables within a java.util.Map
map.put("SESSIONCREATEDBY","JAVA");
map.put("ISAUTHENTICATED","Y");
map.put("USERID",Integet.toString(resultSet.getInt("USERID")));
map.put("USERNAME",resultSet.getString("USERNAME"));
map.put("ROLE",Integer.toString(resultSet.getInt("ROLE")));
|
We store the Map object as CFMXSESSIONB into the session, as shown in Listing 6.
Listing 6. Storing the java.util.Map structure into the session
// Store the Map in the HttpSession
session.setAttribute("CFMXSESSIONB",map);
|
Session variable data type translation
ColdFusion MX for Application Server lets J2EE and ColdFusion developers map data types between the two technologies, resulting in an easy exchange of information. Table 1 shows a sampling of common data types. For more detailed information, see the Macromedia ColdFusion MX documentation (see Resources).
Table 1. Data type mappings between ColdFusion and Java
| ColdFusion data type | Java data type |
| ARRAY | java.util.Vector |
| DATE | java.util.Date |
| NUMBER | java.lang.Double (default) |
| RECORDSET | java.sql.ResultSet |
| STRING | java.lang.String |
| STRUCT | java.util.Map |
Now that we've created the session objects in both Java and ColdFusion technologies, we want to share the session's contents in a cross-pollination type methodology. The seamless interaction between the two technologies through the sharing of data objects, such as those contained in the session, lets skilled ColdFusion and J2EE developers work in tandem on a single, integrated solution.
Accessing ColdFusion MX-created session objects in Java
In Creating sessions in ColdFusion MX, we defined a ColdFusion struct collection object to store our properties. In Java, you can interpret the ColdFusion struct object as a java.util.Map (see Table 1 for more detail). Accordingly, Map is the data type we'll use to access the session structure's contents.
ColdFusion registers the session structure as the name attribute value defined in the <CFAPPLICATION> tag (see Listing 1). Therefore, we can access the ColdFusion-created session objects in a Java page, in our case in DisplaySession.jsp, by invoking the standard getAttribute method and casting the returned object as a java.util.Map, as shown in Listing 7.
Listing 7. Get the CFMX session object and cast it as a java.util.Map
//////////////////////////////////////////////////////////////
// Get the CFMX Created Session Object
//
// The CF Session is automatically named as the CFAPPLICATION's
// NAME attribute value
/////////////////////////////////////////////////////////////
map = (java.util.Map) session.getAttribute("CFMXSESSIONA");
|
Because the session's contents are stored as a java.util.Map structure, we can invoke the standard get method to access its values, as seen in Listing 8.
Listing 8. Access the values with the get method
<%-- Display session variables --%>
<tr>
<td align="right" class="label" width="250">USERID</td>
<td width="300"><%= map.get("USERID") %></td>
</tr>
<tr>
<td align="right" class="label">USERNAME</td>
<td><%= map.get("USERNAME")%></td>
</tr>
<tr>
<td align="right" class="label">ROLE:</td>
<td><%= map.get("ROLE")%></td>
</tr>
<tr>
<td align="right" class="label">SESSION CREATED IN:</td>
<td><b><%= map.get("SESSIONCREATEDBY")%></b></td>
</tr>
|
Accessing Java-created session objects in ColdFusion MX
Now that we have shared from ColdFusion MX to Java, let's take a gander at the other way around. In this section we'll access the session objects created in Java in a ColdFusion page called DisplaySession.cfm. ColdFusion developers can create "Java" objects within a ColdFusion page by using the createObject function. We will need to create three Java objects, as shown in Listing 9.
Listing 9. Create the necessary Java objects
<!-- Create Java objects within CF --->
<cfset REQ=createObject("Java","javax.servlet.http.
HttpServletRequest")>
<cfset SES=createObject("Java","javax.servlet.http.HttpSession")>
<cfset MAP=createObject("Java","java.util.Map")>
|
Now, we must get the session that is a java.servlet.http.HttpSession object passed in the javax.servlet.http.HttpServletRequest object. Once this is complete, we can access the session objects by invoking the session's getAttribute method, as shown in Listing 10.
Listing 10. Java methods to access the session object
<!--- Get the HttpServletRequest --->
<cfset REQ = GetPageContext().getRequest()>
<!--- Get the HttpSession --->
<cfset SES = req.getSession()>
<!--- Get the CFMX session object (java.util.Map) --->
<cfset MAP = ses.getAttribute("CFMXSESSIONB")>
|
Because ColdFusion can interpret the java.util.Map structure, we can display the session values by using the standard ColdFusion variable output convention, as seen in Listing 11 below.
Listing 11. Accessing values through the standard ColdFusion convention
<!--- Display Session Variables --->
<tr>
<td align="right" class="label" width="250">USERID</td>
<td width="300">#MAP.USERID#</td>
</td>
<tr>
<td align="right" class="label">USERNAME</td>
<td width="300">#MAP.USERNAME#</td>
</tr>
<tr>
<td align="right" class="label">ROLE:</td>
<td width="300">#MAP.ROLE#</td>
</tr>
<tr>
<td align="right" class="label">SESSION CREATED IN:</td>
<td width="300"><b>#MAP.SESSIONCREATEDBY#</b></td>
</tr>
|
The download package contains a simple ColdFusion MX for J2EE application using shared session variables between ColdFusion and Java components. Please read the readme.doc file for complete setup instructions.
Figure 4 gives you a peek at our application in action. Here, we have successfully grabbed Java session data in a ColdFusion MX page.Figure 4. The sample application
In the past, the J2EE-centric world of WebSphere Application Server and ColdFusion weren't compatible. This was a sad reality, as both software offerings have a lot to offer to the developer and their projects. In this article, we showed how you can share session data between ColdFusion and your WebSphere J2EE software artifacts. Such sharing of session data lets you tap into the best of both worlds to formulate powerful Web applications with the speed provided by CFML, while not compromising the extensive J2EE capabilities of IBM WebSphere Application Server.
| Name | Size | Download method |
|---|---|---|
| i-coldstudio.zip | HTTP |
Information about download methods
- Participate in the discussion forum.
- Download the sample application for a simple ColdFusion MX for J2EE application using shared session variables.
- Get an overview and resources to help you get started with ColdFusion MX for WebSphere.
- For detailed information on data types, see the Macromedia ColdFusion MX documentation.
- Read more of Kulvir's articles.
- Download a trial version of ColdFusion MX for WebSphere.
- Find articles, tutorials, and downloads for developers at WebSphere Developer Domain.
Kwang S. Kang, consultant for IBM's Software Services for WebSphere, has extensive experience providing enterprise solutions on IBM's WebSphere and Macromedia's Cold Fusion platforms. You can reach Kwang at kkwang@us.ibm.com.

Kulvir Singh Bhogal works as an IBM consultant, devising and implementing Java-centric solutions at customer sites across the nation. You can reach Kulvir at kbhogal@us.ibm.com.

