Presentation is loading. Please wait.

Presentation is loading. Please wait.

Doug Hughes, Alagad Inc. Drinkin’ Cold Coffee -or- How to use Java Objects from ColdFusion.

Similar presentations


Presentation on theme: "Doug Hughes, Alagad Inc. Drinkin’ Cold Coffee -or- How to use Java Objects from ColdFusion."— Presentation transcript:

1 http://www.alagad.com Doug Hughes, Alagad Inc. Drinkin’ Cold Coffee -or- How to use Java Objects from ColdFusion.

2 Doug Hughes, Alagad Inc. http://www.alagad.com What are you going to learn? Why you might want to use Java objects from ColdFusion. Why you might want to use Java objects from ColdFusion. How to instantiate Java objects from ColdFusion MX. How to instantiate Java objects from ColdFusion MX. How to call the constructors of Java objects. How to call the constructors of Java objects. How to call methods on Java objects. How to call methods on Java objects. The advantages of using Java (as opposed to CFX tags, etc) The advantages of using Java (as opposed to CFX tags, etc) Potential problems and their solutions. Potential problems and their solutions.

3 Doug Hughes, Alagad Inc. http://www.alagad.com What can you do with Java? Use strong encryption. Use strong encryption. Manipulate Images. Manipulate Images. Edit audio clips. Edit audio clips. Zip and Unzip files. Zip and Unzip files. Create network socket connections. Create network socket connections. Low level programming. Low level programming. Almost anything you can do with the Java language. Almost anything you can do with the Java language.

4 Doug Hughes, Alagad Inc. http://www.alagad.com Why do you care about Java? ColdFusion is written in Java ColdFusion is written in Java Macromedia has been pushing CF deployment on J2EE servers Macromedia has been pushing CF deployment on J2EE servers In short, an easy way to write J2EE applications. In short, an easy way to write J2EE applications. In short, ColdFusion is a friendly version of Java. In short, ColdFusion is a friendly version of Java. ColdFusion provides an easy way to use Java without requiring extensive knowledge of Java. ColdFusion provides an easy way to use Java without requiring extensive knowledge of Java. Blackstone may deploy ColdFusion applications as WAR files on J2EE. Blackstone may deploy ColdFusion applications as WAR files on J2EE.

5 Doug Hughes, Alagad Inc. http://www.alagad.com Where to find the Java Docs http://java.sun.com/j2se/1.4.2/docs/api/in dex.html http://java.sun.com/j2se/1.4.2/docs/api/in dex.html http://java.sun.com/j2se/1.4.2/docs/api/in dex.html http://java.sun.com/j2se/1.4.2/docs/api/in dex.html The hardest part of using Java is learning how to read the documentation. The hardest part of using Java is learning how to read the documentation.

6 Doug Hughes, Alagad Inc. http://www.alagad.com How to invoke Java objects CreateObject() CreateObject() <cfobjecttype="Java"action="Create"class="java.util.zip.ZipFile" name="myZip" />

7 Doug Hughes, Alagad Inc. http://www.alagad.com Typing in Java vs. ColdFusion Java is a strongly typed language Java is a strongly typed language This means the language always knows what type a variable is This means the language always knows what type a variable is ColdFusion is a typeless language. ColdFusion is a typeless language. This means any variable can be a string, integer, query, whatever. This means any variable can be a string, integer, query, whatever. Because ColdFusion is typeless, CF has to guess what type of data you are passing into Java object methods. Because ColdFusion is typeless, CF has to guess what type of data you are passing into Java object methods. If ColdFusion passes an incorrect type into a Java method you will get errors. If ColdFusion passes an incorrect type into a Java method you will get errors.

8 Doug Hughes, Alagad Inc. http://www.alagad.com An example typing error

9 Doug Hughes, Alagad Inc. http://www.alagad.com Use JavaCast() to type CF variables You can use JavaCast() to type data being passed into Java methods. You can use JavaCast() to type data being passed into Java methods. Casts data from ColdFusion to Java. Casts data from ColdFusion to Java. Intended for calling overloaded Java Methods. Intended for calling overloaded Java Methods. Syntax: Syntax: JavaCast(type, variable)

10 Doug Hughes, Alagad Inc. http://www.alagad.com How to init Java objects Most Java objects require that you call a constructor to configure objects for use. Most Java objects require that you call a constructor to configure objects for use. Use “.init()” to call constructors on Java objects. Use “.init()” to call constructors on Java objects.

11 Doug Hughes, Alagad Inc. http://www.alagad.com Calling Object Constructors To instantiate a Java zipFile object you have to call one of three constructors To instantiate a Java zipFile object you have to call one of three constructors http://java.sun.com/j2se/1.4.2/docs/api/java/util/zip/Zip File.html http://java.sun.com/j2se/1.4.2/docs/api/java/util/zip/Zip File.html http://java.sun.com/j2se/1.4.2/docs/api/java/util/zip/Zip File.html http://java.sun.com/j2se/1.4.2/docs/api/java/util/zip/Zip File.html Example: Example:

12 Doug Hughes, Alagad Inc. http://www.alagad.com What happens when you don’t init an object?

13 Doug Hughes, Alagad Inc. http://www.alagad.com What happens when you don’t init an object?

