Vinu Kumar


26 July 2010

Prerequisite knowledge
Required products Sample files
User level
This article assumes that you have a basic knowledge of programming in Java and of the JEE architecture.
Adobe ColdFusion Enterprise Edition (2016 release) (Download trial)

Adobe ColdFusion Builder (2016 release) (Download trial)
(252 KB)


Additional Requirements

JBoss Portal + JBoss Application Server (AS)
In this article, you will learn about deploying ColdFusion in JBoss and about developing ColdFusion applications with Java. The sample application, a travel website, demonstrates how to rewrite your JSP to CFML pages and access Java code from your CFML pages. This article also shows you how to write portlets for the JBoss portal and embed the sample application in the portal.
A ColdFusion application can have CFML pages, ColdFusion components (CFCs), JSPs, and servlets. A CFC is a collection of code and resembles classes in Java. Some of the scenarios of interoperability include the following:
  • You can use existing Java APIs or any other third-party Java APIs within CFML code.
  • You can rewrite complex JSP code to simple CFML code.
  • You can add new CFML on top of existing servlets and JSPs.

Deploying ColdFusion in JBoss

  1. Download ColdFusion using the links in the Requirements section. Install ColdFusion and choose the EAR file option in the installer.
EAR option in the installer
Figure 1. Select the EAR option in the installer
  1. For the TravelNet sample application to work with your installation, use the default context root, cfusion, and the admin password admin. Complete the installation process.
  2. Once the installation is complete, set up the JBoss Portal/JBoss ActionScript if you have not already done so. Go to the directory where the ColdFusion EAR file was generated and expand cfusion.ear and cfusion.war files.
  3. Deploy them to JBoss. Name the directories cfusion.ear and cfusion.war.
  4. Copy the expanded EAR file to {JBOSS.HOME}/servers/default/ and start JBoss ({JBOSS.HOME}/bin/
  5. Browse to http://localhost:8080/cfusion/CFIDE/administrator to configure the admin.
  6. Enable the J2EE session variable in admin by going to Memory Variables > Use J2EE session variables.
Memory option in ColdFusion administrator
Figure 2. Setting the memory option in ColdFusion administrator 
Now you are ready to try the samples and explore some of other Java features of ColdFusion.

Deploying the TravelNet sample application

To deploy the sample application, use the following steps.
  1. Download the sample file linked in the Requirements section.
  2. Expand the TravelNet demo archive. There is an ANT build script available under the travelnet directory, which will help you configure the example. Follow the instructions in the readme.txt to set up the application. The demo is set up based on an HSQL database available with JBoss. Refer to the readme.txt to enable the database.
  3. Browse to http://localhost:8080/cfusion/travelnet to run the demo application.
TravelNet application
Figure 3. Screen shot of the TravelNet application
Analyzing the application
TravelNet is a sample application shipped with the JRun J2EE server. In the demo application, the JSP pages are simply modified to CFML pages and also made to interact with the Java classes. In addition, CFML can also forward calls to a JSP, as follows:
<cfscript> getPageContext().include("forward.jsp"); </cfscript>
The original JSPs are part of the sample application. The next section will cover how you can rewrite each JSP in CFML.
In home.cfm, you can use the cfquery tag to get the list of activities, as follows:
<cfquery datasource="compass" name="compass"> SELECT trip_id, name, teaser, price FROM trip </cfquery> <cfoutput query="compass"> <tr> <td class="contentBG"><a href="tripdetail.cfm?tripId=#trip_id#">#name# </a></td> <td class="contentBG">#teaser#</td> <td class="contentBG">#DollarFormat(price)#</td> </tr> </cfoutput>
As you can see in the above code, the NumberFormat function is replaced directly with the DollarFormat function. ColdFusion provides many built-in functions, which makes the life of a developer easy. Notice that the JSP include directive is replaced with the cfinclude tag. Also, you can pass parameters through the URL and through the target CFML page in the URL scope, as demonstrated in reservation.cfm:
<cfinput type="hidden" name="tripId" value="#URL.TripID#">
In this script you can see that the form element is replaced with the CFFORM tag. The file, reservationaction.cfm, shows how variables are passed from one page to another in form scope. For instance, the variable #form.tripId# retrieves the tripId from the form scope. In this script, the Java class Reservation is invoked to complete the reservation.
resObj = createObject("java","compass.Reservation"); id = resObj.reserve(#form.tripId#, #form.firstName#, #form.lastName#,#form.ccType#, #form.ccNumber#,#form.ccExpiration#);
The Reservation object returns the reservation ID to the CFML page, which then displays within the CFML page.

Writing portlets using ColdFusion

Now that you have seen how you can use CFML within your pages to replace complex JSPs and also how CFML interacts with Java, this section shows you how to create a portlet for the JBoss portal using ColdFusion. There is a sample CFC available in the example archive called Hello.cfc. The EAR installation copies this file to {JBOSS.HOME}/servers/default/deploy/cfusion.ear/cfusion.war/portlets when deployed using the ANT script. This file extends CFIDE.portlets.ColdFusionPortlet and also implements the functions doView and doHelp. The following script shows an example of integrating the TravelNet application in a portlet.
public void function doView(renderRequest,renderResponse) { writeOutput("Welcome to Travel net "); writeOutput('<a href="/cfusion/travel/index.cfm">TravelNet</a>'); compass = new Query(); compass.setSQL('SELECT trip_id, name, teaser, price FROM trip'); compass.setDatasource('compass'); out = compass.execute(); metaInfo = out.getPrefix(); result = out.getResult(); writeOutput('<table width="800" cellspacing="3" cellpadding="8">'); for ( index = 1 ; index LTE metaInfo.RecordCount ; index = (index + 1)){ WriteOutput( '<tr> <td ><a href="/cfusion/travelnet/tripdetail.cfm?tripId=' & result["trip_id"][index] & '">' & result["name"][index] & '</a></td>' & '<td>' & result["teaser"][index] & '</td>'& '<td>' & DollarFormat(result["price"][index]) & '</td>' & '</tr>' ); } }
As you can see in the code snippet above, the CFC queries the compass data source and displays the query results in a table. The code also uses the script support for query introduced in ColdFusion 9.
Integrating to the JBoss portal
Use the following steps to integrate the portlet to the JBoss portal.
  1. Open portlet.xml available at {JBOSS.HOME}/server/default/deploy/cfusion.ear/cfusion.war/WEB-INF/ and update it with the CFC information.
  2. Remove the comment and update the syntax as follows:
<portlet-class>coldfusion.portlet.ColdFusionPortlet</portlet-class> <init-param> <name>cfcName</name> <value>portlets.Hello</value> </init-param>
  1. Start JBoss server and browse to http://localhost:8080/portal.
  2. Log on with the credentials: admin/admin.
  3. Click the Admin option in the upper-right corner and select the Portlet Definition tab.
  4. The "Hello Portlet" appears. Create an instance of the portlet by clicking Create Instance under Actions.
  5. Specify the instance name and instance display name.
  6. Click the Portal Objects tab and create a new portal page.
  7. Select page Layout from Actions.
  8. Select the portlet instance you created before and add it to the required region (Center or left).
  9. Go to the Portal Objects tab and make the new portlet the default portlet.
  10. Click the portal link in the top-right corner to view the newly created portlet.
Portal page
Figure 4. The portal page.
As you can see, writing a dynamic portal is much easier and simpler with ColdFusion 9. The cfscript tag enhancements introduced in ColdFusion 9 will also help Java developers write code faster and help them take advantage of using existing Java applications.

Where to go from here

ColdFusion allows clear separation of business logic and user interface. It is possible to spend less time and enhance the look of a web application through using ColdFusion. ColdFusion also supports integration with hibernate and allows the creation of Ajax-based web UIs. In summary, it’s easy to convert a Java application to a ColdFusion application in a short time, which gives you the capabilities of both Java and ColdFusion on one server.
Refer to the ColdFusion Developer’s Guide to learn about writing ColdFusion applications and portlets for other portal servers.