Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Together at last: Sharing session data between ColdFusion and J2EE components

Fuse disparate worlds with Macromedia ColdFusion MX for IBM WebSphere

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.
Photo of author
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.

Summary:  In the past, combining the benefits of ColdFusion and WebSphere was not really a fathomable option. Things have changed, though, with ColdFusion MX. This article shows developers how to deploy their ColdFusion code into a WebSphere Application Server J2EE environment.

Date:  01 May 2003
Level:  Intermediate
Also available in:   Japanese

Activity:  6174 views
Comments:  

Introduction

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.

  1. 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
  2. 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

  3. 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

  4. Restart the ColdFusion MX for IBM WebSphere Application Server.

Congratulations, you have successfully enabled J2EE session management.


Application development

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).


Creating the session objects

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>

			

Creating sessions in Java

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
ARRAYjava.util.Vector
DATEjava.util.Date
NUMBERjava.lang.Double (default)
RECORDSETjava.sql.ResultSet
STRINGjava.lang.String
STRUCTjava.util.Map


Accessing session variables

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> 


Sample application

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


Conclusion

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.



Download

NameSizeDownload method
i-coldstudio.zip HTTP

Information about download methods


Resources

About the authors

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.

Photo of author

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.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Sample IT projects
ArticleID=10268
ArticleTitle=Together at last: Sharing session data between ColdFusion and J2EE components
publish-date=05012003
author1-email=kkwang@us.ibm.com
author1-email-cc=
author2-email=kbhogal@us.ibm.com
author2-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).