14 Doug Hughes, Alagad Inc. http://www.alagad.com What happens when there are other problems initing your object?

15 Doug Hughes, Alagad Inc. http://www.alagad.com Calling Java Object methods We can call methods of the myZip object to perform various actions. We can call methods of the myZip object to perform various actions. http://java.sun.com/j2se/1.4.2/docs/api/java/ util/zip/ZipFile.html#getInputStream(java.util. zip.ZipEntry) http://java.sun.com/j2se/1.4.2/docs/api/java/ util/zip/ZipFile.html#getInputStream(java.util. zip.ZipEntry) http://java.sun.com/j2se/1.4.2/docs/api/java/ util/zip/ZipFile.html#getInputStream(java.util. zip.ZipEntry) http://java.sun.com/j2se/1.4.2/docs/api/java/ util/zip/ZipFile.html#getInputStream(java.util. zip.ZipEntry) For instance: For instance: The Zip File "#myZip.getName()#" contains #myZip.size()# files.

16 Doug Hughes, Alagad Inc. http://www.alagad.com Another (more complex) example Get a list of files in the myZip object Get a list of files in the myZip object <cfoutput>#myZipEntry.getName()# (#myZipEntry.getSize()# Bytes) (#myZipEntry.getSize()# Bytes) </cfoutput></cfloop>

17 Doug Hughes, Alagad Inc. http://www.alagad.com Results so far <cfoutput> The Zip File "#myZip.getName()#" contains #myZip.size()# files. The Zip File "#myZip.getName()#" contains #myZip.size()# files. </cfoutput> <cfoutput> #myZipEntry.getName()# (#myZipEntry.getSize()# Bytes) #myZipEntry.getName()# (#myZipEntry.getSize()# Bytes) </cfoutput></cfloop>

18 Doug Hughes, Alagad Inc. http://www.alagad.com Using to get more information on Java Objects You can use on any ColdFusion variable. You can use on any ColdFusion variable. If the variable a Java object you get: If the variable a Java object you get: The object type name The object type name The object’s methods The object’s methods The object’s properties The object’s properties Example: Example:

19 Doug Hughes, Alagad Inc. http://www.alagad.com Results from the dump Can be helpful in figuring out what an object is and what methods it provides. Can be helpful in figuring out what an object is and what methods it provides. Helps support the Java documentation. Helps support the Java documentation. Another dump example: http://alagad.com/JavaF romCF/cfdump.cfm Another dump example: http://alagad.com/JavaF romCF/cfdump.cfm http://alagad.com/JavaF romCF/cfdump.cfm http://alagad.com/JavaF romCF/cfdump.cfm

20 Doug Hughes, Alagad Inc. http://www.alagad.com Wrapping Java in CFCs You can use CFCs to wrap Java objects. You can use CFCs to wrap Java objects. Provides easier access to complex Java objects. Provides easier access to complex Java objects. Extremely reusable and cross platform. Extremely reusable and cross platform.

21 Doug Hughes, Alagad Inc. http://www.alagad.com Let’s look at a simple Zip CFC Create Zip files Create Zip files Add files to Zip files Add files to Zip files Return a query of all the files in a Zip file Return a query of all the files in a Zip file Name Name If the entry is a directory If the entry is a directory Uncompressed size Uncompressed size Compressed size Compressed size Extract files from the zip Extract files from the zip

22 Doug Hughes, Alagad Inc. http://www.alagad.com Alagad Image Component A good example of wrapping Java in CFCs. A good example of wrapping Java in CFCs. 100% native ColdFusion Component (CFC) used to create and manipulate image files by invoking and calling methods on Java objects. 100% native ColdFusion Component (CFC) used to create and manipulate image files by invoking and calling methods on Java objects. Over 60 methods for working with Images. Over 60 methods for working with Images. Platform independent. Platform independent. Makes lots of use of Java from within ColdFusion. Makes lots of use of Java from within ColdFusion. http://alagad.com/index.cfm/name-aic http://alagad.com/index.cfm/name-aic http://alagad.com/index.cfm/name-aic

23 Doug Hughes, Alagad Inc. http://www.alagad.com Advantages of using Java in ColdFusion Cross Platform Cross Platform Write code on any platform and deploy to any platform. Write code on any platform and deploy to any platform. The power of Java at your fingertips. The power of Java at your fingertips. It’s Free! It’s Free! It’s not CFX! It’s not CFX!

24 Doug Hughes, Alagad Inc. http://www.alagad.com What did you learn? Why you might want to use Java objects from ColdFusion. Why you might want to use Java objects from ColdFusion. How to instantiate Java objects from ColdFusion MX. How to instantiate Java objects from ColdFusion MX. How to call the constructors of Java objects. How to call the constructors of Java objects. How to call methods on Java objects. How to call methods on Java objects. The advantages of using Java (as opposed to CFX tags, etc) The advantages of using Java (as opposed to CFX tags, etc) Potential problems and their solutions. Potential problems and their solutions. That you really need to by the Alagad Image Component. That you really need to by the Alagad Image Component.


Download ppt "Doug Hughes, Alagad Inc. Drinkin’ Cold Coffee -or- How to use Java Objects from ColdFusion."

Similar presentations


Ads by Google