Listing 1: Our first fusion of CF with Java
<!--- // Create a java.util.GregorianCalendar
Object // --->
<CFOBJECT ACTION="CREATE" TYPE="Java"
CLASS="java.util.GregorianCalendar" NAME="myCalendar">
<!--- Default Constructor is being called implicitly --->
<!--- Check if 2000 is a leap-year --->
<CFSET is2000LeapYear = myCalendar.isLeapYear(2000)>
<!--- Retrieve current date --->
<CFSET theYear = myCalendar.get(myCalendar.YEAR)>
<CFSET theMonth = myCalendar.get(myCalendar.MONTH)
+ 1>
<CFSET theDay = myCalendar.get(myCalendar.DATE)>
<cfoutput>
<html>
<body>
Is 2000 a leap-year? <b>#is2000LeapYear#</b>
<p>
Today is the <b>#theMonth# / #theDay#
/ #theYear#</b>
</body>
</html>
</cfoutput>
Listing 2: Our sample bean MakeSomewhat.java
// MakeSomewhat.java
package test;
public class MakeSomewhat {
// Properties
private String name = "";
private String[] sentences = new String[3];
// Constructor
public MakeSomewhat() {
this.sentences[0]
= "undefined";
}
// public setter
public void setName(String aName)
{
this.name
= aName;
this.sentences[0]
= "This is the first example to integrate the name "+this.name+" in a sentence...";
this.sentences[1]
= "This is the second example to integrate the name "+this.name+" in a
sentence...";
this.sentences[2]
= "This is the third example to integrate the name "+this.name+" in a sentence...";
}
// public getter
public String getName() {
return this.name;
}
// public getter
public String[] getSentences() {
return this.sentences;
}
}
Listing 3: Using our sample bean within CF
<!--- // Using our MakeSomewhat bean //
--->
<!--- Create a MakeSomewhat Object (note
the package name here) --->
<CFOBJECT ACTION="CREATE" TYPE="Java"
CLASS="test.MakeSomewhat" NAME="mySomewhat">
<!--- Set the name --->
<CFSET void = mySomewhat.setName("John
Doe")>
<!--- Retrieve the result array --->
<CFSET SentenceArray = mySomewhat.getSentences()>
<cfoutput>
<html>
<body>
<!--- Loop over Array --->
<cfloop index="i" from="1" to=#ArrayLen(SentenceArray)#>
#SentenceArray[i]# <br>
</cfloop>
</body>
</html>
</cfoutput>
Listing 4: Using our sample CFX
<html>
<body>
<!--- Retrieve the content listing of a
real zip-file (here c:\logfiles.zip) --->
<CFX_ZipBrowser ARCHIVE="c:\logfiles.zip"
NAME="ZipContent">
<cfoutput>
#ZipContent.RecordCount# files in selected
archive:
</cfoutput>
<table border="1">
<tr bgcolor="#cccccc">
<td><b>Name</b></td>
<td><b>Size</b></td>
<td><b>Compressed</b></td>
</tr>
<cfset sumSize = 0>
<cfset sumCompressed = 0>
<!--- Display the zip-file's content
--->
<CFOUTPUT QUERY="ZipContent">
<tr>
<td>#ZipContent.Name#</td>
<cfset sumSize = sumSize
+ ZipContent.Size>
<td align="right">#NumberFormat(ZipContent.Size)#</td>
<cfset sumCompressed = sumCompressed
+ ZipContent.Compressed>
<td align="right">#NumberFormat(ZipContent.Compressed)#</td>
</tr>
</CFOUTPUT>
<!--- Total --->
<cfoutput>
<tr align="right" bgcolor="##dddddd">
<td><b>Total</b></td>
<td><b>#NumberFormat(sumSize)#</b></td>
<td><b>#NumberFormat(sumCompressed)#</b></td>
</tr>
</cfoutput>
</table>
</body>
</html>
Listing 5: The sample <CFX_ZipBrowser>
extended from Allaires CF Studio help
// ZipBrowser.java
import com.allaire.cfx.*;
import java.util.Hashtable;
import java.io.FileInputStream;
import java.util.zip.*;
public class ZipBrowser implements CustomTag
{
public void processRequest(
Request request, Response response )
throws Exception
{
// validate that required
attributes were passed
if (
!request.attributeExists( "ARCHIVE" ) ||
!request.attributeExists( "NAME" ) )
{
throw new Exception(
"Missing attribute (ARCHIVE and NAME are both " +
"required attributes for this tag)" ) ;
}
// get attribute values
String strArchive
= request.getAttribute( "ARCHIVE" ) ;
String strName
= request.getAttribute( "NAME" ) ;
// create a query to use
for returning the list of files
String[] columns
= { "Name", "Size", "Compressed" } ;
int iName
= 1, iSize = 2, iCompressed = 3 ;
Query files
= response.addQuery( strName, columns ) ;
// read the zip file and
build a query from its contents
ZipInputStream
zin = new ZipInputStream( new FileInputStream(strArchive) ) ;
ZipEntry entry;
while ( (
entry = zin.getNextEntry()) != null )
{
// add a row to the results
int iRow = files.addRow() ;
// populate the row with data
files.setData( iRow, iName, entry.getName() ) ;
files.setData( iRow, iSize, String.valueOf(entry.getSize()) ) ;
files.setData( iRow, iCompressed, String.valueOf(entry.getCompressedSize())
) ;
// finish up with entry
zin.closeEntry() ;
}
// close the
archive
zin.close()
;
}
}
Listing 6: A sample taken from the CF Studio
help of accessing an EJB within CF
<!---
You would use CFOBJECT to create and call
all the appropriate objects. The sequence below assumes that the WeblLogic
JNDI is used to register and find EJBHome instances :
--->
<cfobject action=create type=JAVA
class="weblogic/jndi/Environment" name=wlEnv>
<CFSET ctx = wlEnv.getInitialContext()>
<CFSET ejbHome = ctx.lookup("statelessSession.TraderHome")>
<CFSET trader = ejbHome.Create()>
<CFSET value = trader.shareValue(20, 55.45)>
<cfoutput>
Share value = #value#
</cfoutput>
<CFSET value = trader.remove()>
<!---
The CFOBJECT tag creates the WebLogic Environment
object, which is then used to get the InitialContext. The context object
is used to look up the EJBHome interface. The call to create() results
in getting an instance of stateless session EJB.
--->