This article assumes that you have a basic knowledge of programming in Java and of the JEE architecture.
Intermediate
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:
{JBOSS.HOME}/servers/default/ and start JBoss ({JBOSS.HOME}/bin/run.sh).
Now you are ready to try the samples and explore some of other Java features of ColdFusion.
To deploy the sample application, use the following steps.
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.
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.
Use the following steps to integrate the portlet to the JBoss portal.
{JBOSS.HOME}/server/default/deploy/cfusion.ear/cfusion.war/WEB-INF/ and update it with the CFC information. <portlet-class>coldfusion.portlet.ColdFusionPortlet</portlet-class>
<init-param>
<name>cfcName</name>
<value>portlets.Hello</value>
</init-param>
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.
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.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
Tutorials & Samples |