How to use a Java Bean in a ColdFusion page?

Following steps are to be followed:-

1.Write a Java program that does not contain main method.

2.Complile the java program into .class file(for that you need to install jdk and do the necessary settings)

3.Put the .class file in web_root/WEB-INF/classes(i.e the default class path set in JVM by CF admin). If you want to place .class files somewhere you like,you have to set the class path in CF administrator. For that:-

Go to CF Administrator->Server Setting->Java and JVM->ColdFusion Class Path->Full path to .class    file.

For example:
   If my class i.e SI_Bean.class is present in drive as “D:\Projects\Demo\java_class\SI_Bean.class”
   write D:\Projects\Demo\java_class in ColdFusion Class Path
   Then Press Submit Changes button.
 
4.Restart the Coldfusion Application Server.(For this Go Run->Type services.msc->Click on the Cold    fusion 9 Application Server->click Restart).Remember,every time you change the class path or code of    java class,You have to restart CF server.
 
5.Write a cfml program to make an object of the class and invoke its properties(i.e variables and    methods).For that you can use  or createObject().
   Syntax:-
      
 
      type=”java” because we are creating object of java
      class=”MyClass” class name which you have created.
      name=”myObj” userDefined name I.e instance of your class
                                                or
   
      myObj=createObject(“java”,”MyClass”);
   
   myObj     :    userDefined name i.e instance of your class
   Ist argument: java for creation of java object
   2nd argument:    Name of class you have created.
 
6.To call the methods by the object, Provide necessary parameter.
   e.g
   
                                          OR
   
         result = myObj.methodName(argument1,argument2,.......);
    
 
7.Execute the .cfm page.
 
A simple example that implement java bean in CFML page.
(This example calculates Simple interest)
 
SI_Bean.java
 
public class SI_Bean
{
       private float prin,roi,yr,si;
/**setDetails() method receives parameter from .cfm page*/
 
   public void setDetails(String prin,String roi,String year)
   {
      this.prin=Float.parseFloat(prin);
      this.roi=Float.parseFloat(roi);
      this.yr=Float.parseFloat(year);
   }
/**getSimpleInterest() method calculate the Simple Interest
and return back to .cfm*/
       public float getSimpleInterest()
   {
      si=(prin*yr*roi)/100;
      return si;
   }
}
 
simpleInterest.cfm
 

   
      obj=createobject("java","SI_Bean");
      obj.setDetails(100 , 0.8, 5);
      si=obj.getSimpleInterest();
      writeoutput("CALLING JAVA CLASS");
      writeoutput("Simple Interest is " & #si# & "");
   
150 150 Burnignorance | Where Minds Meet And Sparks Fly!