<ul><li>Leveraging the power under the hood </li></ul>CF and Java
Comparison of Runtime <ul><li>Java </li></ul><ul><li>Write {filename}.java
The javac compiler creates {filename}.class </li></ul><ul><li>ColdFusion </li></ul><ul><li>Write {filename}.cf(c|m)
Server compiles them to {_filename_}.class </li></ul>Java Virtual Machine JVM
Instance (object) Classes and Instances .class is like a blueprint. It gives  the builder the instructions HOW to build so...
'Class'  Methods and Properties <ul><li>That doesn’t mean a class is not useful. We don't always need the real object. </l...
Square footage and size information
A materials list </li></ul></ul><ul><li>These are called ‘class’ methods and properties
In Java, these are referred to in the docs as 'static' </li></ul>
'Instance'  Methods and Properties <ul><li>Similarly, there are some things we don’t know about an object until it is crea...
The location of the house
The owner’s name </li></ul></ul><ul><li>These are known as ‘instance’ methods and properties. </li></ul>
Classes and Instances in CF <ul><li>We create a reference to a Java class in CF by using <cfobject..> of createObject()
<cfset javaMath    = createObject(“java”, “java.lang.Math”)>
We can now call class methods on this
Upcoming SlideShare
Loading in …5
×

CF and Java

1,043 views
945 views

Published on

An introduction into the use of Java inside ColdFusion. With CF being a J2EE application, we always have the ability to use Java if it's necessary. We look at how to get hold of Java objects, the difference between a class and an instance and how CF differentiates between them. Then we look at some examples...

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,043
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

CF and Java

  1. 1. <ul><li>Leveraging the power under the hood </li></ul>CF and Java
  2. 2. Comparison of Runtime <ul><li>Java </li></ul><ul><li>Write {filename}.java
  3. 3. The javac compiler creates {filename}.class </li></ul><ul><li>ColdFusion </li></ul><ul><li>Write {filename}.cf(c|m)
  4. 4. Server compiles them to {_filename_}.class </li></ul>Java Virtual Machine JVM
  5. 5. Instance (object) Classes and Instances .class is like a blueprint. It gives the builder the instructions HOW to build something..all the implementation details.. BUT ..you wouldn’t want to live in a blueprint. For a real house, we have to ‘construct’ the house. Class C:Documents and Settings ick.harveyLocal SettingsTemporary Internet FilesContent.IE5U0BPZ5RVMPj03995150000[1].jpg
  6. 6. 'Class' Methods and Properties <ul><li>That doesn’t mean a class is not useful. We don't always need the real object. </li></ul><ul><ul><li>How many rooms
  7. 7. Square footage and size information
  8. 8. A materials list </li></ul></ul><ul><li>These are called ‘class’ methods and properties
  9. 9. In Java, these are referred to in the docs as 'static' </li></ul>
  10. 10. 'Instance' Methods and Properties <ul><li>Similarly, there are some things we don’t know about an object until it is created... </li></ul><ul><ul><li>What color the house is
  11. 11. The location of the house
  12. 12. The owner’s name </li></ul></ul><ul><li>These are known as ‘instance’ methods and properties. </li></ul>
  13. 13. Classes and Instances in CF <ul><li>We create a reference to a Java class in CF by using <cfobject..> of createObject()
  14. 14. <cfset javaMath = createObject(“java”, “java.lang.Math”)>
  15. 15. We can now call class methods on this
  16. 16. Q. How do you know what are class methods? A. Check out the Java API docs. </li></ul>
  17. 17. Example of Java Doc http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html
  18. 18. Calling Methods <ul><li>Calling methods on Java objects, is much the same as calling them on CF ones. We use the dot notation.. </li></ul><ul><cfset sinX = javaMath.sin(X)> (class method) <cfset myPI = javaMath.PI> (class property) </ul><ul><li>javaMath is a reference to the Math class, .sin() is a class method and PI is a class property. (demo) </li></ul>
  19. 19. The Constructor <ul><li>But what if we need the instance? (this is usually where I make most mistakes)
  20. 20. To create an instance from a class we call the constructor method. In CF, we call the constructor by using init()
  21. 21. HOWEVER...
  22. 22. In CF, if we don’t call it ourselves, CF will do it for us.
  23. 23. BEWARE, sometimes, WE must pass data in via the constructor, in which case, WE have to call it ourselves. </li></ul>
  24. 24. Implicit Constructor <ul><li>Example of CF calling the constructor for us
  25. 25. To create an instance of java.lang.StringBuffer, we don’t pass anything in the constructor, so we can leave it to CF.
  26. 26. <cfset javaSB = createObject(“java”, “java.lang.StringBuffer”)>
  27. 27. <cfset javaSB.init()>
  28. 28. <cfset javaSB.append(“some text for my buffer”)>
  29. 29. append() is an instance method, CF automatically calls the constructor. (demo) </li></ul>
  30. 30. Explicit Constructor <ul><li>Example of a constructor that must be called explicitly.
  31. 31. To create an instance of java.io.File, we need to pass in the path/URI of the file.
  32. 32. <cfset javaFile = createObject(“java”, “java.io.File”)>
  33. 33. <cfset javaFile.init()>
  34. 34. <cfset ok2read = javaFile.exists()>
  35. 35. If we let CF instantiate and then try and call an instance method [eg exists()], we get an error. </li></ul><ul><cfset javaFile.init(“c: empcfug.txt”)> </ul>
  36. 36. Performance..? <ul><li>Creating & destroying instances costs processing power and hence time, so be wise about creating new objects.
  37. 37. Some objects are immutable, which means their value can't change. Assigning them new values, causes Java to destroy the old one and create a new one.
  38. 38. The CF String (==Java String) is a good example </li></ul>
  39. 39. DNS Example <ul><li>The code is drawn from Pete Freitag's blog and uses the JRE's java.net.* package
  40. 40. The example shows how to use Java's networking API to do (reverse) DNS lookups directly from within CF </li></ul>
  41. 41. Using External Java Libraries <ul><li>The examples so far have all used classes in the core JRE (Java Runtime Environment), but what if you want to use someone else's Java code </li></ul>1. You need to have the .class file (or compile it) and then place it where CF can see it. 2. You need the API so you can use it.
  42. 42. FLV Example <ul><li>The code is drawn from Ray Camden's blog and illustrates the use of someone's Java code inside CF.
  43. 43. Compile the .java to a .class and copy to {server_root}/WEB-INF/classes/
  44. 44. Restart CF </li></ul>

×